I work with two signals, the second being perfectly delayed and attenuated, with no reverberation. I implemented cross-correlation algorithm, which yields me these results:
Here is my code for these results:
res1_fft = np.fft.fft(res1_red)
res2_fft = np.fft.fft(res2_red)
prod = np.conjugate(res1_fft) * res2_fft
CC = np.fft.ifft(prod)
abs = [i for i in range(-int(len(CC)/2),int(len(CC)/2))]
plt.plot(abs,np.fft.fftshift(np.real(CC)))
plt.grid()
plt.show()
Please note that I both tried with my data being zero-padded or not. In both case I get a pike at the right delay of 137 samples I imposed.
However as soon as I add the PHAT filtering with this code:
res1_fft = np.fft.fft(res1_red)
res2_fft = np.fft.fft(res2_red)
prod = np.conjugate(res1_fft) * res2_fft
CC = np.fft.ifft(prod)
sign = 1/(np.abs(prod)) * prod
GCC_vrai = np.fft.ifft(sign)
and then plot GCC_vrai
, I get weird results, with a pike always in 0:
I also tried this piece of code I found there (with a similar problem seen there too), with a very similar result.
N = len(res1_red)
Ncorr = 2*N-1; # N size of the signal
NFFT = 2**nextpow2(Ncorr)
R12 = np.fft.fft(res2_red,NFFT)*np.conjugate(np.fft.fft(res1_red,NFFT))
r12_temp = np.fft.fftshift(np.fft.ifft(np.exp(1j*np.angle(R12))))
r12 = r12_temp[int(NFFT/2+1-(N-1)/2):int(NFFT/2+1+(N-1)/2)];
abs = [i for i in range(-int(len(r12)/2),int(len(r12)/2))]
plt.plot(abs,np.real(r12))
plt.grid()
plt.show()
What am I doing wrong?