9951 explained code solutions for 126 technologies


python-scipyHow can I use Python and SciPy to perform a Short-Time Fourier Transform?


The Short-Time Fourier Transform (STFT) is a powerful tool for analyzing non-stationary signals. It can be used to analyze the frequency components of a signal over short intervals of time. Python and SciPy provide a number of functions that can be used to perform a STFT.

The following example code uses SciPy's stft function to perform a STFT on a signal:

import numpy as np
from scipy.signal import stft

# Create a 1-dimensional signal
x = np.linspace(0, 10, 1000)

# Perform STFT
freqs, times, Sx = stft(x, fs=1.0, window='hann', nperseg=128, noverlap=None, nfft=None, detrend='constant', return_onesided=True, boundary='zeros', padded=True, axis=-1)

The output of this code is a 3-dimensional array containing the frequencies, times, and STFT values of the signal.

The following list explains the parts of the code:

  • stft: This is the SciPy function used to perform the STFT.
  • x: This is the 1-dimensional signal that is being analyzed.
  • fs: This is the sampling frequency of the signal.
  • window: This is the window function used for the STFT.
  • nperseg: This is the length of each segment used for the STFT.
  • noverlap: This is the number of samples that overlap between segments.
  • nfft: This is the number of points used in the FFT computation.
  • detrend: This is the type of detrending used in the STFT.
  • return_onesided: This determines whether the STFT is one-sided or two-sided.
  • boundary: This is the type of padding used at the edges of the signal.
  • padded: This determines whether the signal is padded or not.
  • axis: This is the axis used for the FFT computation.

Helpful links

Edit this code on GitHub