I created two signals in the time domain: signal A which is a square wave, and signal B which is a right-angled triangle wave, as shown in the picture below.

 Signal A recorded in the time domain

Signal B recorded in the time domain

I took the FFT of these signals, multiplied channel 1 FFT with the conjugate of channel 2 FFT. Then I inverse-FFT'd to observe it back in the time domain, and I observed this:

enter image description here

Now, instead if I did the correlation directly in the time domain using signal in Python like this:

txcorr = signal.correlate(wave1, wave2, mode = 'same', method='direct')

I see this output:

enter image description here

So, my assumption is taking the conjugate of a signal in the frequency domain reverses the time axis? Could someone give me a more mathematical background or where I could read to understand what is happening exactly in the frequency domain? Of course, when I simply do the multiplication in the frequency domain without taking the conjugate of a signal, it produces the shape expected

Additionally, this is probably the biggest confusion: Why is the "maximum point" of the cross-correlated signal in the time domain not at 0.3s? This is what I expected from what cross-correlation page in wikipedia showed: enter image description here

  • $\begingroup$ Welcome to SE.SP! You have used the same length for the output as the input. In general, if you do correlate or convolve signals of lengths $N$ and $M$ samples, then the result will be of length $N + M - 1$. You have an output of $N$ samples, the same as both inputs. This may lead to problems. Also, can you explain why you used the options you did in your call to signal.correlate. $\endgroup$ – Peter K. Feb 25 at 17:17
  • $\begingroup$ Ahhh that makes much more sense.. I see in your answer that you used 'full' as the mode for signal.correlate, I had made it 'same' because I was confused why I was getting twice the number of values. Do you have any material where I can read why this is the case, when two frequency signals are multiplied? $\endgroup$ – anony Mar 2 at 15:02

The issue is that the FFT version always assumes that the indices start numbering from $n=0$. The result of the FFT version will be periodic, with period the same as the FFT length.

To show the same result as the correlation version, you need to use numpy.fft.fftshift.

Here is my attempt to generate your original signals:

Initial signals

Then the two versions of correlation:

Original result

which seems to do what you're seeing.

Whnn I apply fftshift to the FFT version, I get:

Result using fftshift

Code Only Below

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
t = np.linspace(-0.2, 0.5, 500, endpoint=False)
t2 = np.linspace(-0.2, 0.5, 250, endpoint=False)
t3 = np.linspace(-0.4, 1.0, 999)
square = (signal.square(2 * np.pi * t - np.pi/2 , duty=0.2) + 1)/3
triangle = np.zeros(500)
triangle[250:500] = (signal.sawtooth(2 * np.pi * t2 - np.pi, 0 ) + 1)/2

txcorr = signal.correlate(square, triangle, mode = 'full', method='direct')
txcorr2 = np.fft.ifft(np.fft.fft(square, 999)*np.conj(np.fft.fft(triangle, 999)))

plt.plot(t, square)
plt.plot(t, triangle)
plt.plot(t3, txcorr)
plt.plot(t3, txcorr2)
plt.plot(t3, np.fft.fftshift(txcorr2), '.')
plt.plot(t3, txcorr)
| improve this answer | |
  • $\begingroup$ How come the peak for the correlated signal (in your third figure) peaks around 0.4? I recreated the same code you used for my example, except my square wave is aligned perfectly with the triangle, but the peak is not at the middle of the triangle slope $\endgroup$ – anony Mar 2 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.