Ejemplo n.º 1
0
def main(argv):
    nc = len(argv) - 4

    if len(argv) < 4:
        print "usage:",  argv[0], "record start duration [ coefficients ... ]"
        sys.exit(1)
    if nc < 1: nc = 1
    c = [0.] * nc
    for i in range(0, nc):
        c[i] = float(argv[i+4])
    nsig = wfdb.isigopen(argv[1], None, 0)
    if nsig < 1: sys.exit(3)
    s = wfdb.WFDB_SiginfoArray(nsig)
    v = wfdb.WFDB_SampleArray(nsig)
    if wfdb.isigopen(argv[1], s.cast(), nsig) != nsig: sys.exit(3)
    if wfdb.isigsettime(wfdb.strtim(argv[2])) < 0: sys.exit(4)
    nsamp = wfdb.strtim(argv[3])
    if nsamp < 1:
        print argv[0]+ ": inappropriate value for duration"
        sys.exit(5)
    if wfdb.osigopen("16l", s.cast(), nsig) != nsig: sys.exit(6)
    wfdb.sample(0, 0)
    for t in range(0, nsamp):
        if not wfdb.sample_valid(): break
        for j in range(0, nsig):
            vv = 0
            for i in range(0, nc):
                if c[i] != 0.: vv += c[i] * wfdb.sample(j, t+i)
            v[j] = int(vv)
        if wfdb.putvec(v.cast()) < 0: break
    wfdb.newheader("out")
    wfdb.wfdbquit()
Ejemplo n.º 2
0
def setupWfdb(rec_name, annotator ):
    nsig = openWfdbSignal(rec_name);
    
    #Allocate memory for sig info array
    #we can use siarray to access WFDB_Siginfo structure
    siarray = wfdb.WFDB_SiginfoArray(nsig);
    
    #Allocate memory for data
    sdata = wfdb.WFDB_SampleArray(nsig);
    
    #Open WFDB record
    wfdb.isigopen(rec_name, siarray.cast(), nsig);
    
    
    #read annotations from file
    #WFDB_Anninfor() contains name and attributes of annotator .atr etc
    a = wfdb.WFDB_Anninfo();
    
    #WFDB_Annotation describes the attributes of signals 
    #declare object in c : WFDB_Annotation annot; see below for declaring object in python
    annot = wfdb.WFDB_Annotation();
    
    #read name and status of annotation file
    #a.name="atr";
    #a.name="ecg";
    #a.name="output_annotator"
    a.name=annotator;
    a.stat = wfdb.WFDB_READ;
    
    freq=wfdb.sampfreq(rec_name);
    nsamp=siarray[0].nsamp;
    print ("sampling frequency is: " + str(freq))
    init_time=wfdb.timstr(0);
    #print("strtim for starting value is: " + str(wfdb.strtim(init_time)));
    
    ##comment june 16
    ###### print signal specification #####
    record_info=wfdb.getinfo(rec_name)
    #print("getinfor is " + str(record_info));

    # print("total num of samples: " + str(nsamp));
    # print "Starting time of record is: "+ str(init_time);
    # print("sampling frequency is:"+ str(freq));      
    ########## READ ANNOTATION ##################
    if wfdb.annopen(rec_name, a, 1) < 0: 
        print("cannot open aanopen");
        exit();
    
    return (nsamp, freq, annot, init_time,sdata);
Ejemplo n.º 3
0
def main(argv):
    siarray = wfdb.isigopen("100s")
    if siarray.nsig < 2: sys.exit(1)
    v = wfdb.WFDB_SampleArray(2)
    for i in range(0,10):
        if wfdb.getvec(v.cast()) < 0: sys.exit(2)
        print "\t%d\t%d" % (v[0], v[1])
Ejemplo n.º 4
0
def openWfdbSignal(rec_name):
    #Find the number of signals in record
    nsig = wfdb.isigopen(rec_name, None, 0);
    if nsig<0:
        print "number of signals < 0, error opening signal record";
        exit();
    print "Number of signals: " + str(nsig) +" in record: "+ rec_name;
    return nsig;
Ejemplo n.º 5
0
def main(argv):
    nsamp = 1000
    if len(argv) < 2:
        print "usage:", argv[0], "record"
        sys.exit(1)
    nsig = wfdb.isigopen(argv[1], None, 0)
    if nsig <= 0: sys.exit(2)
    s = wfdb.WFDB_SiginfoArray(nsig)
    vin = wfdb.WFDB_SampleArray(nsig)
    vout = wfdb.WFDB_SampleArray(nsig)
    if wfdb.isigopen(argv[1], s.cast(), nsig) != nsig: sys.exit(2)
    if wfdb.osigopen("8l", s.cast(), nsig) <= 0: sys.exit(3)
    while nsamp > 0 and wfdb.getvec(vin.cast()) > 0:
        nsamp -= 1
        for i in range(0, nsig): vout[i] -= vin[i]
        if wfdb.putvec(vout.cast()) < 0: break
        for i in range(0, nsig): vout[i] = vin[i]
    wfdb.newheader("dif")
    wfdb.wfdbquit()
