- Introduction
The Short-Time Fourier Transform (STFT) is a signal processing technique that provides time-frequency representation of a signal. It allows us to analyze how the frequency content of a signal changes over time. The result of the STFT is often visualized using a spectrogram, which is a 2D plot with time on the x-axis, frequency on the y-axis, and the magnitude or power of the frequency components represented by color or intensity.
Mathematically, a signal can be represented as a function of time, denoted as x(t). The Fourier Transform (FT) of the signal, denoted as X(f), provides the frequency content of the signal but loses the time information. The STFT addresses this limitation by dividing the signal into short segments and applying the FT to each segment, thereby capturing both time and frequency information.
- STFT Computation
The STFT is computed by dividing the signal into short segments (windows) and applying the Fourier Transform to each segment. The window is moved along the signal with a certain overlap, and the Fourier Transform is computed for each position. The resulting STFT coefficients represent the frequency content of the signal at different time instances.
Mathematically, the STFT of a signal x(t) is defined as:
$STFT[x(t)](τ,f) = ∫[x(t)w(t-τ)e^{-j2πft}]dt$
where w(t) is the window function, τ is the time shift, and f is the frequency. The window function is typically a smooth function that tapers to zero at the edges, such as a Hann or Gaussian window, to reduce spectral leakage.
In the provided code, the stft
function from the scipy.signal
module is used to compute the STFT. The function takes the signal, sampling frequency (fs
), and other optional parameters as input and returns the STFT coefficients along with the corresponding frequency and time arrays.
- Spectrogram Visualization
The spectrogram is a visual representation of the STFT coefficients. It shows how the frequency components of the signal evolve over time. In the spectrogram, the x-axis represents time, the y-axis represents frequency, and the color or intensity at each point indicates the magnitude or power of the corresponding frequency component at that time instant.
Mathematically, the spectrogram can be defined as the squared magnitude of the STFT coefficients:
spectrogram(t,f) = |STFT{x(t)}(τ,f)|^2
In the provided code, the pcolormesh
function from Matplotlib is used to plot the spectrogram. The STFT coefficients are converted to a logarithmic scale using 20 * np.log10(np.abs(Zxx))
to improve visibility of the frequency components. The logarithmic scale is used because the dynamic range of the STFT coefficients can be large, and the logarithmic scale helps to compress the range and enhance the visualization of weaker frequency components.
- Applications of STFT and Spectrograms
STFT and spectrograms have various applications in signal processing and analysis. Some common applications include:
- Speech analysis: Spectrograms are widely used in speech processing to visualize and analyze the time-frequency characteristics of speech signals. They can help in tasks such as speech recognition, speaker identification, and emotion detection. The spectrogram reveals the formants (resonant frequencies) of speech, which carry important information about the phonemes and the speaker's characteristics.
- Audio processing: Spectrograms are used in audio analysis and processing to identify and extract specific frequency components, detect audio events, and perform audio compression and enhancement. For example, spectrograms can be used to identify and remove unwanted noise or to enhance specific frequency ranges in audio signals.
- Vibration analysis: In mechanical engineering, spectrograms are used to analyze vibration signals from machines and structures. They can help in fault diagnosis, condition monitoring, and predictive maintenance. By examining the spectrogram, engineers can identify abnormal frequency components or patterns that indicate potential faults or degradation in the machinery.
- Radar and sonar: Spectrograms are used in radar and sonar systems to analyze the time-frequency characteristics of the received signals. They can help in target detection, classification, and tracking. The micro-Doppler signatures of targets can be observed in the spectrogram, providing information about the target's motion and characteristics.
- Micro-Doppler and Spectrograms
Micro-Doppler is a phenomenon observed in radar systems when there are small, fast-moving parts or vibrations in the target (e.g. birds flapping its wings, drone propeller rotating). These micro-motions induce additional frequency modulations on the main Doppler frequency shift of the target. Spectrograms are commonly used to analyze and visualize micro-Doppler signatures.
Mathematically, the micro-Doppler effect can be modeled as a time-varying frequency modulation on the received radar signal. The micro-Doppler frequency shift, denoted as f_μD(t), is given by:
$f_μD(t) = (2v_μ(t)/λ)cos(θ)$
where $v_μ(t)$ is the velocity of the micro-motion, λ is the wavelength of the radar signal, and θ is the angle between the micro-motion direction and the radar line of sight.