Beispiel #1
0
def main(argv):

    time = maxslope = nslope = scmin = 0
    a = wfdb.WFDB_Anninfo()
    annot = wfdb.WFDB_Annotation()

    if len(argv) < 2:
        print "usage:", argv[0], "record [threshold]"
        sys.exit(1)
    
    a.name = "qrs"
    a.stat = wfdb.WFDB_WRITE

    nsig = wfdb.isigopen(argv[1], None, 0)
    if nsig < 1: sys.exit(2)
    s = wfdb.WFDB_SiginfoArray(nsig)
    v = wfdb.WFDB_SampleArray(nsig)
    if wfdb.wfdbinit(argv[1], a, 1, s.cast(), nsig) != nsig: sys.exit(2)
    if wfdb.sampfreq(None) < 240. or wfdb.sampfreq(None) > 260.:
	wfdb.setifreq(250.)
    if len(argv) > 2: scmin = wfdb.muvadu(0, argv[2])
    if scmin < 1: scmin = wfdb.muvadu(0, 1000)
    slopecrit = scmax = 10 * scmin
    ms160 = wfdb.strtim("0.16")
    ms200 = wfdb.strtim("0.2")
    s2 = wfdb.strtim("2")
    annot.subtyp = annot.chan = annot.num = 0
    annot.aux = None
    wfdb.getvec(v.cast())
    t9 = t8 = t7 = t6 = t5 = t4 = t3 = t2 = t1 = v[0]

    while 1:
        t0 = v[0]
        filter = t0 + 4*t1 + 6*t2 + 4*t3 + t4 - t5 - 4*t6 - 6*t7 - 4*t8 - t9
        if time % s2 == 0:
            if nslope == 0:
                slopecrit -= slopecrit >> 4
                if slopecrit < scmin: slopecrit = scmin
            elif nslope >= 5:
                slopecrit += slopecrit >> 4
                if slopecrit > scmax: slopecrit = scmax
        if nslope == 0 and abs(filter) > slopecrit: 
            nslope = 1
            maxtime = ms160
            if filter > 0:
                sign = 1
            else:
                sign = -1
            qtime = time
        if nslope != 0:
            if filter * sign < -slopecrit: 
                sign = -sign
                nslope = nslope + 1
                if nslope > 4:       
                    maxtime = ms200
                else:
                    maxtime = ms160
            elif filter * sign > slopecrit and \
                     abs(filter) > maxslope:
                maxslope = abs(filter)
            if maxtime < 0:
                if 2 <= nslope and nslope <= 4:
                    slopecrit += ((maxslope>>2) - slopecrit) >> 3
                    if slopecrit < scmin:
                        slopecrit = scmin
                    elif slopecrit > scmax:
                        slopecrit = scmax
                    annot.time = wfdb.strtim("i") - (time - qtime) - 4
                    annot.anntyp = wfdb.NORMAL
                    wfdb.putann(0, annot)
                    time = 0
                elif nslope >= 5: 
                    annot.time = wfdb.strtim("i") - (time - qtime) - 4
                    annot.anntyp = wfdb.ARFCT
                    wfdb.putann(0, annot)
                nslope = 0
            maxtime = maxtime - 1
        t9 = t8
        t8 = t7
        t7 = t6
        t6 = t5
        t5 = t4
        t4 = t3
        t3 = t2
        t2 = t1
        t1 = t0
        time = time + 1
        if not wfdb.getvec(v.cast()) > 0: break

    wfdb.wfdbquit()
Beispiel #2
0
def main(argv):
    nbeats = stoptime = 0
    a = wfdb.WFDB_Anninfo()
    annot = wfdb.WFDB_Annotation()

    if len(argv) < 3:
        print "usage:", argv[0], "annotator record [beat-type from to]",
        sys.exit(1)    
    a.name = argv[1]
    a.stat = wfdb.WFDB_READ
    nsig = wfdb.isigopen(argv[2], None, 0)
    if nsig < 1: sys.exit(2)
    s = wfdb.WFDB_SiginfoArray(nsig)
    v = wfdb.WFDB_SampleArray(nsig)
    vb = wfdb.WFDB_SampleArray(nsig)
    sum = [None] * nsig
    if wfdb.wfdbinit(argv[2], a, 1, s.cast(), nsig) != nsig: sys.exit(3)
    hwindow = wfdb.strtim(".05")
    window = 2*hwindow + 1
    for i in range(nsig):
        sum[i] = [0] * window
    if len(argv) > 3:
        btype = wfdb.strann(argv[3])
    else:
        btype = wfdb.NORMAL
    if len(argv) > 4: wfdb.iannsettime(wfdb.strtim(argv[4]))
    if len(argv) > 5:
        stoptime = wdfb.strtim(argv[5])
        if stoptime < 0:
            stoptime = -stoptime
        if s[0].nsamp > 0 and stoptime > s[0].nsamp:
            stoptime = s[0].nsamp    
    else:
        stoptime = s[0].nsamp
    if stoptime > 0: stoptime -= hwindow

    while 1:
        if not (wfdb.getann(0, annot) == 0 and annot.time < hwindow): break 
    while 1:        
        if annot.anntyp == btype:
            wfdb.isigsettime(annot.time - hwindow - 1)
            wfdb.getvec(vb.cast())
            j=0
            while j < window and wfdb.getvec(v.cast()) > 0:
                for i in range(nsig):
                    sum[i][j] += v[i] - vb[i]
                j += 1
            nbeats += 1
        if not (wfdb.getann(0, annot) == 0 and \
           (stoptime == 0L or annot.time < stoptime)): break
    
    if nbeats < 1:
        print argv[0] + ": no `" + wfdb.annstr(btype) + "' beats found"
        sys.exit(4)

    print "Average of", nbeats, "`" + wfdb.annstr(btype) + "' beats:"
    for j in range(window):
        for i in range(nsig):
            print "%(av)g" % {'av': float(sum[i][j])/nbeats},
            sys.stdout.write("") # surpress next space
            if i < nsig-1:
                print "\t",
            else:
                print
    wfdb.wfdbquit()