Ejemplo n.º 6
0
def main(argv):
    if len(argv) < 2:
        print "usage:", argv[0], "record"
        sys.exit(1)
    nsig = wfdb.isigopen(argv[1], None, 0)
    if nsig < 1: sys.exit(2)
    s = wfdb.WFDB_SiginfoArray(nsig)
    if wfdb.isigopen(argv[1], s.cast(), nsig) != nsig: sys.exit(2)
    print "Record", argv[1]
    print "Starting time:", wfdb.timstr(0)
    print "Sampling frequency: %(sf)g Hz" % {'sf': wfdb.sampfreq(argv[1])}
    print nsig, "signals"
    for i in range(0,nsig):
        print "Group %(g)d, Signal %(s)d:" % {'g': s[i].group, 's': i }
        print " File:", s[i].fname
        print " Description:", s[i].desc
        print " Gain:",
        if s[i].gain == 0.:
            print "uncalibrated; assume", wfdb.WFDB_DEFGAIN,
        else:
            print "%(gain)g" % {'gain': s[i].gain},
        if s[i].units is not None:
            print "adu/" + s[i].units
        else:
            print "adu/mV"
        print " Initial value:", s[i].initval
        print " Storage format:", s[i].fmt
        print " I/O:",
        if s[i].bsize == 0:
            print "can be unbuffered"
        else:
            print str(s[i].bsize) + "-byte blocks"
        print " ADC resolution:", s[i].adcres, "bits"
        print " ADC zero:", s[i].adczero
        if s[i].nsamp > 0:
            print " Length:", wfdb.timstr(s[i].nsamp), \
                  "(" + str(s[i].nsamp) + " sample intervals)"
            print " Checksum:", s[i].cksum
        else:
            print " Length undefined"
    wfdb.wfdbquit()
