Search code examples
pythonscipy

Using scipy.signal.stft() vs scipy.signal.ShortTimeFFT.stft()


I'm trying to do a short time fourier transform on this signal and have been trying to perform it using the ShortTimeFFT() method because the scipy documentation states that the signal.stft() method is legacy. However, everywhere I search online, (and AI) says just use signal.stft(). I haven't been able to find any guidance outside the documentation for using the ShortTimeFFT.stft() method.

Should I be trying to make the switch? or just use signal.stft(), I've been having a lot of trouble understanding the ShortTimeFFT function and getting it to behave the way I want (see my struggles here). So I'm considering just using signal.stft()


Solution

  • You can use stft() or ShortTimeFFT(), the latter has more features, and stft() is slightly easier to use:

    import numpy as np
    from scipy.signal import stft
    import matplotlib.pyplot as plt
    
    fs = 1024
    t = np.arange(0, 1.0, 1.0 / fs)
    x = np.sin(2 * np.pi * 100 * t) + np.sin(2 * np.pi * 200 * t)
    
    f, t, Zxx = stft(x, fs=fs, nperseg=256)
    
    plt.pcolormesh(t, f, np.abs(Zxx), shading='gouraud')
    plt.ylabel('Hz')
    plt.xlabel('Sec')
    plt.show()
    
    
    • Both methods are fundamentally the same transform.

    • Both methods are being used commonly.

    • stft() is easy to use for quick computations. You can simply switch to ShortTimeFFT() with no cost, if you want, at any time.

    • If you are intensely applying STFT to some complex signals for highly expensive tasks, which I doubt it, then you would consider using ShortTimeFFT().

    • Also see.