Creating and Analyzing Spectrograms in MATLAB

Understanding Spectrograms in MATLAB A spectrograph visualizes how the frequency spectrum of a signal changes over time. MATLAB's spcetrogram function implements the Short-Time Fourier Transform (STFT) to create these time-frequency representations.

Basic Implementation Let's begin with a practical example using a chirp signal:

% Define sampling frequency
samplingRate = 1000;

% Create time vector
timeVector = 0:1/samplingRate:2-1/samplingRate;

% Generate quadratic chirp signal
inputSignal = chirp(timeVector,100,1,200,'quadratic');

% Compute and display spectrogram
spectrogram(inputSignal,100,80,100,samplingRate,'yaxis')

% Adjust viewing angle
view(-77,72)

% Apply smooth shading
shading interp

% Hide colorbar
colorbar off

Extracting and Visualizing Spectrogram Data To analyze the spectrogram data programmatically, we can capture the outputs:

% Get spectrogram components: time, frequency, amplitude, power
[amplitudeMatrix,frequencyVector,timePoints,powerDensity] = spectrogram(inputSignal,100,80,100,samplingRate);

% Find maximum power across frequencies
[maxPower,indices] = max(10*log10(powerDensity));

% Plot the maximum power trajectory in 3D
hold on
plot3(timePoints,frequencyVector(indices),maxPower,'r','linewidth',4)
hold off

% Add colorbar and set 2D view
colorbar
view(2)

Interpreting Output Variables The spectrogram function returns four key components:

frequencyVector: A 51×1 matrix containing frequency values amplitudeMatrix: A 51×96 complex matrix representing amplitude powerDensity: A 51×96 matrix of power spectral density timePoints: A 1×96 matrix of time instants

Visualizing Specific Frequency Components To examine a particular frequency band, we can extract and plot its time evolution:

% For a specific frequency (e.g., 200 Hz)
targetFrequency = 200;
freqIndex = 21; % Corresponds to 200 Hz in this example

% Plot time-domain representation of the specific frequency
plot(timePoints,abs(amplitudeMatrix(freqIndex,:)))

% Alternatively, plot the frequency spectrum at a specific time point
% plot(frequencyVector, abs(amplitudeMatrix(:,50))) % 50th time point

Spectrogram Function Syntax The spectrogram function has two primary syntax forms:

[S,F,T,P] = spectrogram(signal,window,noverlap,nfft,fs)
[S,F,T,P] = spectrogram(signal,window,noverlap,F,fs)

When called without output arguments, the function automatically displays the spectrogram. With output arguments, it returns the STFT components which can be used for custom visualization.

Parameter Details

Input Parameters

signal: Input signal vector. By default, the signal is divided into 8 segments for processing. If the signal length isn't divisible by 8, it will be truncated. window: Window function applied to each segment. Defaults to a Hamming window of length nfft. Can be specified as an integer (number of segments) or a vector (custom window). noverlap: Number of overlapping samples between segments. Defaults to 50% overlap. nfft: Number of points for FFT computation. Defaults to the maximum of 256 and the smallest power of 2 greater than the segment length. fs: Sampling frequency in Hz. Defaults to normalized frequency (1 Hz).

Output Parameters

S: Short-time Fourier transform of the input signal. Each column represents the frequency components at a specific time segment. Time increases along columns, frequency along rows. F: Frequency vector. When using a frequency vector as input, the Goertzel algorithm is used to compute the spectrum at specified frequencies. T: Time instants corresponding to the center of each segment. P: Power spectral density (PSD). For real signals, P contains the single-sided PSD estimates. For complex signals with specified frequencies, P contains the two-sided PSD.

Window Function Considerations The window parameter determines how the signal is segmented and processed:

When window is an integer, the signal is divided into window segments, each with a Hamming window applied. When window is a vector, the signal is divided into length(window) segments, each using the specified window. To replicate the functionality of the older specgram function, specify a Hann window of length 256.

Overlap and FFT Length

noverlap must be less than window length or length(window). This parameter controls the overlap between adjacent segments. nfft determines the frequency resolution of the spectrogram. Larger values provide better frequency resolution but reduced time resolution.

Power Spectral Density Calculation The power density matrix is calculated as:

P(i,j) = k|S(i,j)|²

where k is a scaling factor that depends on the window function and sampling rate. For single-sided PSD, special scaling is applied at 0 Hz and the Nyquist frequency.

Tags: MATLAB signal-processing spectral-analysis STFT time-frequency-analysis

Posted on Sat, 31 Oct 2026 16:20:54 +0000 by rhasce