Spectrogram in Python

Spectrogram

A spectrogram is a wave-like graph which is used to represent measures like loudness, frequencies, and other signals that change over time. With the help of a spectrogram, these signals and measures are visually more understandable. A spectrogram is a two-dimensional graph in which the time component is represented mostly on the x-axis.

In Python, plotting a spectrogram is a part of data visualisation and can be done using various libraries.

In this tutorial, we will see how to plot a spectrogram in Python using different dictionaries.

Plotting Spectrogram using the matplotlib.pyplot.specgram function of the Matplotlib Library.

The Matplotlib library is a very good library when it comes to data visualisation in python. With the help of this library, many types of graphs and visual representations of the data can be made in Python.

In this method, the matplotlib.pyplot.specgram function is used to plot the spectrogram. This function has various parameters. Some of them are:

  • x (1-D array) – This parameter defines the data of which the spectrogram has to be made.
  • Fs (float) – This parameter helps in calculating the fourier frequencies, i.e, cycles of the samples per unit time. The default value of this parameter is 2.
  • sides – This parameter defines which sides of the whole spectrum are to be returned. The default is one-sided for real data and two-sided for complex data. onesided forces that the spectrum returned is one-sided and twosided forces two-sided spectrum.
  • noverlap (int) – This parameter defines the number of points of overlap between the blocks in the spectrum. The default value of this parameter is 128.

There are many other parameters in this function such as pad_to, NFFT, detrend, etc. Also, note that all the parameters except the x (1-D array) parameter are not necessary to define, i.e, they are optional.

Example:

Output:

  • The program above creates a spectrogram for the function A=30tan(5*np.pi*t).
  • Note that in the program above, the linspace() function of the NumPy is used. The linspace() function helps in creating a random numerical sequences that are evenly spaced.

Plotting Spectrogram using the scipy.signal.spectrogram function of the SciPy Library

Python’s SciPy library is a module that is used for tasks like linear algebra, integration, image processing, and many more. It is an open source library that helps in performing both scientific and technical computing. The SciPy library is often used along with the NumPy library.

In this method, the scipy.signal.spectrogram function is used to plot a spectrogram. This function has various parameters. Some of them are:

  • x (1-D array) – This parameter defines the data of which the spectrogram has to be made.
  • Fs (float) – This parameter helps in calculating the fourier frequencies, i.e, cycles pf the samples per unit time. The default value of this parameter is 1.
  • nperseg (int) – This parameter is used to define the length of each segment. The default value of this parameter is None.
  • noverlap (int) – This parameter defines the number of points of overlap between the blocks in the spectrum. The default value of this parameter is None.
  • axis (int) – This parameter defines the axis along which the spectrogram is computed. The default value of this parameter is over the last axis, i.e, axis=-1.

There are many other parameters in this function such as nfft, scaling, detrend, etc. Also, note that all the parameters except the x (1-D array) parameter are not necessary to define, i.e, they are optional.

Example:

Output:

  • The program creates a spectrogram for the function A=5cos(500*np.pi*t).
  • Note that here also the linspace() function of the NumPy is used.
  • Here, the value of the fs parameter is set to 1 which will represent the sample frequency.
  • And the nfft is set to 510 which represents the length of the FFT being used.

That’s all about Spectogram in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *