Open In App

Spectrogram in MATLAB

Last Updated : 28 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A Spectrogram is a detailed, visual representation of how the frequency spectrum of a signal varies with time. They are highly useful in academic and research fields such as physics where signals of different kinds (electrical, broadband, etc.) are required to be studied visually. Spectrograms are very useful in analyzing the results of signal filtering.

MATLAB provides the spectrogram () function that plots the spectrograms for the required input signal. In this article, we shall see how to use the said function to plot spectrograms for different signal forms and various options available for the former.

Syntax:

spectrogram(<signal>)

This will plot the required spectrogram corresponding to the input signal.

Now we shall see the working of the same using some examples and dummy input signals. Firstly, we shall plot a simple spectrogram of a linear chirping function of MATLAB, which is a part of the signal processing toolbox, and see the output plot.

Example 1:

Matlab




% MATLAB codefor
% defining time
t = 0:0.001:2;        
  
% Generating a dummy signal
z= chirp(t,0,1,230);     
  
% Plotting the spectrogram
spectrogram(z)
  
% Adding title
title('Spectrogram of Linear chirp')


Output:

 

Here, a time vector of interval length 0.001 is passed to the linear chirp signal. The chirp function will start at a frequency of 0 Hz at time=0 and will pass the 230-hertz frequency at time=1. 

Now take another example, we will create a sinusoidal input wave and plot its spectrogram for 2300 samples.

Example 2:

Matlab




% MATLAB code for time vector
t = 0:2290;
  
% Phase angle of input signal
phase = pi/4;    
  
% Frequency of input signal
freq = 230;        
  
% Generating wave
z = sin(freq*t + phase);    
  
% Plotting spectrogram with title
spectrogram(z)
title('Spectrogram of a sinusoidal wave signal')


Output:

 

In the above code, we create a sinusoidal signal with frequency = 230Hz, initial phase angle = 45 degrees, and taking 2300 time instances.

Now, we shall plot a quadratic chirp as our last example. Here, we create a quadratic chirp input signal by passing a fifth parameter as ‘quadratic’ which makes the chirp function change its Fast-Fourier Transformation method to quadratic from linear. The initial frequency of the same is 220Hz and it will cross 230Hz at time = 1.

Example 3:

Matlab




t = 0:.0001:23;        
  
% Quadratic chirp
z= chirp(t,220,1,230,'quadratic');    
spectrogram(z)                        
  
% Plotting spectrogram
title('Spectrogram of Quadratic chirp')


Output:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads