# Impact of STFT window function and FFT length on computation time

I have been doing a study which part of it includes a comparison of computation time vs window type and length (among some other things in the computation time, however I speak in terms of relative computation time).

I found that among three windows I tested: Hann, Hamming and Blackman; the Blackman window had the slowest computation time (albeit only by ~20ms) with the Hann window outperforming the rest. This was tested over 100 runs for each.

My question is, why is this the case? Is there something specific about the windowing function (as well as FFT length of course) that causes this increase in computation time? Are there any sources for justification that explores this in more depth?

For reference, my input signal samples are 2s long with sampling rate of 2MHz, so a total length of 4,000,000 samples. The code I am using is as follows:

from datetime import datetime
from scipy import signal

for window in ['hann', 'hamming', 'blackman']: #'hamming', 'hann',
for nperseg in [1024, 2048, 4096, 8192, 16384]: # 1024, 2048, 4096, 8192
win = signal.windows.get_window(window, nperseg)
et = 0
for i in range(0, 100):
start_time = datetime.now() # time.monotonic()
f, t, Zxx = signal.stft(x, fs=fs, window=win, nperseg=nperseg)
end_time = datetime.now() # time.monotonic()
et += (end_time - start_time).total_seconds() * 1000 # convert to ms
print(f'Time Taken (window={window},nperseg={nperseg}): {et / 100}ms')

• As long as you precompute the window, there should be no difference. May 18, 2022 at 14:28
• Could you elaborate on precomputing the window? What I am doing is given a stream of input samples at 2MHz sampling rate, I am computing the STFT for individual 2s samples and measuring the time taken to do the computation (after reading of course) and then averaging over 100 runs. May 18, 2022 at 14:33
• @rshah Before computing the STFT, assign the window to a variable and use that variable to scale your data - don't call the functions that create the window (e.g., hamming) over and over again.
– Ash
May 18, 2022 at 14:39
• @Ash Thank you. However, pre-computing the window still results in these differences in computation time between window functions regardless of the FFT length. May 18, 2022 at 14:59
• @rshah, What is the standard deviation of your measurements?
– Ash
May 18, 2022 at 15:23

• Thanks for elaborating! I would have thought that in some cases longer window lengths would not necessarily result in small differences, but I may be wrong. In either case, perhaps this is now an issue of how I measure the time (i.e. using datetime.now()) so perhaps there is another preferred method. May 18, 2022 at 16:24