# PHAT filtering for cross-correlation yields wrong results while simple cross-correlation does OK (Python)

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?

• Cross correlation benefits from averaging but can give a reasonable answer. The normalized estimators need to be averaged. MSC will need around a hundred averages to start being useful.
– user28715
Commented Jun 7, 2018 at 6:18
• Cross correlation indeed gives me a reasonable result. Yet I am there (in the pictures) working with a perfectly delayed and attenuated SINGLE source, while in my real experiment I also work in a zone where I have one source but also some little noise. As cross correlation gives a large pike, I wanted to use this PHAT filtering which is said to narrow pikes to have better results. What is MSC? I actually didn't really understand your answer... Commented Jun 7, 2018 at 6:25
• You also need to time align the 2 channels. The alignment delay with the greatest response is your delay estimate
– user28715
Commented Jun 7, 2018 at 6:27
• Magnitude squared coherence
– user28715
Commented Jun 7, 2018 at 6:29
• Well I indeed want to find the alignment delay thanks to GCC-PHAT. Commented Jun 7, 2018 at 6:36