def _gammatonefilter(self, signal, chan=0): """ a gammatone filter bank """ B, A = filtering.gammatone_biquad(44100, self.size, 150) # run the 4 biquads y = scipy.signal.lfilter(B[chan, 0, :], A[chan, 0, :], signal) y = scipy.signal.lfilter(B[chan, 1, :], A[chan, 1, :], y) y = scipy.signal.lfilter(B[chan, 2, :], A[chan, 2, :], y) y = scipy.signal.lfilter(B[chan, 3, :], A[chan, 3, :], y) return y
def _gammatonefilter(self, signal, chan=0): """ a gammatone filter bank """ B,A = filtering.gammatone_biquad(44100,self.size,150) # run the 4 biquads y = scipy.signal.lfilter(B[chan,0,:],A[chan,0,:],signal) y = scipy.signal.lfilter(B[chan,1,:],A[chan,1,:],y) y = scipy.signal.lfilter(B[chan,2,:],A[chan,2,:],y) y = scipy.signal.lfilter(B[chan,3,:],A[chan,3,:],y) return y
def testSerialIIRFilters(self, level=1): """ test correspondence of a cascaded IIR filter """ # set parameters for non-interacting neurons: # new state only depends on the input self.net.setSimAlgorithm(SIM_FILTER) self.ins = 1 self.outs = 1 self.net.setReservoirAct(ACT_LINEAR) self.net.setOutputAct(ACT_LINEAR) self.net.setInputs(self.ins) self.net.setOutputs(self.outs) self.net.setInitParam(ALPHA, 0.) self.net.setInitParam(FB_CONNECTIVITY, 0.) self.net.setInitParam(IN_CONNECTIVITY, 1.) self.net.setInitParam(IN_SCALE, 0.) self.net.setInitParam(IN_SHIFT, 1.) self.net.init() # set paramas for a gammatone filter # (cascade of 4 biquads) Btmp, Atmp = filtering.gammatone_biquad(44100, self.size, 150) B = N.empty((self.size, 12)) A = N.empty((self.size, 12)) serial = 4 B[:, 0:3] = Btmp[:, 0, :] B[:, 3:6] = Btmp[:, 1, :] B[:, 6:9] = Btmp[:, 2, :] B[:, 9:12] = Btmp[:, 3, :] A[:, 0:3] = Atmp[:, 0, :] A[:, 3:6] = Atmp[:, 1, :] A[:, 6:9] = Atmp[:, 2, :] A[:, 9:12] = Atmp[:, 3, :] self.net.setIIRCoeff(B, A, serial) # simulate network step by step: # the states x are now only the filtered input signal, which # is the same for each neuron ! (because ALPHA = 0) indata = N.random.rand(1, self.sim_size) * 2 - 1 indata = N.asfarray(indata, self.dtype) filterout = N.zeros((1, self.sim_size), self.dtype) outtmp = N.zeros((self.outs), self.dtype) for n in range(self.sim_size): intmp = indata[:, n].copy() self.net.simulateStep(intmp, outtmp) filterout[0, n] = self.net.getX()[0] # now calculate the same with scipy.signal.lfilter filterout2 = self._gammatonefilter(indata.flatten()) assert_array_almost_equal(filterout.flatten(), filterout2)
def testSerialIIRFilters(self, level=1): """ test correspondence of a cascaded IIR filter """ # set parameters for non-interacting neurons: # new state only depends on the input self.net.setSimAlgorithm(SIM_FILTER) self.ins = 1 self.outs = 1 self.net.setReservoirAct(ACT_LINEAR) self.net.setOutputAct(ACT_LINEAR) self.net.setInputs( self.ins ) self.net.setOutputs( self.outs ) self.net.setInitParam(ALPHA, 0.) self.net.setInitParam(FB_CONNECTIVITY, 0.) self.net.setInitParam(IN_CONNECTIVITY, 1.) self.net.setInitParam(IN_SCALE, 0.) self.net.setInitParam(IN_SHIFT, 1.) self.net.init() # set paramas for a gammatone filter # (cascade of 4 biquads) Btmp,Atmp = filtering.gammatone_biquad(44100,self.size,150) B = N.empty((self.size,12)) A = N.empty((self.size,12)) serial = 4 B[:,0:3] = Btmp[:,0,:] B[:,3:6] = Btmp[:,1,:] B[:,6:9] = Btmp[:,2,:] B[:,9:12] = Btmp[:,3,:] A[:,0:3] = Atmp[:,0,:] A[:,3:6] = Atmp[:,1,:] A[:,6:9] = Atmp[:,2,:] A[:,9:12] = Atmp[:,3,:] self.net.setIIRCoeff(B,A,serial) # simulate network step by step: # the states x are now only the filtered input signal, which # is the same for each neuron ! (because ALPHA = 0) indata = N.random.rand(1,self.sim_size)*2-1 indata = N.asfarray(indata, self.dtype) filterout = N.zeros((1,self.sim_size),self.dtype) outtmp = N.zeros((self.outs),self.dtype) for n in range(self.sim_size): intmp = indata[:,n].copy() self.net.simulateStep( intmp, outtmp ) filterout[0,n] = self.net.getX()[0] # now calculate the same with scipy.signal.lfilter filterout2 = self._gammatonefilter( indata.flatten() ) assert_array_almost_equal(filterout.flatten(),filterout2)
def setGammatoneFilters(self, fs, lowFreq): """ Set to each neuron one gammatone filter so that the filter frequencies will reach from lowFreq to fs/2 (each neuron will have a different gammatone filter !) fs sample rate lowFreq lowest filter frequency """ size = self.getSize() B, A = filtering.gammatone_biquad(fs, size, lowFreq) # 4 biquads self.B = N.empty((size, 12)) self.A = N.empty((size, 12)) self.serial = 4 self.B[:, 0:3] = B[:, 0, :] self.B[:, 3:6] = B[:, 1, :] self.B[:, 6:9] = B[:, 2, :] self.B[:, 9:12] = B[:, 3, :] self.A[:, 0:3] = A[:, 0, :] self.A[:, 3:6] = A[:, 1, :] self.A[:, 6:9] = A[:, 2, :] self.A[:, 9:12] = A[:, 3, :]