def singleChannelNoise(ds, timeStart=0.0, segmentLength=10.0, pad=0.5, reps=10, bandMin=5, bandMax=400, fnameRigFilter='', seed=0): '''Construct a single chanel band limited white noise stimulus. bandMin and bandMax specify the frequency of the band pass. segmentLength specifies the length of a noise segment in seconds. pad specifies an amount of silence surrounding the noise segment (pad occurs both before and after the noise). Reps specifies a number of times to repeat the pad,segment pair (thus, the total stimulus will be (segmentLength+2*pad)*reps seconds long). If specified, fnameRigFilter is a file name specifying a mat file containing a rig-specific velocity to voltage filter, which should be applied to the stimulus (after band passing). Seed, if specified and True (e.g. not 0), seeds the random number generator''' sl=int(round(segmentLength*ds.fs())) pad=int(round(pad*ds.fs())) l=reps*(sl+2*pad) xstart=int(round(timeStart*ds.fs())) sel = (None, [0], (xstart, xstart+l)) if seed: random.seed(seed) dat=concatenate([zeros(pad), random.randn(sl), zeros(pad)]) dat=bandpass(dat, bandMin, bandMax, ds.fs()) fnameRigFilter=fnameRigFilter.strip() if fnameRigFilter: h=readmatfile(fnameRigFilter) h=h['h'] ff=h['vel2volt0'] opt=h['vel2volt_offsetpts_0'][0][0] hfs=h['sampfreq'][0][0] if hfs!=ds.fs(): ff=array_resample(ff, hfs, ds.fs()) opt=int(round(ds.fs()/hfs)) dat=_cfilt(dat, ff, opt) dat=resize(dat, reps*dat.shape[0]) sd=dat[pad:-pad].std() dat*=1.0/sd dat=reshape(dat, (-1, 1)) setSelection(ds, dat, sel) fn='gwn%ito%iHz%ireps%isec_' % (bandMin, bandMax, reps, segmentLength) if fnameRigFilter: fn+="filtered" else: fn+="raw" ds.setAttrib('fname', fn)
def GWN(ds, select=(None, None, None), bandMin=5.0, bandMax=150.0, std=1.0, rseed=None, invert=False): '''Generates band limited Gaussian white noise in the frequency band (bandMin, bandMax) with mean 0, and standard deviation std. This noise is added to all the data specified by select. If rseed is specified it will be used to seed the random number generator before generation. This will allow repeated generation of identical "noise". Rseed should probably be an integer, though it may also be an array of ints. If invert is True, the noise will be multiplied by -1. This is usally irelevant, but can be used in combination with rseed to add and later remove identicle noise samples. Notes: std is specified before bandpass filtering, so for very narrow bandwidths it will act as an upper bound rather than an exact measure. bandMin and bandMax are implicitly limited to the range 0, ds.fs()/2, and no filter will be applied if they are outside these limits, or if both are False. bandMax will be automatically set to ds.fs()/2 if it is None but bandMin is >0. ''' if rseed: seed(rseed) od=getSelection(ds, select) wn=normal(0, std, od.shape) if bandMin or bandMax: fs=getSelectionHeader(ds, select)['SamplesPerSecond'] nqf=fs/2 if bandMax==None: bandMax=nqf if bandMin==bandMax: print "eek. Zero bandwidth. aborting." return elif bandMin>bandMax: print "which part of 'Min' and 'Max' don't you understand?" bandMin, bandMax=bandMax, bandMin if bandMin>=0 and bandMax<=nqf: for i in range(wn.shape[1]): wn[:,i]=bandpass(wn[:,i], bandMin, bandMax, fs) if invert: wn*=-1 wn=od+wn setSelection(ds, wn, select)
def _gtoSpikes(g, noise, fs): noise = noise * (g.max() - g.min()) if noise > 0: wn=normal(0, noise, g.shape[0]) nqf=fs/2 bandMax=200 bandMin = 5 wn=bandpass(wn, bandMin, bandMax, fs) g = g + wn g = g - g.min() g = g/g.max() g = 1.0/(1.0+exp( (-1*(g-.5))*40)) ht=.7 lt=.3 evts = [] hit=nonzero1d(g>ht) if len(hit)==0: return array(evts) fcross=take(hit, nonzero1d((hit-shift(hit, 1))!=1)) evts.append(hit[0]) for j in range(1, fcross.shape[0]): if any(g[fcross[j-1]:fcross[j]]<lt): evts.append(fcross[j]) return array(evts)
def _genWN(sl, sd): if len(sd['freq'])>2: random.seed(sd['freq'][2]) dat=concatenate([zeros(200), random.randn(sl), zeros(200)]) dat=bandpass(dat, sd['freq'][0], sd['freq'][1], sd['fs']) return dat[200:-200]