I'm attempting to optimize some code I was given wherein the FFT is taken of a sliding window over a time series (given as a list) and each result is accumulated into a list. The original code is as follows:
def calc_old(raw_data):
FFT_old = list()
for i in range(0, len(raw_data), bf.WINDOW_STRIDE_LEN):
if (i + bf.WINDOW_LEN) >= len(raw_data):
# Skip the windows that would extend beyond the end of the data
continue
data_tmp = raw_data[i:i+bf.WINDOW_LEN]
data_tmp -= np.mean(data_tmp)
data_tmp = np.multiply(data_tmp, np.hanning(len(data_tmp)))
fft_data_tmp = np.fft.fft(data_tmp, n=ZERO_PAD_LEN)
fft_data_tmp = abs(fft_data_tmp[:int(len(fft_data_tmp)/2)])**2
FFT_old.append(fft_data_tmp)
And the new code:
def calc_new(raw_data):
data = np.array(raw_data) # Required as the data is being handed in as a list
f, t, FFT_new = spectrogram(data,
fs=60.0,
window="hann",
nperseg=bf.WINDOW_LEN,
noverlap=bf.WINDOW_OVERLAP,
nfft=bf.ZERO_PAD_LEN,
scaling='spectrum')
In summary, the old code windows the time series, removes the mean, applies a Hann windowing function, takes the FFT (while zero-padding, as ZERO_PAD_LEN>WINDOW_LEN
), and then takes the absolute value of the real half and squares it to produce a power spectrum (Units of V**2). It then shifts the window by the WINDOW_STRIDE_LEN
, and repeats the process until the window would extend beyond the end of the data. This has an overlap of WINDOW_OVERLAP
.
The spectrogram, so far as I can tell, should do the same thing with the arguments I have given. However, the resulting dimensions of the FFT's differ by 1 for each axis (e.g. old code is MxN, new code is (M+1)x(N+1)) and the value in each frequency bin is massively different -- several orders of magnitude, in some cases.
What am I missing here?