
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - Short-Time Fourier Transform (STFT)
Short-Time Fourier Transform in SciPy
The Short-Time Fourier Transform (STFT) in SciPy is a tool to analyze signals in both the time and frequency domains. It works by dividing a signal into small, overlapping segments by using a sliding window and then performing a Fourier Transform on each segment.
The result is a time-frequency representation of the signal by showing how its frequency content changes over time. Mathematically, the STFT of a signal x(t) is defined as follows −
X(t, f) = - x() w( - t) e-j 2 f d
Where −
- x() is the signal
- w(t) is the window function centered at time
- ej2f represents the Fourier Transform.
- and are the time and frequency variables respectively.
The window function ensures that only a small segment of the signal around contributes to the Fourier Transform at that point.
Steps of STFT
Following are the steps to perform the Short Time Fourier Transform in SciPy −
- Windowing: This divides the signal into overlapping segments using a window function. The window function is typically chosen to minimize spectral leakage e.g., Hann, Hamming, Blackman.
- Fourier Transform: Then apply the Fourier Transform to each windowed segment.
- Time-Frequency Representation: Finally combine the results for all segments to form a 2D representation with time and frequency axes.
Short-Time Fourier Transform (STFT) in SciPy
In SciPy we have the function scipy.signal.stft() to perform the Short-Time Fourier Transform which provides flexibility in parameters such as the window type, segment length, overlap and FFT size.
Syntax
Following is the syntax of scipy.signal.stft() function which is used to perform Short-Time Fourier Transform −
scipy.signal.stft(x,fs=1.0,window='hann',nperseg=256,noverlap=None,nfft=None,detrend=False,return_onesided=True,boundary='zeros',padded=True,axis=-1,scaling='spectrum')
Parameters
Here are the parameters of the function scipy.signal.stft() −
- x(array_like): Input signal. The data to be transformed.
- fs(float, optional): Sampling frequency of the input signal. Default is 1.0.
- window(str or tuple or array_like, optional): Desired window function to apply to each segment. Defaults to 'hann'.
- nperseg(int, optional): Length of each segment for the STFT. Default is 256.
- noverlap(int, optional): Number of points to overlap between segments. If not specified, it defaults to half of nperseg.
- nfft(int, optional): Number of points for the FFT computation. If not provided, defaults to nperseg.
- detrend(str or function or bool, optional): Specifies how to detrend each segment. Default is False (no detrending).
- return_onesided(bool, optional): If True, returns a one-sided spectrum for real signals. Default is True.
- boundary(str or None, optional): Specifies how to handle the signal boundaries. Default is 'zeros'.
- padded(bool, optional): If True, pads each segment to the nearest power of two. Default is True.
- axis(int, optional): Axis along which the STFT is computed. Default is -1 (last axis).
- scaling(str, optional): Determines the scaling of the STFT output. Options are 'spectrum' (default) and 'density'.
Basic Example
Following is the example which computes and plots the STFT of a signal composed of two sine waves by using the function scipy.signal.stft() with default parameters −
import numpy as np from scipy.signal import stft import matplotlib.pyplot as plt # Create a simple signal fs = 1000 # Sampling frequency t = np.linspace(0, 2, 2 * fs, endpoint=False) # 2-second time vector x = np.sin(2 * np.pi * 50 * t) + np.sin(2 * np.pi * 120 * t) # Two sine waves # Compute the STFT f, t, Zxx = stft(x, fs=fs, window='hann', nperseg=256, noverlap=128) # Plot the STFT Magnitude plt.figure(figsize=(10, 6)) plt.pcolormesh(t, f, np.abs(Zxx), shading='gouraud') plt.title('STFT Magnitude') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.colorbar(label='Magnitude') plt.show()
Below is the output of the STFT basic example −

Adjusting Overlap
This example shows how to change the overlap between segments to 75% resulting in smoother time-frequency resolution −
import numpy as np from scipy.signal import stft import matplotlib.pyplot as plt # Create a simple signal fs = 1000 # Sampling frequency t = np.linspace(0, 2, 2 * fs, endpoint=False) # 2-second time vector x = np.sin(2 * np.pi * 50 * t) + np.sin(2 * np.pi * 120 * t) # Two sine waves # Compute the STFT f, t, Zxx = stft(x, fs=fs, window='hann', nperseg=256, noverlap=128) # Plot the STFT Magnitude plt.figure(figsize=(10, 6)) plt.pcolormesh(t, f, np.abs(Zxx), shading='gouraud') plt.title('STFT Magnitude') plt.ylabel('Frequency [Hz]') plt.xlabel('Time [sec]') plt.colorbar(label='Magnitude') plt.show()
Below is the output of the STFT which adjusts the overlap using the function scipy.signal.stft() −

Applications of STFT
Here are the applications of SciPy Short Time Fourier Transform −
- Speech and Audio Processing: Analyze time-varying frequency content of speech signals.
- Music Analysis: Identify harmonic components and beats in music.
- Biomedical Signals: Analyze non-stationary signals like EEG or ECG.
- Vibration Analysis: Detect faults in rotating machinery.
- Communications: Demodulate time-varying frequency signals.