The Wayback Machine - https://web.archive.org/web/20240602214126/https://www.geeksforgeeks.org/matplotlib-pyplot-specgram-in-python/
Open In App

Matplotlib.pyplot.specgram() in Python

Last Updated : 21 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface.

matplotlib.pyplot.specgram() Function

The specgram() function in pyplot module of matplotlib library is used to plot a spectrogram.

Syntax: matplotlib.pyplot.specgram(x, NFFT=None, Fs=None, Fc=None, detrend=None, window=None, noverlap=None, cmap=None, xextent=None, pad_to=None, sides=None, scale_by_freq=None, mode=None, scale=None, vmin=None, vmax=None, *, data=None, **kwargs)

Parameters: This method accept the following parameters that are described below:

  • x: This parameter is a sequence of data.
  • Fs : This parameter is a scalar. Its default value is 2.
  • window: This parameter take a data segment as an argument and return the windowed version of the segment. Its default value is window_hanning()
  • sides: This parameter specifies which sides of the spectrum to return. This can have following values : ‘default’, ‘onesided’ and ‘twosided’.
  • pad_to : This parameter contains the integer value to which the data segment is padded.
  • Fc: This parameter is also contains the integer value to offsets the x extents of the plot to reflect the frequency range. Its default value is 0
  • NFFT : This parameter contains the number of data points used in each block for the FFT.
  • detrend : This parameter contains the function applied to each segment before fft-ing, designed to remove the mean or linear trend {‘none’, ‘mean’, ‘linear’}.
  • scale_by_freq : This parameter is allows for integration over the returned frequency values.
  • mode : This parameter is that what sort of spectrum to use {‘default’, ‘psd’, ‘magnitude’, ‘angle’, ‘phase’}.
  • noverlap : This parameter is the number of points of overlap between blocks.
  • scale : This parameter contains the scaling of the values in the spec {‘default’, ‘linear’, ‘dB’}.
  • Fc : This parameter is the center frequency of x.
  • camp: This parameter is a matplotlib.colors.Colormap instance.

Returns: This returns the following:

  • spectrum :This returns the angle spectrum in radians.
  • freqs :This returns the frequencies corresponding to the elements in spectrum.
  • t: This returns the times corresponding to midpoints of segments.
  • im: This returns the image created by imshow containing the spectrogram.

The resultant is (spectrum, freqs, t, im)

Below examples illustrate the matplotlib.pyplot.specgram() function in matplotlib.pyplot:

Example 1:




# Implementation of matplotlib function
import matplotlib.pyplot as plt 
import numpy as np 
  
    
dt = 0.005
t = np.arange(0.0, 20.0, dt) 
x = np.sin(np.pi * t) + 1.5 * np.cos(np.pi * 2*t) 
  
plt.specgram(x, Fs = 1)
plt.title('matplotlib.pyplot.specgram() Example\n', 
          fontsize = 14, fontweight ='bold')
  
plt.show()


Output:
Image

Example 2:




# Implementation of matplotlib function
import matplotlib.pyplot as plt 
import numpy as np 
  
  
np.random.seed(9360801) 
    
dt = 0.0005
t = np.arange(0.0, 20.0, dt) 
s1 = np.sin(4 * np.pi * 100 * t) 
s2 = 1.5 * np.sin(1.5 * np.pi * 400 * t) 
    
s2[t <= 10] = s2[12 <= t] = 0
    
nse = 0.2 * np.random.random(size = len(t)) 
    
x = s1 + s2 + nse   
NFFT = 512 
Fs = int(1.0 / dt)  
  
plt.specgram(x, Fs = Fs, cmap = plt.cm.bone) 
plt.title('matplotlib.pyplot.specgram() Example\n',
          fontsize = 14, fontweight ='bold')
  
plt.show()


Output:
Image



Similar Reads

Matplotlib.axes.Axes.specgram() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute. matplotlib.axes.Axes.specgram() Function The Axes.
3 min read
Matplotlib.pyplot.broken_barh() in Python
Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays. Pyplot is a collection of command style functions that make matplotlib work like MATLAB. matplotlib.pyplot.broken_barh() The function broken_barh() is used to Plot a horizontal sequence of rectan
2 min read
Matplotlib.pyplot.axis() in Python
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. Pyplot is a Matplotlib module which provides a MATLAB-like interface. Matplotlib is designed to be as usable as MATLAB, with the ability to use Python and the advantage of being free and open-source. matplotlib.pyplot.axis() This function is us
1 min read
Matplotlib.pyplot.get_fignums() in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc. matplotlib.pyplot.get_fignums() method The ge
2 min read
Python | Matplotlib.pyplot ticks
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2003.One of the greatest benefits of visualization is that it allows us visual access to huge
3 min read
Matplotlib.pyplot.waitforbuttonpress() in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc. matplotlib.pyplot.waitforbuttonpress() method
2 min read
Matplotlib.pyplot.streamplot() in Python
Stream plot is basically a type of 2D plot used majorly by physicists to show fluid flow and 2D field gradients .The basic function to create a stream plot in Matplotlib is: ax.streamplot(x_grid, y_grid, x_vec, y_vec, density=spacing) Here x_grid and y_grid are arrays of the x and y points.The x_vec and y_vec represent the stream velocity of each p
7 min read
Matplotlib.pyplot.ylim() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.ylim() Function The ylim() function in pyplot module of matplotlib library is used to get or set the y-limits of the current axes. Syntax
2 min read
Matplotlib.pyplot.figimage() function in Python
Matplotlib is a widely used library in Python for plotting various graphs, as it provides very efficient ways and easy to understand methods for complex plots also. pyplot is a collection of command style functions that make matplotlib work like MATLAB. figimage() function matplotlib.pyplot.figimage() is a method for adding a non-resampled image to
2 min read
matplotlib.pyplot.arrow() in Python
Matplotlib is a very powerful plotting library useful for those working with Python and NumPy. And for making statistical interference, it becomes very necessary to visualize our data and Matplotlib is the tool that can be very helpful for this purpose. It provides MATLAB like interface only difference is that it uses Python and is open source. mat
2 min read
Article Tags :
Practice Tags :