Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 57

STFT for Time-Frequency Analysis

Recommended resources

Hello! Welcome back to the course.

In our last lesson, we delved into multirate systems, deriving the polyphase representation to create highly efficient decimation and interpolation filters. We focused on how to process signals efficiently when changing sampling rates. Today, we pivot from implementation efficiency to a new analytical challenge: how to understand signals whose very nature changes over time.

This brings us to our learning outcome: to analyze the Short-Time Fourier Transform (STFT) for time-frequency analysis of non-stationary signals. We'll explore why the standard Fourier Transform falls short for this task and how the STFT provides a powerful solution by generating a time-frequency representation of a signal.

1. The Limitation of the Fourier Transform

The Fourier Transform is a cornerstone of signal processing, telling us what frequencies are present in a signal. However, it does so by averaging over the signal's entire duration. This is perfect for stationary signals, where the statistical properties (like frequency content) do not change over time.

But what about signals like speech, music, or radar chirps? Their frequency content is constantly evolving. These are called non-stationary signals.

Example of a Non-Stationary Signal for STFT Analysis
This signal consists of four distinct frequencies (300Hz, 200Hz, 100Hz, 50Hz) played one after another. A standard Fourier Transform would show that all four frequencies are present, but it would completely obscure the order and timing of their appearance.

For such signals, knowing only what frequencies exist is not enough; we also need to know when they exist. The following video uses a great example—the tones generated by a telephone keypad (DTMF signals)—to illustrate this exact problem.

The Short Time Fourier Transform | Digital Signal Processing

To understand the motivation for the STFT, watch this segment from 'The Short Time Fourier Transform' by Free Engineering Lectures. It clearly demonstrates the shortcomings of analyzing a non-stationary signal in either the time domain or the frequency domain alone.

Watch from the beginning to 03:26. Focus on the core problem: The time-domain plot shows when events happen but not what frequencies are present. The frequency-domain plot (from a DFT of the whole signal) shows what frequencies are present but not when they happen.

As the video shows, we need a method that combines the strengths of both domains. This is precisely what the Short-Time Fourier Transform was designed to do.


2. The "Divide and Conquer" Strategy of STFT

The core idea of the STFT is elegantly simple: if a signal is non-stationary globally, we can assume it is "stationary enough" (or quasi-stationary) within a small, localized time window. The STFT formalizes this by breaking the long signal into a sequence of shorter, overlapping segments and then computing the Fourier transform for each segment.

This process generates a series of frequency spectra, each corresponding to a different point in time, allowing us to see how the frequency content evolves.

The following diagram illustrates this process.

Short-Time Fourier Transform (STFT) Process Illustration
This diagram shows how a signal is analyzed using the STFT. A window of a specific length is applied to a segment of the signal. The Fourier Transform (usually an FFT) of this segment is computed. The window is then shifted by a 'Hop Length' and the process is repeated, creating a time-dependent set of spectra.

This sliding window approach gives us two key parameters:

  • Window Length: The duration of the segment being analyzed at each step.
  • Hop Length (or Hop Size): The number of samples the window is shifted forward for the next analysis. The difference between the window length and hop length is the overlap.

3. Mathematical Formulation of the STFT

Let's formalize this process mathematically. The discrete-time STFT of a signal is defined as:

Let's break this down:

  • is our input signal.
  • is a window function (e.g., Hamming, Hann, or a simple rectangular window) of a finite length. The index represents the position of the window in time; as changes, the window slides along the signal. The product isolates a short segment of the signal.
  • The rest of the expression is simply the Discrete Fourier Transform (DFT) of this windowed segment, computed for frequency bins indexed by .

The result, , is a two-dimensional function representing the signal's frequency content (indexed by ) at a specific time (indexed by ).

For a rigorous and clear presentation of this formula, please refer to the following notes.

Short-Time Fourier Transforms

These lecture notes from Carnegie Mellon University provide a concise mathematical definition of the STFT.

Please read Section 3.2, 'Computing the short-time Fourier transform'. This section formally defines the STFT, first with a continuous frequency variable (like the DTFT) and then with a discrete frequency variable (like the DFT), which is what is used in practice. Note the notation X[n, k], explicitly showing the dependence on both time and frequency.

The Spectrogram

The STFT produces complex numbers, containing both magnitude and phase information. For visualization, we are often most interested in the magnitude. The spectrogram is a visual representation of the magnitude (or magnitude squared) of the STFT. It is typically plotted as an image with:

  • Time on the horizontal axis.
  • Frequency on the vertical axis.
  • Magnitude represented by color or intensity (e.g., bright colors for high energy, dark for low).

The next video segment brings this to life, applying the STFT to the DTMF signal and constructing its spectrogram.

The Short Time Fourier Transform | Digital Signal Processing

Now, let's watch the STFT in action. This part of the 'Free Engineering Lectures' video shows how the sliding window captures the changing frequencies of the DTMF signal and how these captures are assembled into a spectrogram.

Watch from 03:14 to 08:05. Observe how the DFT of each small chunk reveals different frequency peaks. Pay close attention to how the final spectrogram neatly displays both the time and frequency information on a single plot.


4. The Time-Frequency Resolution Trade-off

