def spectrogram(ds, select=(None, None, None), newpath='/sgw', winwidth=.01, tstep = .0001, fstart=10000, fstop=120000, fstep=500, wintype='no'): ''' Contstruct a timeseries dataset that is a corse spectogram of the data D specified by ds and select (which should also be a time series, containing only one channel). The result will have length tsteps and width fsteps.The nth channel contains the frequency specified by the nth element of range(fstart, fstop, fstep). The value of the specrogram at each sample point is the magnitude of the real fourier transform of a region of length "winwidth" times a windowing function. The nth point is based on a window centered tstep*n into the domain. Winwidth and tstep are specified in seconds, not sample points. wintype determines the shape of the windowing function. Currently supported values are "no" (a flat "window") and "triangle" SWITCHVALUES(wintype)=['no', 'triangle'] ''' dat = getSelection(ds, select) h = getSelectionHeader(ds, select) fs = h['SamplesPerSecond'] nfs = 1.0/tstep winwidth =int(round(winwidth*fs/2.0)) window = None if wintype=="triangle": window = np.linspace(0, 1, winwidth) window = np.concatenate([window, window[::-1]]) nft = winwidth + 1 ind = np.arange(fstart, fstop, fstep) ind = nft * ind / (fs/2.0) ind = ind.astype(np.int32)[::-1] tstep = int(round(tstep*fs)) xind = np.arange(winwidth, dat.shape[0]-winwidth, tstep).astype(np.int32) ft = np.zeros((xind.shape[0], ind.shape[0])) for i in range(xind.shape[0]): d = dat[xind[i]-winwidth:xind[i]+winwidth,0] #warning, very strange behavior of rfft for 2d arrays, #even if shape is Nx1 or 1xN. if window!=None: d = d * window d = np.fft.rfft(d) ft[i,:] = abs(d[ind]) ds.createSubData(newpath, ft, {'SampleType':'timeseries', 'SamplesPerSecond':nfs, 'imageYrange':(fstart, fstop-fstart)}, True)
def mixtureModel(ds, select=(None, None, None), minCenters=3, maxCenters=10, model="Gaussian_pk_Lk_Ck", name="MixtureModel", runs=5): centers = range(minCenters, maxCenters+1) dat = getSelection(ds, select) mod = _mmcall(dat, centers, model, False, runs) doc = ds.getInstance('/') try: mm = doc.getInstance("/AbstractModel:%s" % name) mmb = mm.getInstance("MienBlock:gmm") par = mmb.getElements("Parameters", depth=1)[0] except: mm = createElement("AbstractModel", {"Name":name}) mmb = createElement('MienBlock', {"Name":"gmm",'Function':'ccbcv.gmm.gmm'}) par= createElement("Parameters", {}) mmb.newElement(par) mm.newElement(mmb) doc.newElement(mm) mm.setAttrib('total_weight', 1.0) w, m, c = _mod2par(mod) args = {'weights':w, 'means':m, 'covs':c} par.setValue(args, override=True)