trial_lengths = np.zeros(n_trials, dtype=np.int32) print "computing trial lengths..." for filename in data_files: channel, trial = get_channel(filename), get_trial(filename) if channel == 1: trial_lengths[trial] = os.path.getsize(filename) // 2 print "trial length %i: %d" % (trial, trial_lengths[trial]) trial_starts = np.append(0, np.cumsum(trial_lengths)) n_samples_total = trial_starts[-1] if not dry_run: Data_sc = np.memmap(datfilename, shape=(n_samples_total, n_channels), dtype=out_dtype, mode="w+") for filename in data_files: if not dry_run: X_c = np.fromfile(filename, dtype=in_dtype) channel, trial = get_channel(filename), get_trial(filename) print "inserting data from %s" % filename if not dry_run: Data_sc[trial_starts[trial]:trial_starts[trial + 1], channel] = X_c if not dry_run: del Data_sc xmlpath = switch_ext(datfilename, "xml") print "writing %s" % xmlpath write_xml(n_channels, 0, 0, 24000., xmlpath) os.chdir(orig_dir)
plot(t[::10],out1,'r') plot(t[::10],out2,'g') legend(('original','noisy signal','1-way','2-way')) print "Notice phase lag from 1-way (causal) filter" hold(False) show() if __name__ == '__main__': usage = """ Downsample your data. Default ratio is 16, so N_out = floor(N_in/16). By default this script does a two-way filter (like filtfilt). To see why, try %prog -t If output file is not specified, it will have extension eeg (foo.dat -> foo.eeg) %prog your_dat_file.dat [output filename] %prog -h displays help""" parser = OptionParser(usage) parser.add_option("-1",help="One-way filter.",action="store_false",default=True,dest="two_way") parser.add_option("-r",help="Downsampling ratio",type=int,action="store",dest="ratio",default=RATIO) parser.add_option("-t",help="Test 1-way and two-way filtering.",action="store_true",dest="test",default=False) parser.add_option("--in-type",help="Datatype of input file. One of i2,i4,i8,f4,f8. Default i2 (16-bit integer).",action="store",dest="dt_in",default=DT_IN) parser.add_option("--out-type",help="Datatype of output file. One of i2,i4,i8,f4,f8. Default i2 (16-bit integer).",action="store",dest="dt_out",default=DT_OUT) (opts,args) = parser.parse_args() if opts.test: test_ds() exit() OUTNAME = args[1] if len(args) > 1 else switch_ext(args[0],"eeg") DT_IN = opts.dt_in DT_OUT = opts.dt_out downsample(args[0],OUTNAME,two_way=opts.two_way,ratio=opts.ratio)
def filter_2way(b,a,in_memmap,out_memmap): print "Forward filtering" filter_1way(b,a,in_memmap,out_memmap) print "Backward filtering" filter_1way(b,a,out_memmap[::-1],out_memmap[::-1]) if __name__ == '__main__': usage = """ Filter your data between f_hi and f_lo, measured in Hz (the frequencies you give will be compared with sample rate in xml file.) Give either f_hi, f_lo, or both using --hi and --lo. By default this script does a two-way filter (like filtfilt). If output file is not specified, it will have extension fil (foo.dat -> foo.fil) %prog your_dat_file.dat [output filename] [--hi=f_hi] [--lo=f_lo] %prog -h displays help""" parser = OptionParser(usage) parser.add_option("-1",help="One-way filter.",action="store_false",default=True,dest="two_way") parser.add_option("--hi",help="Top of passband (Hz)",type=float,action="store",dest="hi") parser.add_option("--lo",help="Bottom of passband (Hz)",type=float,action="store",dest="lo") parser.add_option("--in-type",help="Datatype of input file. One of i2,i4,i8,f4,f8. Default i2 (16-bit integer).",action="store",dest="dt_in",default=DT_IN) parser.add_option("--out-type",help="Datatype of output file. One of i2,i4,i8,f4,f8. Default i2 (16-bit integer).",action="store",dest="dt_out",default=DT_OUT) (opts,args) = parser.parse_args() if opts.hi is None and opts.lo is None: parser.error("must specify high pass freq or low pass freq with --hi or --lo") OUTNAME = args[1] if len(args) > 1 else switch_ext(abspath(args[0]),"fil") DT_IN = opts.dt_in DT_OUT = opts.dt_out filt(args[0],OUTNAME,two_way=opts.two_way,f_hi=opts.hi,f_lo=opts.lo)
type=int, action="store", dest="ratio", default=RATIO) parser.add_option("-t", help="Test 1-way and two-way filtering.", action="store_true", dest="test", default=False) parser.add_option( "--in-type", help= "Datatype of input file. One of i2,i4,i8,f4,f8. Default i2 (16-bit integer).", action="store", dest="dt_in", default=DT_IN) parser.add_option( "--out-type", help= "Datatype of output file. One of i2,i4,i8,f4,f8. Default i2 (16-bit integer).", action="store", dest="dt_out", default=DT_OUT) (opts, args) = parser.parse_args() if opts.test: test_ds() exit() OUTNAME = args[1] if len(args) > 1 else switch_ext(args[0], "eeg") DT_IN = opts.dt_in DT_OUT = opts.dt_out downsample(args[0], OUTNAME, two_way=opts.two_way, ratio=opts.ratio)
type=float, action="store", dest="hi") parser.add_option("--lo", help="Bottom of passband (Hz)", type=float, action="store", dest="lo") parser.add_option( "--in-type", help= "Datatype of input file. One of i2,i4,i8,f4,f8. Default i2 (16-bit integer).", action="store", dest="dt_in", default=DT_IN) parser.add_option( "--out-type", help= "Datatype of output file. One of i2,i4,i8,f4,f8. Default i2 (16-bit integer).", action="store", dest="dt_out", default=DT_OUT) (opts, args) = parser.parse_args() if opts.hi is None and opts.lo is None: parser.error( "must specify high pass freq or low pass freq with --hi or --lo") OUTNAME = args[1] if len(args) > 1 else switch_ext(abspath(args[0]), "fil") DT_IN = opts.dt_in DT_OUT = opts.dt_out filt(args[0], OUTNAME, two_way=opts.two_way, f_hi=opts.hi, f_lo=opts.lo)
print "%i channels, %i trials"%(n_channels, n_trials) trial_lengths = np.zeros(n_trials,dtype=np.int32) print "computing trial lengths..." for filename in data_files: channel,trial = get_channel(filename),get_trial(filename) if channel == 1: trial_lengths[trial] = os.path.getsize(filename)//2 print "trial length %i: %d"%(trial,trial_lengths[trial]) trial_starts = np.append(0,np.cumsum(trial_lengths)) n_samples_total = trial_starts[-1] if not dry_run: Data_sc = np.memmap(datfilename,shape=(n_samples_total,n_channels),dtype=out_dtype,mode="w+") for filename in data_files: if not dry_run: X_c = np.fromfile(filename,dtype=in_dtype) channel,trial = get_channel(filename),get_trial(filename) print "inserting data from %s"%filename if not dry_run: Data_sc[trial_starts[trial]:trial_starts[trial+1],channel] = X_c if not dry_run: del Data_sc xmlpath = switch_ext(datfilename,"xml") print "writing %s"%xmlpath write_xml(n_channels,0,0,24000.,xmlpath) os.chdir(orig_dir)