The STFT seems like the perfect solution, but it comes with a fundamental compromise, dictated by the choice of the window function's duration. This is known as the time-frequency resolution trade-off.

  • A long (wide) window captures many samples. This allows the FFT to distinguish between closely spaced frequencies, providing good frequency resolution. However, because the analysis is averaged over a long duration, any rapid changes within that window are blurred, leading to poor time resolution.

  • A short (narrow) window captures few samples. This pinpoints the timing of events accurately, providing good time resolution. However, with fewer samples, the FFT has a harder time separating close frequencies, resulting in poor frequency resolution.

This trade-off is not a flaw in the algorithm but an inherent property of signals, related to the Heisenberg Uncertainty Principle.

The following video provides an excellent discussion of this trade-off, using the terms "wideband" and "narrowband" spectrograms, and demonstrates it with both the DTMF and a speech signal.

The Short Time Fourier Transform | Digital Signal Processing

This final segment of the video explores the crucial impact of window size on the resulting spectrogram. It's essential for understanding how to properly apply and interpret the STFT.

Watch from 08:05 to 16:14. Focus on the difference between the spectrograms generated with short and long windows. Notice how for the speech signal, a short window is better for identifying sharp consonant sounds (good time resolution), while a long window is better for resolving the harmonic structure of vowels (good frequency resolution).

Test your understanding!

You are analyzing an audio signal containing a rapid sequence of very short drum hits (like a drum roll) followed by a sustained, low-frequency bass note.

  1. To clearly see the individual timing of each drum hit in the drum roll, would you choose a wide (long) or narrow (short) analysis window for your STFT? Why?
  2. To accurately measure the precise frequency of the sustained bass note, would you choose a wide or narrow window? Why?
Show answer
  1. For the drum roll, you would choose a narrow (short) window. The drum hits are events that are highly localized in time. A short window provides good time resolution, allowing you to distinguish one hit from the next.

  2. For the sustained bass note, you would choose a wide (long) window. Since the note's frequency is stable over a longer period, time resolution is less important. A long window provides good frequency resolution, which is necessary to get an accurate estimate of the note's fundamental frequency.

This illustrates the core trade-off: you cannot have optimal resolution in both time and frequency simultaneously with the STFT.


5. A Deeper Perspective: The Filter Bank Interpretation

Given your background in signal processing, you'll appreciate that the STFT can be interpreted in ways other than just a "sliding window FFT." The CMU notes you looked at earlier describe two other mathematically equivalent interpretations that connect the STFT directly to filtering concepts.

Short-Time Fourier Transforms

For a deeper insight into the STFT, let's explore its alternative formulations as described in the CMU lecture notes.

Please read Section 3.3, 'Alternate interpretations of the STFT operation' (from page 38 to 42). Pay close attention to Figures 3.3 and 3.4 and the accompanying equations. This section reformulates the STFT in two powerful ways.

Let's summarize the three interpretations:

  1. Fourier Transform Interpretation (Fig 3.2): This is the view we've used so far. The signal is multiplied by a sliding window , and the DFT of the product is taken.

  2. Lowpass Filter Interpretation (Fig 3.3): Here, the signal is first modulated by a complex exponential . This shifts the frequency content at down to baseband (DC). This frequency-shifted signal is then passed through a lowpass filter whose impulse response is the window function . The output of the filter at time gives you the STFT coefficient . You do this in parallel for each frequency .

  3. Bandpass Filter Interpretation (Fig 3.4): In this view, the STFT is implemented as a bank of bandpass filters. The original signal is fed into a parallel bank of filters. Each filter, with impulse response , is a bandpass filter centered at frequency . The output of each bandpass filter is then demodulated (shifted back to baseband) to produce the STFT coefficients.

This filter bank interpretation is particularly powerful. It shows that the STFT is fundamentally a way of analyzing a signal through a set of uniformly spaced bandpass filters, where the bandwidth of each filter is determined by the Fourier transform of the window function. The time-frequency trade-off can now be seen as a filter design problem: a narrow passband (good frequency resolution) requires a long impulse response (poor time resolution), and vice versa.


Conclusion

In this lesson, we have moved from one-dimensional analysis (time or frequency) to two-dimensional time-frequency analysis. We established why this is necessary for non-stationary signals and saw how the STFT accomplishes this.

Key Takeaways:

  • The standard Fourier Transform is unsuitable for non-stationary signals because it averages frequency information over all time, losing temporal detail.
  • The Short-Time Fourier Transform (STFT) provides a time-frequency representation by computing the DFT on a series of short, overlapping segments of a signal.
  • The spectrogram, a plot of the magnitude of the STFT, is a powerful tool for visualizing how a signal's frequency content changes over time.
  • The STFT is governed by a fundamental time-frequency resolution trade-off: a wide window gives good frequency resolution but poor time resolution, while a narrow window gives good time resolution but poor frequency resolution.
  • The STFT can be equivalently interpreted as a bank of uniformly spaced bandpass filters, providing a deep connection to classic filter theory.

Preview of the Next Lesson:

The time-frequency trade-off we introduced today is a manifestation of a deep physical and mathematical constraint known as the Uncertainty Principle. In our next lesson, we will apply the uncertainty principle to explain the time-frequency resolution trade-off in the STFT more formally. We will quantify the relationship between a window's duration and its bandwidth and see how different window shapes (like Gaussian, Hamming, etc.) perform in this context.