HomeLinuxMatplotlib Spectrogram

Matplotlib Spectrogram


A “Spectrogram” reveals the frequency spectrum of a sign over the course of time. “Spectrograms” are ceaselessly utilized in sign processing, particularly in audio and speech processing. The “Matplotlib”, a scientific computing library for Python offers the “matplotlib.pyplot.specgram()” perform to create a spectrogram.

This information describes how to attract/plot a “Spectrogram” in Python using “Matplotlib”.

This tutorial presents the next matters:

What’s a Spectrogram in Python?

The “Spectrograms” illustrate the frequency spectrum of a sign because it varies over time. It’s such that point is represented by the “x-axis” and frequency by the “y-axis“. The colour depth at every level within the plot signifies the magnitude of the frequency spectrum at that cut-off date.

Methods to Draw/Create a Matplotlib Spectrogram in Python?

For drawing a spectrogram, the “specgram()” perform of the “pyplot” module is utilized in Python.

Instance

This code is utilized to attract a spectrogram in Python:

import matplotlib.pyplot as plt
import numpy
t = numpy.linspace(0, 20, 200*20)
x = numpy.sin(2*numpy.pi*10*t) + numpy.sin(2*numpy.pi*20*t)
# Plot the spectrogram
plt.specgram(x, Fs=200)
plt.xlabel(‘Time (s)’)
plt.ylabel(‘Frequency (Hz)’)
plt.colorbar(label=‘Depth (dB)’)
plt.present()

 

Within the above code:

  • The “matplotlib” and “numpy” libraries are imported, respectively.
  • The “linspace()” perform returns an array of “4000” equally spaced values between “0” and “20”, and shops them in “t”. This represents the time axis of the desired sign.
  • The “sin()” perform is used to create an array of values by including two sinusoidal capabilities with the frequencies of “10 Hz” and “20 Hz”, respectively. This represents the sign to be analyzed.
  • The “specgram()” perform plots the spectrogram of the sign “x”, utilizing a sampling frequency of “200 Hz”. The spectrogram reveals the depth of various frequency parts as they range with time, utilizing colours to point the magnitude in decibels (dB).

Output

The spectrogram has been displayed within the above output.

Methods to Customise the Matplotlib Spectrogram in Python?

“Matplotlib” gives many customization choices to create visually interesting spectrograms. Following is an instance:

Instance: Altering the Shade Map of the Spectrogram

The below-provided code is used to alter the colour map of the spectrogram:

import matplotlib.pyplot as plt
import numpy
t = numpy.linspace(0, 20, 200*20)
x = numpy.sin(2*numpy.pi*10*t) + numpy.sin(2*numpy.pi*20*t)
# Plot the spectrogram
plt.specgram(x, Fs=200, cmap=‘plasma’)
plt.xlabel(‘Time (s)’)
plt.ylabel(‘Frequency (Hz)’)
plt.colorbar(label=‘Depth (dB)’)
plt.present()

 

Within the above code snippet, the “cmap” parameter of the “plt.specgram()” perform is used to alter the colour map of the spectrogram.

Output

Within the above output, the colour map of the created spectrogram has been custom-made accordingly.

Observe: To vary the time and frequency decision, the “NFFT” and “noverlap” parameters of the “plt.specgram()” perform is utilized in Python, respectively.

Conclusion

Python’s “Matplotlib” library offers the “matplotlib.pyplot.specgram()” perform that’s used to attract a spectrogram in Python. To customise the spectrogram, varied parameters comparable to “cmap”, “NFFT”, and “noverlap”, and many others. of the “plt.specgram()” perform are used to alter the colormap, time, and frequency decision of the spectrogram, respectively. This information offered an in-depth tutorial on “Matplotlib Spectrogram” utilizing quite a few examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments