# Apply FFT to positive spiky signal

I have a signal that looks like this

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)


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))
fig.suptitle("FFT")
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")
ax.legend();


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

• 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? Jun 10 at 16:08
• Good catch. I did 1/50*2*pi, which is approx 0.125.
– Idr
Jun 10 at 16:16
• It's totally OK to post self-answers, like you did! Jun 10 at 17:35