I have a signal that looks like this

original signal

It has regularly occurring peaks 50 units apart, and offset from 0 by approximately 20. I thought I could apply the DTFT to this signal and extract the period (expected value 50) and the phase offset (expected value 20). So in graphical terms, I thought I could find the sinusoid in orange, which is created with the following code:

period = 50
offset = 20
x = np.arange(len(sig))
freq = 1/period * 2 * np.pi
y = 40000 * np.sin(x*freq-offset)

signal with sinusoid

So I apply the FFT using scipy as follows:

from scipy.fft import fft, fftfreq
yf = fft(sig)
xf = fftfreq(len(sig))

I then plot it with the expectation that the frequency with the maximum magnitude value would be close to the one I found by eyeballing. But that is not the case, and there are several frequency values which have higher magnitudes than the one I was expecting (.125).

fig = plt.figure(figsize=(14, 5))
ax = fig.add_subplot()
ax.plot(xf[s], np.abs(yf[s]))
ax.vlines(freq, 0, 1, transform=ax.get_xaxis_transform(), colors='r', label="expected max freq value")


Can I use the FFT on this signal to find both the phase offset (20), and the period (50)? From the graph it seems obvious that the signal is dominated by that one sinusoid, but perhaps my intuition is off because this is like a rectified signal.

The full jupyter notebook is available here

  • 2
    $\begingroup$ if your period is 50 samples, then your frequency is 1/50=0.02, not ca 0.12. Not quite sure how you come to your expected frequency? $\endgroup$ – Marcus Müller Jun 10 at 16:08
  • $\begingroup$ Good catch. I did 1/50*2*pi, which is approx 0.125. $\endgroup$ – Idr Jun 10 at 16:16
  • 1
    $\begingroup$ It's totally OK to post self-answers, like you did! $\endgroup$ – Marcus Müller Jun 10 at 17:35

When calculating the frequency correctly as the inverse of the period, instead of the inverse of the period times 2 pi, the fft does indeed give the expected answer.

enter image description here


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.