This is the 27th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Review and

Matplotlib is a Python module dedicated to drawing and rendering. The underlying layer is divided into script layer, art layer, and back end. Script layer for us to provide common graphics such as broken line, column, straight square, pie chart, but also provide some special graphics such as violin, box type, quantity field map, examples of previous articles.

  • Provides a boxplot() that shows the distribution of data: matplotlib draws a boxplot

  • Quiver () : matplotlib Plotting quantity field map

  • Provides a violinplot() that shows the distribution and probability of data: matplotlib plots the violin

When we usually meet the quality of audio and video files, we will understand the quality of audio and video by viewing its spectrum map and other information. At the same time, the spectrum map is used in medical, visual, audio detection and analysis and other scenes.

In this issue, we will learn matplotlib. Pyplot. Specgram () the learning of relevant properties, let ‘s go ~

1. Spectrum diagram overview

  • What is a spectrum map?

    • A spectrum diagram showing the relationship of signal frequency to energy
    • The spectrogram records and plots signals at various frequencies in the form of ripples along the X and y axes
    • The spectrum can be divided into three types: linear amplitude spectrum, logarithmic amplitude spectrum, and self-power spectrum
  • Spectrum diagram application scenarios

    • Spectrum graph is often used in mechanical fault diagnosis system to detect fault location, type and degree
    • Spectrum graphs are the main tool dedicated to the analysis of vibration parameters
    • The spectrum map is used to detect the frequency amplitude variation in a specific period of time, such as audio and video
  • Get the usage spectrum diagram

    import matplotlib.pyplot as plt 
    plt.specgram(x)
    Copy the code
  • Pyplot. Specgram (x) method

    • Specgram calculates and plots the spectrum of the data in X
    • The data is divided into NFFT length segments and the spectrum of each segment is calculated
    • The windowing function window is applied to each segment, and the amount of overlap of each segment is specified by NoverLap
    • The spectrum map uses imshow() to plot the color map

2. Spectrum map attributes

  • Set the spectrum mode

    • Key word: mode
    • Optional values are: {‘default’, ‘PSD ‘, ‘magnitude’,’ Angle ‘, ‘phase’}
    • The default is the power spectrum, magnitude is the amplitude spectrum, Angle is the unexpanded phase spectrum and phase is the expanded phase spectrum
  • Sets the spectrum color scheme

    • Keyword: CMAP
    • For example: “color table _r”
    • Commonly used are: ‘Accent’, ‘Accent_r’, ‘Blues’, ‘Blues_r’, ‘BrBG’, ‘BrBG_r’, ‘BuGn’, ‘BuGn_r’, ‘BuPu’, ‘BuPu_r’, ‘CMRmap’, ‘CMRmap_r’, ‘Dark2’, ‘Dark2_r’, ‘GnBu’, ‘GnBu_r’, ‘Greens’
  • Sets the spectrum graph window

    • Keyword: window
    • The default is window_hanning
    • The vectors for creating Windows are: window_hanning, window_none, numpy.blackman, numpy.hamming
  • Sets the spectrum side

    • Key words: side
    • Optional value: onside | twosided
    • Oneside is the default, with oneside used for real data, returning oneside of the spectrum
    • The twoSide sides are used for complex data, forcing the return on both sides
  • Set spectrum graph NFFT

    • Key words: NFFT
    • The default value is 256
    • The number of data points set for each block for FFT cannot be used to get zero fill

3. Step of drawing spectrum map

  • Import matplotlib. Pyplot class
import matplotlib.pyplot as plt
Copy the code
  • Prepare x data using numpy’s arange(), sin(), cos(), etc
t = np.arange(0.0.20.0,dt)

s1 = np.cos(2*np.pi*100*t)

s2 = 2*np.cos(2*np.pi*400*t)

s2[t<=5]=s2[12<t]=0

nse = 0.01 * np.random.random(size=len(t))

x = s1+s2+nse
Copy the code
  • Call the Pyplot.specgram () method to plot the spectrum
plt.specgram(x,NFFT=256,Fs=1,noverlap=128)
Copy the code
  • Call Pyplot.show () to display the spectrum graph
plt.show()
Copy the code

  • Change the color of the spectrum graph to gray, and set NFFT to 1024
plt.specgram(x,NFFT=1024,Fs=1,noverlap=900,cmap="gray")
Copy the code

4. Test the cat

We learned about the properties of drawing spectrum maps, which are usually combined with line maps in practice, so let’s do it

  • Create Axes objects from pyplot.subplots()
  • The Axes1 object calls plot() to draw a line chart
  • The Axes2 object calls the specgram() method to draw a spectrum map
fig,(ax1,ax2) = plt.subplots(nrows=2)

dt= 0.0005
t = np.arange(0.0.20.0,dt)

s1 = np.cos(2*np.pi*100*t)

s2 = 2*np.cos(2*np.pi*400*t)

s2[t<=5]=s2[12<t]=0

nse = 0.01 * np.random.random(size=len(t))

x = s1+s2+nse

ax1.plot(t,x,"pink")

ax2.specgram(x,NFFT=1024,Fs=1,noverlap=900,cmap="gray")

plt.show()
Copy the code

conclusion

In this issue, we have matplotlib. Pyplot. Specgram () methods to study the relevant properties, usually in the check data such as audio and video frequency amplitude of use more.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time