def test_xcorr_acorrt(self): data1 = np.sin(np.arange(1001) * 2 * np.pi / 500.) cor1 = xcorrt(data1, data1, 1000) cor2 = acorrt(data1, 1000, oneside=False, clipdata=False) # from pylab import plot, show, subplot, legend # subplot(211) # plot(data1) # subplot(212) # plot(cor1, label='xcorrt') # plot(cor2, label='acorr clip=False') # legend() # show() np.testing.assert_array_almost_equal(cor1, cor2)
def test_xcorr_acorrf(self): data1 = np.sin(np.arange(1001) * 2.1 * np.pi / 500.) cor1 = acorrt(data1, 1024, oneside=True, clipdata=False) cor2 = acorrf(data1, 1024, oneside=True, clipdata=False) # from pylab import plot, show, subplot, legend # subplot(211) # plot(data1) # subplot(212) # plot(cor1, label='acorrt') # plot(cor2, label='acorrf') # legend() # show() print (np.sum((cor1 - cor2) ** 2) / len(cor1)) ** 0.5 self.assertTrue((np.sum((cor1 - cor2) ** 2) / len(cor1)) ** 0.5 < 0.1)
def test_xcorr_acorrf(self): data1 = np.sin(np.arange(1001) * 2.1 * np.pi / 500.) cor1 = acorrt(data1, 1024, oneside=True, clipdata=False) cor2 = acorrf(data1, 1024, oneside=True, clipdata=False) # from pylab import plot, show, subplot, legend # subplot(211) # plot(data1) # subplot(212) # plot(cor1, label='acorrt') # plot(cor2, label='acorrf') # legend() # show() print(np.sum((cor1 - cor2)**2) / len(cor1))**0.5 self.assertTrue((np.sum((cor1 - cor2)**2) / len(cor1))**0.5 < 0.1)
def deconvt(rsp_list, src, spiking, shift, length=None, normalize=True): """ Time domain deconvolution. Calculate Toeplitz auto-correlation matrix of source, invert it, add noise and multiply it with cross-correlation vector of response and source. In a formula: RF = (STS + spiking*I)^-1 * STR This function calculates RF. N... length ( S0 S-1 S-2 ... S-N+1 ) ( S1 S0 S-1 ... S-N+2 ) S = ( S2 ... ) ( ... ) ( SN-1 ... S0 ) R = (R0 R1 ... RN-1)^T RF = (RF0 RF1 ... RFN-1)^T S... source matrix (shape N*N) R... response vector (length N) RF... receiver function (deconvolution) vector (length N) STS = S^T*S = Toeplitz autocorrelation matrix STR = S^T*R = cross-correlation vector I... Identity :parameters: rsp_list (list of) data containing the response src data of source spiking random noise added to autocorrelation (eg. 1.0, 0.1) shift shift the source by that amount of samples to the left side to get onset in RF at the right time (negative -> shift source to the right side) shift = (middle of rsp window - middle of src window) + (0 - middle rf window) length number of data points of deconvolution normalize if True normalize all deconvolutions so that the maximum of the first deconvolution is 1 :return: (list of) deconvolutions (length N) """ # time_rf = winrf[1]-winrf[0] = length / samp # shift_zero=int(samp*( # (winrsp[1] - winrsp[0] - winsrc[1] + winsrc[0] - time_rf) / 2 # + winrsp[0] - winsrc[0] - winrf[0])) # = shift_zero=int(samp*( # (winrsp[1] + winrsp[0] - winsrc[1] - winsrc[0] -winrf[1] - winrf[0]) / 2 # # if length == None: length = len(src) flag = False RF_list = [] STS = acorrt(src, length, demean=False, clipdata=False) STS[0] += spiking #print shift, len(src), len(rsp_list), spiking, length if not isinstance(rsp_list, (list, tuple)): flag = True rsp_list = [rsp_list] for rsp in rsp_list: STR = xcorrt(rsp, src, length // 2, shift, demean=False) if len(STR) > len(STS): STR = np.delete(STR, -1) RF = toeplitz(STS, STR) RF_list.append(RF) if normalize: norm = 1 / np.max(np.abs(RF_list[0])) for RF in RF_list: RF *= norm if flag: return RF else: return RF_list # comment return commands for testing samp = 50. from pylab import plot, subplot, show subplot(411) plot(np.arange(len(src)) / samp, src) plot(np.arange(len(rsp)) / samp, rsp) subplot(412) plot(np.arange(len(STS)) / samp, STS) subplot(413) plot(np.arange(len(STR)) / samp, STR) subplot(414) plot(np.arange(len(RF)) / samp, RF_list[0]) show()