def test_acorr(self): data1 = np.sin(np.arange(1001) * 2 * np.pi / 500.) data2 = np.sin(np.arange(1001) * 2 * np.pi / 500.) data2[:100] = 0 data2[-100:] = 0 cor1 = xcorr.xcorr(data1, data1, 1000, padding=1) cor2 = xcorr.acorr(data1, 1000, twosided=True, padding=1) cor3 = xcorr.acorr(data1, 1000, twosided=True, padding=1000) cor4 = xcorr.acorr(data1, 1000, shift_zero=-100, twosided=False, padding=1) cor5 = xcorr.acorr(data1, 1000, shift_zero=100, twosided=False, padding=1) # from pylab import plot, show, subplot, legend # subplot(211) # plot(data1) # subplot(212) # plot(cor1, label='xcorr.xcorr') # plot(cor2, label='xcorr.acorr') # plot(cor3, label='xcorr.acorr padding=1000') # plot(cor4, label='xcorr.acorr shifted -100 twosided=False') # plot(cor5, label='xcorr.acorr shifted 100 twosided=False') # legend() # show() np.testing.assert_array_almost_equal(cor1, cor2) np.testing.assert_array_almost_equal(cor1, cor3)
def test_xcorr(self): N=1001 data1 = np.sin(np.arange(N//2+1)/100.) data2 = np.e**(-(np.arange(N)-500)**2/100.) - np.e**(-(np.arange(N)-50)**2/100.) + 5*np.e**(-(np.arange(N)-950)**2/100.) cor1 = scipy.signal.correlate(data1 - np.mean(data1), data2-np.mean(data2), 'full') cor2 = xcorr.xcorr(data1, data2, 750, padding=0) cor3 = xcorr.xcorr(data1, data2, 750, padding=1) cor1 *= max(cor2)/max(cor1) cor4 = scipy.signal.correlate(data2-np.mean(data2), data1 - np.mean(data1), 'full') cor5 = xcorr.xcorr(data2, data1, 750, padding=0) cor6 = xcorr.xcorr(data2, data1, 750, padding=1) cor5b = xcorr.xcorr(data2, data1, 750, shift_zero=-100, padding=0) cor5c = xcorr.xcorr(data2, data1, 750, shift_zero=100, padding=0) cor4 *= max(cor5)/max(cor4) cor7 = scipy.signal.correlate(data1, data2, 'full') cor8 = xcorr.xcorr(data1, data2, 750, padding=0, demean=0) cor7 *= max(cor8)/max(cor7) cor9 = xcorr.xcorr(data1, data2, 750, shift_zero=100, padding=0, demean=0) cor10 = xcorr.xcorr(data1, data2, 750, shift_zero=100, twosided=False, padding=0, demean=0) # from pylab import plot, show, subplot, legend # subplot(411) # plot(data1) # plot(data2) # subplot(412) # plot(cor1, label='scipy.signal demeaned') # plot(cor2, label='xcorr.xcorr') # plot(cor3, label='xcorr.xcorr padded to same length') # legend() # subplot(413) # plot(cor4, label='scipy.signal demeaned') # plot(cor5, label='xcorr.xcorr') # plot(cor5b, label='xcorr.xcorr shifted -100') # plot(cor5c, label='xcorr.xcorr shifted 100') # plot(cor6, label='xcorr.xcorr padded to same length') # legend() # subplot(414) # plot(cor7, label='scipy.signal') # plot(cor8, label='xcorr.xcorr not demeaned') # plot(cor9, label='xcorr.xcorr shifted 100') # plot(cor10, label='xcorr.xcorr shifted 100 twosided=False') # legend() # show() np.testing.assert_array_almost_equal(cor1, cor2) np.testing.assert_array_almost_equal(cor4, cor5) np.testing.assert_array_almost_equal(cor7, cor8) np.testing.assert_array_almost_equal(cor5[200:300], cor5b[100:200]) np.testing.assert_array_almost_equal(cor5[200:300], cor5c[300:400])
def sync(c,os): aX = xcorr.xcorr( os, pilot.pilot(1) ) ret = [] for i in range( os*60 ): ret.append( aX.ce(c[i]) ) pos = ret.index(max(ret)) return ( pos, ret )
def pitch_autocorrelation(signal, sample_frequence, frame_width, shift_width, threshold): signal = normalization(signal) frames = framing(signal, sample_frequence, frame_width, shift_width) frames_energy = [] for frame in frames: frames_energy.append(energy(frame)) fundamental_frequency_per_frame = [] for i in range(len(frames)): if frames_energy[i] > threshold: #Voiced try: lags, corr = xcorr(frames[i], maxlag=int(sample_frequence / 50)) peaks, properties = sig.find_peaks(corr) peaks_prominences = sig.peak_prominences(signal, peaks)[0] peaks_prominences = list(peaks_prominences) index_max_1 = peaks_prominences.index(max(peaks_prominences)) peaks_prominences_copy = peaks_prominences del peaks_prominences_copy[index_max_1] index_max_2 = peaks_prominences_copy.index( max(peaks_prominences_copy)) if index_max_2 >= index_max_1: index_max_2 += 1 postion_max_1 = peaks[index_max_1] postion_max_2 = peaks[index_max_2] distance = abs(postion_max_1 - postion_max_2) fundamental_period = distance / sample_frequence fundamental_frequency = 1 / fundamental_period except: fundamental_frequency = 0 else: # Unvoiced fundamental_frequency = 0 fundamental_frequency_per_frame.append(fundamental_frequency) return fundamental_frequency_per_frame
def getHeartRateSchmidt(audio_data, Fs): ## Get heatrate: # From Schmidt: # "The duration of the heart cycle is estimated as the time from lag zero # to the highest peaks between 500 and 2000 ms in the resulting # autocorrelation" # This is performed after filtering and spike removal: Fs = float(Fs) ## 25-400Hz 4th order Butterworth band pass audio_data = bf.butterworth_lowpass_filter(audio_data, 2, 400, Fs) audio_data = bf.butterworth_highpass_filter(audio_data, 2, 25, Fs) ## Spike removal from the original paper: audio_data = ssr.schmidt_spike_removal(audio_data, Fs) ## Find the homomorphic envelope homomorphic_envelope = heh.Homomorphic_Envelope_with_Hilbert( audio_data, Fs) ## Find the autocorrelation: y = homomorphic_envelope - np.mean(homomorphic_envelope) c = xcorr.xcorr(y) signal_autocorrelation = c[len(homomorphic_envelope) + 1:] min_index = 0.5 * Fs max_index = 2 * Fs index = np.argmax(signal_autocorrelation[min_index:max_index]) true_index = index + min_index - 1 heartRate = 60 / (true_index / Fs) ## Find the systolic time interval: # From Schmidt: "The systolic duration is defined as the time from lag zero # to the highest peak in the interval between 200 ms and half of the heart # cycle duration" max_sys_duration = int(np.round(((60 / heartRate) * Fs) / 2)) min_sys_duration = np.round(0.2 * Fs) pos = np.argmax(signal_autocorrelation[min_sys_duration:max_sys_duration]) systolicTimeInterval = (min_sys_duration + pos) / Fs return heartRate, systolicTimeInterval
def autocorrelation(frame, fs, threshold): """ autocorrelation(frame, fs, threshold) Compute the pitch of frame using the autocorrelation method. Parameters ---------- frame : ndarray fs : float The sampling threshold : float The threshold bellow which the frame is unvoiced. Returns ------- pitch : float """ if frame_energy(frame) < threshold: return 0 lags, corr = xcorr(frame, maxlag=fs // 50) distance = get_distance(lags, corr) return fs / distance
def test_acorr(self): data1 = np.sin(np.arange(1001)*2*np.pi/500.) data2 = np.sin(np.arange(1001)*2*np.pi/500.) data2[:100]=0 data2[-100:]=0 cor1 = xcorr.xcorr(data1, data1, 1000, padding=1) cor2 = xcorr.acorr(data1, 1000, twosided=True, padding=1) cor3 = xcorr.acorr(data1, 1000, twosided=True, padding=1000) cor4 = xcorr.acorr(data1, 1000, shift_zero=-100, twosided=False, padding=1) cor5 = xcorr.acorr(data1, 1000, shift_zero=100, twosided=False, padding=1) # from pylab import plot, show, subplot, legend # subplot(211) # plot(data1) # subplot(212) # plot(cor1, label='xcorr.xcorr') # plot(cor2, label='xcorr.acorr') # plot(cor3, label='xcorr.acorr padding=1000') # plot(cor4, label='xcorr.acorr shifted -100 twosided=False') # plot(cor5, label='xcorr.acorr shifted 100 twosided=False') # legend() # show() np.testing.assert_array_almost_equal(cor1, cor2) np.testing.assert_array_almost_equal(cor1, cor3)
def crossCorrelate(self, a, b, maxDelay): # maxDelay cannot be greater the length of the arrays, that will cause errors assert maxDelay < len(a) lags, coeffs = xcorr(a, b, maxlags=maxDelay) return lags, coeffs
# #fr_true[pltslice, n], # 3*fr_std[pltslice,n], # sgax=axs[n], # alpha=0.5) axs[i, j].set_ylim(-0.05, 1.1) axs[i, j].set_ylabel("$\lambda_{}(t)$".format(k + 1)) axs[i, j].set_title("Firing Rates") axs[i, j].set_xlabel("Time") k = k + 1 plt.tight_layout() fig.savefig("TVpgGLM/fig/syn_tv_N10_raster.pdf") ##################################### ### Cross-correlation analysis (p3)## ##################################### f_true = xcorr.xcorr(fr_true, dtmax=20) f_est = xcorr.xcorr(fr_est, dtmax=20) plt.figure() fig, axs = plt.subplots(N, N, sharey=True) for i in range(N): for j in range(N): axs[i, j].plot(f_true[i, j, :], color=color[0]) axs[i, j].plot(f_est[i, j, :], color=color[1]) axs[i, j].set_xticklabels([]) axs[i, j].set_yticklabels([]) fig.savefig("TVpgGLM/fig/syn_tv_N10_xcorr.pdf") ###################################
import xcorr import numpy as np from __microphone__ import Microphone mic1 = Microphone(0,0, 'WavFileDatabase/Data2/mic1.wav') mic2 = Microphone(0,0,'WavFileDatabase/Data2/mic2.wav') test1 = mic1.getAmplitudeList() test2 = mic2.getAmplitudeList() test1 = test1[0:50] test2 = test2[0:50] a = np.asarray(test1) b = np.asarray(test2) lags, c = xcorr.xcorr(test2, test1 ,normed=True, maxlags=49 ) print lags print c
print("5-pk") print(GW_five_percent2) gw_time=gw_time gw=gw_data ir_time2=ir_time ir=ir_data lpf_ir_data2=lowpass(ir,300e3) lpf_gw_data=lowpass(gw,700e3) plt.plot(gw_time*1e6,lpf_gw_data,ir_time[50:]*1e6,lpf_ir_data[50:]) plt.plot([gw_time[0]*1e6,gw_time[-1]*1e6],[0.05*np.max(lpf_gw_data),0.05*np.max(lpf_gw_data)],'r-') plt.xlabel('Time ($\mu s$)') plt.ylabel('data 2 Uncalibrated Ez from DBY station') plt.show() delay=xcorr(ir_time[50:],ir_time2[50:],lpf_ir_data[50:],lpf_ir_data2[50:]) #This part of the code accounts for the xcorr from 5% of GW peak if GW_five_percent2>GW_five_percent: xcorr=np.abs(delay)+(np.abs(GW_five_percent2-GW_five_percent)) if delay<0: xcorr=xcorr*-1 else: xcorr=xcorr print("xcorr_delay") print(xcorr) else: xcorr=np.abs(delay)-(np.abs(GW_five_percent2-GW_five_percent)) if delay<0:
def calculateDelay(self, x, y): a = np.asarray(x) b = np.asarray(y) lags, c = xcorr(a, b, normed=True, maxlags=(len(a) - 1)) timeDelay = lags[np.argmax(c)] return timeDelay
def test_xcorr(self): N = 1001 data1 = np.sin(np.arange(N // 2 + 1) / 100.) data2 = np.e**(-(np.arange(N) - 500)**2 / 100.) - np.e**( -(np.arange(N) - 50)**2 / 100.) + 5 * np.e**(-(np.arange(N) - 950)**2 / 100.) cor1 = scipy.signal.correlate(data1 - np.mean(data1), data2 - np.mean(data2), 'full') cor2 = xcorr.xcorr(data1, data2, 750, padding=0) cor3 = xcorr.xcorr(data1, data2, 750, padding=1) cor1 *= max(cor2) / max(cor1) cor4 = scipy.signal.correlate(data2 - np.mean(data2), data1 - np.mean(data1), 'full') cor5 = xcorr.xcorr(data2, data1, 750, padding=0) cor6 = xcorr.xcorr(data2, data1, 750, padding=1) cor5b = xcorr.xcorr(data2, data1, 750, shift_zero=-100, padding=0) cor5c = xcorr.xcorr(data2, data1, 750, shift_zero=100, padding=0) cor4 *= max(cor5) / max(cor4) cor7 = scipy.signal.correlate(data1, data2, 'full') cor8 = xcorr.xcorr(data1, data2, 750, padding=0, demean=0) cor7 *= max(cor8) / max(cor7) cor9 = xcorr.xcorr(data1, data2, 750, shift_zero=100, padding=0, demean=0) cor10 = xcorr.xcorr(data1, data2, 750, shift_zero=100, twosided=False, padding=0, demean=0) # from pylab import plot, show, subplot, legend # subplot(411) # plot(data1) # plot(data2) # subplot(412) # plot(cor1, label='scipy.signal demeaned') # plot(cor2, label='xcorr.xcorr') # plot(cor3, label='xcorr.xcorr padded to same length') # legend() # subplot(413) # plot(cor4, label='scipy.signal demeaned') # plot(cor5, label='xcorr.xcorr') # plot(cor5b, label='xcorr.xcorr shifted -100') # plot(cor5c, label='xcorr.xcorr shifted 100') # plot(cor6, label='xcorr.xcorr padded to same length') # legend() # subplot(414) # plot(cor7, label='scipy.signal') # plot(cor8, label='xcorr.xcorr not demeaned') # plot(cor9, label='xcorr.xcorr shifted 100') # plot(cor10, label='xcorr.xcorr shifted 100 twosided=False') # legend() # show() np.testing.assert_array_almost_equal(cor1, cor2) np.testing.assert_array_almost_equal(cor4, cor5) np.testing.assert_array_almost_equal(cor7, cor8) np.testing.assert_array_almost_equal(cor5[200:300], cor5b[100:200]) np.testing.assert_array_almost_equal(cor5[200:300], cor5c[300:400])