0
$\begingroup$

I'm trying to test some intuition using the matplotlib specgram routine. I first generate a simple sine wave

import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import numpy as np

t = linspace(0,100,1000)
omega = 2*np.pi*1
y = np.sin(omega*t)
plt.plot(t,y)
plt.show()

This generates the expected image of a sine wavesine wave

I can also look at the mod squared of the FFT of this signal:

the_fft = np.fft(y)
spacing = t[1] - t[0]
ps = the_fft * np.conj(the_fft)
freqs = np.fft.fftfreq(len(t),spacing)
idx = np.argsort(freqs)
plt.plot(freqs[idx],ps[idx])
plt.show() 

which gives me the square of the Fourier transform, which has a peak at 1 as expected (since my frequency is 1). fft

I then try to compute the specgram, as

_ = plt.specgram(the_fft,NFFT=100,noverlap=0,Fs=1./spacing,window=mlab.window_none) plt.show()

which then gives me this specgram.

From this it's not easy to tell that the only frequency present in the signal is 1. What needs to be done in the specgram call to see this? Of course this is probably the expected output of this, so I'm really also asking a basic DSP question at the same time.

$\endgroup$
0
$\begingroup$

You are computing the spectrogram of the DFT as if it were a time signal. Try this instead

import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(0,100,1000)
omega = 2*np.pi*1
y = np.sin(omega*t)

spacing = t[1] - t[0]
plt.specgram(y,NFFT=100,noverlap=0,Fs=1./spacing,window=mlab.window_none)
plt.show()
| improve this answer | |
$\endgroup$
  • $\begingroup$ Indeed. I simply forgot that it's the signal itself that goes in there instead of its FFT. Thanks for fixing my silly mistake! $\endgroup$ – gammapoint Jul 3 '15 at 23:30
  • $\begingroup$ FFT is only the name of the algorithm, DFT (Discrete Fourier Transform) is the correct term for the vector/quantity $\endgroup$ – gsmafra Jul 3 '15 at 23:31

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.