Ejemplo n.º 7
0
def main(argv):
    record = ''

    # Parse the arguments
    try:
        opts, args = getopt.getopt(argv, "hr:", ["help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt == '-r':
            record = arg

    # Read the number of signals in the record
    nsig = wfdb.isigopen(record, None, 0)

    # Exit if the record is not found, or there are no signals
    if nsig < 1:
        usage()
        sys.exit(2)

    siarray = wfdb.WFDB_SiginfoArray(nsig)
    wfdb.isigopen(record, siarray, nsig)

    n = 0
    v = wfdb.intArray(nsig)

    # Loop over each sample and print the signal values.
    while wfdb.getvec(v) > 0:
        print n,
        for i in range(0,nsig):
            print v[i],
        print
        n = n + 1
        
    wfdb.wfdbquit()
Ejemplo n.º 8
0
 def read(self , record = None):
     if record == None:
         if self.record == None:
             sys.exit(1)
         else:
             record = self.record
     else:
         self.record = record
         
     sig0 = []
     sigt = []
     
     siarray = wfdb.WFDB_SiginfoArray(2)
     
     if (wfdb.isigopen(record , siarray.cast() , 2) < 1):
         sys.exit(1)
     
     v = wfdb.WFDB_SampleArray(2)
     
     
     #Getting X Axes Interval
     #Appending data to array
     X_INTERVAL = 20
     pause = 0
     for i in range(siarray.cast().nsamp):
         if ( wfdb.getvec(v.cast()) < 0 ):
             break
         sig0.append(v.__getitem__(0))
         sigt.append(pause)
         pause = pause + X_INTERVAL
         pass
     
     #High Pass Filtering
     highpass_filter = HighpassFilter()
     high_pass = highpass_filter.process(sig0,siarray.cast().nsamp)
     #Low pass Filtering
     lowpass_filter = LowpassFilter()
     low_pass = lowpass_filter.process(high_pass, len(high_pass))
     
     qrs = QRSDetector()
     qrs_data = qrs.process(low_pass, len(low_pass))
     
     feature_extractor = FeatureExtractor()
     features = feature_extractor.get_RR_interval(sig0, qrs_data)
     
     #sig0 = qrs_dat
       
     
     wfdb.wfdbquit() 
     
     return sigt , sig0 , features
Ejemplo n.º 9
0
    def read(self, record=None):
        if record == None:
            if self.record == None:
                sys.exit(1)
            else:
                record = self.record
        else:
            self.record = record

        sig0 = []
        sigt = []

        siarray = wfdb.WFDB_SiginfoArray(2)

        if (wfdb.isigopen(record, siarray.cast(), 2) < 1):
            sys.exit(1)

        v = wfdb.WFDB_SampleArray(2)

        #Getting X Axes Interval
        #Appending data to array
        X_INTERVAL = 20
        pause = 0
        for i in range(siarray.cast().nsamp):
            if (wfdb.getvec(v.cast()) < 0):
                break
            sig0.append(v.__getitem__(0))
            sigt.append(pause)
            pause = pause + X_INTERVAL
            pass

        #High Pass Filtering
        highpass_filter = HighpassFilter()
        high_pass = highpass_filter.process(sig0, siarray.cast().nsamp)
        #Low pass Filtering
        lowpass_filter = LowpassFilter()
        low_pass = lowpass_filter.process(high_pass, len(high_pass))

        qrs = QRSDetector()
        qrs_data = qrs.process(low_pass, len(low_pass))

        feature_extractor = FeatureExtractor()
        features = feature_extractor.get_RR_interval(sig0, qrs_data)

        #sig0 = qrs_dat

        wfdb.wfdbquit()

        return sigt, sig0, features
Ejemplo n.º 10
0
 def initialise(self, **kwds):
 #----------------------------
   fname = str(self.dataset)
   global _WFDBLock, _CurrentRecord, _RecordCount
   with _WFDBLock:
     if _CurrentRecord and _CurrentRecord != fname:
       raise IOError("WFDB does not allow concurrent access to different recordings")
     _CurrentRecord = fname
     _RecordCount += 1
   wfdb.setgvmode(wfdb.WFDB_HIGHRES)
   ## http: url needs .hea extension??
   #logging.debug('Opening: %s (%s)', fname, uri)
   if fname.startswith(PHYSIOBANK):
     fname = fname[len(PHYSIOBANK):]
   self._siginfo = wfdb.isigopen(fname)
   if self._siginfo is None:
     raise IOError("Cannot open header for '%s'" % fname)
   self._nsignals = self._siginfo.nsig
   for s in self.signals():
     WFDBSignal.initialise_class(s)
Ejemplo n.º 11
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()
Ejemplo n.º 12
0
def main(argv):
    db_path = "/opt/physiobank/database"
    record = ''
    wfdb.setwfdb(db_path)
    # Parse the arguments
    try:
        opts, args = getopt.getopt(argv, "hr:", ["help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt == '-r':
            record = arg

    # Read the number of signals in the record
    nsig = wfdb.isigopen(record, None, 0)

    # Exit if the record is not found, or there are no signals
    if nsig < 1:
        usage()
        sys.exit(2)

    siarray = wfdb.WFDB_SiginfoArray(nsig)
    wfdb.isigopen(record, siarray, nsig)

    n = 0
    v = wfdb.intArray(nsig)
    lf = bf_math.intArray(siarray[0].nsamp)
    global sld_samp
    global ln
    global y_axes
    global sr_ds  
    global subplt
    sr = (int)(wfdb.sampfreq(record))
    print "sr:",sr
    sr_ds = 50
    # Loop over each sample and print the signal values.
    while wfdb.getvec(v) > 0:
        for i in range(0,1):
            lf[n] = v[i]
        n = n + 1
    bf_math.filtering(lf,siarray[0].nsamp,sr)
    _nsamp = siarray[0].nsamp
    
    x_axes = xrange(_nsamp*sr_ds/sr)
    print "x_axes:", _nsamp*sr_ds/sr
    y_axes = []
    for i in xrange(_nsamp):
         b, val = bf_math.down_sample(lf[i], sr, sr_ds)
         if (b):
             y_axes.append(val)
    print "y_axes:", len(y_axes)
    plt.figure(1)
    subplt = plt.subplot(111)
    ln, = plt.plot(xrange(0,5*sr_ds), y_axes[0:5*sr_ds] )
    #plt.axis([-100, 100])
    plt.ylim(-300,300)
    #subplt.autoscale_view(True,True,True)
    axcolor = 'lightgoldenrodyellow'
    axsamp = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)
    sld_samp = Slider(axsamp, 'samp', 0.1, 33*60, valinit=1)

    sld_samp.on_changed(update)
    plt.show()    
    wfdb.wfdbquit()
Ejemplo n.º 13
0
sig_time = []
count = 0
ann_graph = []
split_time0 = []
annotator_array = []

#Database and record name
db_name = "afpdb"

annotation = dload_annotator_names(db_name)[0]
#rec_name=dload_rec_names(db_name)[0];
record = "n08"
rec_name = "afpdb/" + record

#Find the number of signals in record
nsig = wfdb.isigopen(rec_name, None, 0)

if nsig < 0:
    print "error opening signal record"
    exit()

print "Number of signals: " + str(nsig) + " in record: " + rec_name

#Allocate memory for sig info array
#we can use siarray to access WFDB_Siginfo structure
siarray = wfdb.WFDB_SiginfoArray(nsig)

#Allocate memory for data
sdata = wfdb.WFDB_SampleArray(nsig)

#Open WFDB record
Ejemplo n.º 14
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()
count=0;
ann_graph=[];
split_time0=[];
annotator_array=[];

#Database and record name
db_name="afpdb";


annotation=dload_annotator_names(db_name)[0];
#rec_name=dload_rec_names(db_name)[0];
record="n08"
rec_name = "afpdb/"+record

#Find the number of signals in record
nsig = wfdb.isigopen(rec_name, None, 0);

if nsig<0:
    print "error opening signal record";
    exit();

print "Number of signals: " + str(nsig) +" in record: "+ rec_name;

#Allocate memory for sig info array
#we can use siarray to access WFDB_Siginfo structure
siarray = wfdb.WFDB_SiginfoArray(nsig);

#Allocate memory for data
sdata = wfdb.WFDB_SampleArray(nsig);

#Open WFDB record