I'm performing a STFT of a sine wave in which the frequency increases (up-chirp signal) using python numpy.fft as shown in the following code:
import numpy as np import matplotlib.pyplot as plt def stft(x, L, overlap, window): opoints = int(L*overlap) X = np.array([np.abs(np.fft.fft(window*x[i:i+L])[:L//2]) for i in range(0,len(x)-L,opoints)]) return X t = np.linspace(0, 5, 11025*5) xf = np.linspace(0, 2000, 11025*5) w = 2*np.pi*xf x = np.sin(w*t) f = np.linspace(0, 11025, 1024) window = np.hanning(1024) stft = stft(x,1024,0.5,window) plt.imshow(stft[:,:1024//2].T, cmap = plt.get_cmap('gist_yarg'), origin='lower', aspect='auto', interpolation='nearest', extent=(0,5, 0,11025/2)) plt.xlabel("Time [s]") plt.ylabel("Frequency [Hz]") plt.show()
It works for a constant frequency sinewave. However it shows not an offset but a gain error if using the up-chirp signal as input.
- Hanning window
- Overlap = 0.5
- Window size 1024
- Sampling frequency 11025Hz
- Total sample time: 5 seconds
What is causing the gain error that leads the 2 kHz signal appearing at double frequency at the end of the time window?
Thank you in advance, Pedro