matplotlib.pyplot.specgram#
- 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)[source]#
Plot a spectrogram.
Compute and plot a spectrogram of data inx. Data are split intoNFFT length segments and the spectrum of each section iscomputed. The windowing functionwindow is applied to eachsegment, and the amount of overlap of each segment isspecified withnoverlap. The spectrogram is plotted as a colormap(using imshow).
- Parameters:
- x1-D array or sequence
Array or sequence containing the data.
- Fsfloat, default: 2
The sampling frequency (samples per time unit). It is used to calculatethe Fourier frequencies,freqs, in cycles per time unit.
- windowcallable or ndarray, default:
window_hanning
A function or a vector of lengthNFFT. To create window vectors see
window_hanning
,window_none
,numpy.blackman
,numpy.hamming
,numpy.bartlett
,scipy.signal
,scipy.signal.get_window
, etc. If afunction is passed as the argument, it must take a data segment as anargument and return the windowed version of the segment.- sides{'default', 'onesided', 'twosided'}, optional
Which sides of the spectrum to return. 'default' is one-sided for realdata and two-sided for complex data. 'onesided' forces the return of aone-sided spectrum, while 'twosided' forces two-sided.
- pad_toint, optional
The number of points to which the data segment is padded when performingthe FFT. This can be different fromNFFT, which specifies the numberof data points used. While not increasing the actual resolution of thespectrum (the minimum distance between resolvable peaks), this can givemore points in the plot, allowing for more detail. This corresponds tothen parameter in the call to
fft
. The default is None,which setspad_to equal toNFFT- NFFTint, default: 256
The number of data points used in each block for the FFT. A power 2 ismost efficient. This shouldNOT be used to get zero padding, or thescaling of the result will be incorrect; usepad_to for this instead.
- detrend{'none', 'mean', 'linear'} or callable, default: 'none'
The function applied to each segment before fft-ing, designed to removethe mean or linear trend. Unlike in MATLAB, where thedetrend parameteris a vector, in Matplotlib it is a function. The
mlab
module definesdetrend_none
,detrend_mean
, anddetrend_linear
,but you can use a custom function as well. You can also use a string tochoose one of the functions: 'none' callsdetrend_none
. 'mean' callsdetrend_mean
. 'linear' callsdetrend_linear
.- scale_by_freqbool, default: True
Whether the resulting density values should be scaled by the scalingfrequency, which gives density in units of 1/Hz. This allows forintegration over the returned frequency values. The default is True forMATLAB compatibility.
- mode{'default', 'psd', 'magnitude', 'angle', 'phase'}
What sort of spectrum to use. Default is 'psd', which takes thepower spectral density. 'magnitude' returns the magnitudespectrum. 'angle' returns the phase spectrum without unwrapping.'phase' returns the phase spectrum with unwrapping.
- noverlapint, default: 128
The number of points of overlap between blocks.
- scale{'default', 'linear', 'dB'}
The scaling of the values in thespec. 'linear' is no scaling.'dB' returns the values in dB scale. Whenmode is 'psd',this is dB power (10 * log10). Otherwise, this is dB amplitude(20 * log10). 'default' is 'dB' ifmode is 'psd' or'magnitude' and 'linear' otherwise. This must be 'linear'ifmode is 'angle' or 'phase'.
- Fcint, default: 0
The center frequency ofx, which offsets the x extents of theplot to reflect the frequency range used when a signal is acquiredand then filtered and downsampled to baseband.
- cmap
Colormap
, default:rcParams["image.cmap"]
(default:'viridis'
) - xextentNone or (xmin, xmax)
The image extent along the x-axis. The default setsxmin to theleft border of the first bin (spectrum column) andxmax to theright border of the last bin. Note that fornoverlap>0 the widthof the bins is smaller than those of the segments.
- dataindexable object, optional
If given, the following parameters also accept a string
s
, which isinterpreted asdata[s]
ifs
is a key indata
:x
- vmin, vmaxfloat, optional
vmin and vmax define the data range that the colormap covers.By default, the colormap covers the complete value range of thedata.
- **kwargs
Additional keyword arguments are passed on to
imshow
which makes the specgram image. The origin keyword argumentis not supported.
- Returns:
- spectrum2D array
Columns are the periodograms of successive segments.
- freqs1-D array
The frequencies corresponding to the rows inspectrum.
- t1-D array
The times corresponding to midpoints of segments (i.e., the columnsinspectrum).
- im
AxesImage
The image created by imshow containing the spectrogram.
See also
psd
Differs in the default overlap; in returning the mean of the segment periodograms; in not returning times; and in generating a line plot instead of colormap.
magnitude_spectrum
A single spectrum, similar to having a single segment whenmode is 'magnitude'. Plots a line instead of a colormap.
angle_spectrum
A single spectrum, similar to having a single segment whenmode is 'angle'. Plots a line instead of a colormap.
phase_spectrum
A single spectrum, similar to having a single segment whenmode is 'phase'. Plots a line instead of a colormap.
Notes
Note
This is thepyplot wrapper for
axes.Axes.specgram
.The parametersdetrend andscale_by_freq do only apply whenmodeis set to 'psd'.