close
close
stft python

stft python

3 min read 20-10-2024
stft python

Demystifying the Short-Time Fourier Transform (STFT) in Python: A Practical Guide

The Short-Time Fourier Transform (STFT) is a powerful tool for analyzing non-stationary signals, those whose frequency content changes over time. It's widely used in audio processing, speech recognition, and even medical signal analysis. This article aims to demystify the STFT in Python, providing a clear understanding of its principles and practical applications.

What is the STFT?

Imagine a musical piece. Its melody and harmony change over time, making it a non-stationary signal. The STFT, unlike the traditional Fourier Transform (FT) which analyzes the entire signal at once, slices the signal into short segments, called "frames." Each frame is then analyzed using the FT, providing a spectrum representing the dominant frequencies within that specific time window.

This time-frequency representation gives us a snapshot of the frequency content at different points in time.

How does it work in Python?

Let's delve into the code. Python's scipy.signal library provides the stft function, offering a concise way to perform the STFT. Below is a basic implementation:

import numpy as np
from scipy.signal import stft

# Define a time series signal
signal = np.sin(2 * np.pi * 10 * np.arange(1000) / 100) + np.sin(2 * np.pi * 20 * np.arange(1000) / 100)

# Perform STFT
f, t, Zxx = stft(signal, fs=100, nperseg=256)

# Plot the spectrogram
import matplotlib.pyplot as plt
plt.pcolormesh(t, f, np.abs(Zxx), shading='gouraud')
plt.title('STFT Magnitude')
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()

Explanation:

  • We first define a simple signal containing two sine waves with different frequencies.
  • The stft function takes the signal, sampling frequency (fs), and frame size (nperseg) as inputs.
  • It returns frequencies (f), time stamps (t), and the STFT matrix (Zxx).
  • The Zxx matrix contains the complex-valued frequency content of each frame. Its magnitude represents the power of each frequency at that time instance.
  • Finally, we plot the magnitude of Zxx using matplotlib to visualize the spectrogram.

Key parameters:

  • fs: Sampling frequency of the signal.
  • nperseg: Length of each frame in samples.
  • noverlap: Number of samples to overlap between consecutive frames (defaults to half the nperseg).

Understanding the Spectrogram:

The spectrogram is the visual representation of the STFT output. The x-axis represents time, the y-axis represents frequency, and the color intensity represents the magnitude of the frequency at that time. In our example, you should observe two horizontal lines corresponding to the two frequencies present in the signal.

Applications of STFT:

  1. Audio Processing: STFT is crucial in audio effects like pitch shifting, time stretching, and noise reduction. It allows for targeted modifications of specific frequencies within a sound.

  2. Speech Recognition: Speech recognition systems rely on the STFT to extract the spectral features of speech signals, which are then used for identifying different phonemes.

  3. Medical Signal Analysis: STFT helps analyze EEG signals for sleep stage classification, ECG signals for heart rhythm analysis, and other biomedical signals for diagnosis and monitoring.

Additional Notes:

  • The nperseg and noverlap parameters influence the resolution and accuracy of the STFT. Choosing the right values depends on the specific application and the nature of the signal.

  • The STFT offers a powerful tool to analyze non-stationary signals in various domains. Its versatility makes it an essential technique in signal processing.

Conclusion:

This article provides a basic understanding of the STFT and its implementation in Python. Further exploration of the scipy.signal.stft documentation and experimenting with different signal examples will deepen your understanding and help you apply it to diverse signal analysis tasks.

Please note: This article builds upon the following resources:

By combining this knowledge with practical application, you can master the STFT and unlock a world of signal processing opportunities.

Related Posts