How do you make a spectrogram in Python?

 Question:-

How do you make a spectrogram in Python?

Plot spectrogram in Python

A spectrogram is like a photograph of a signal on the x-axis and y-axis. To plot this photograph we have to import a module named as Matplotlib.pyplot. this module provides specgram() method which takes signal as input and plot the spectrogram. So let’s see its Solution.

Example :-

  1. # import the libraries 
  2. import matplotlib.pyplot as plot 
  3. import numpy as np 
  4. # Define the list of frequencies 
  5. frequencies = np.arange(5,105,5) 
  6. # Sampling Frequency 
  7. samplingFrequency = 400 
  8. # Create two ndarrays 
  9. s1 = np.empty([0]) # For samples 
  10. s2 = np.empty([0]) # For signal 
  11. # Start Value of the sample 
  12. start = 1 
  13. # Stop Value of the sample 
  14. stop = samplingFrequency+1 
  15. for frequency in frequencies: 
  16. sub1 = np.arange(start, stop, 1) 
  17. # Signal - Sine wave with varying frequency + Noise 
  18. sub2 = np.sin(2*np.pi*sub1*frequency*1/samplingFrequency)+np.random.randn(len(sub1)) 
  19. s1 = np.append(s1, sub1) 
  20. s2 = np.append(s2, sub2) 
  21. start = stop+1 
  22. stop = start+samplingFrequency 
  23. # Plot the signal 
  24. plot.subplot(211) 
  25. plot.plot(s1,s2) 
  26. plot.xlabel('Sample') 
  27. plot.ylabel('Amplitude') 
  28. # Plot the spectrogram 
  29. plot.subplot(212) 
  30. powerSpectrum, freqenciesFound, time, imageAxis = plot.specgram(s2, Fs=samplingFrequency) 
  31. plot.xlabel('Time') 
  32. plot.ylabel('Frequency') 
  33. plot.show()

Comments

Popular posts from this blog

Programmingshark index 6

Proassignmenthelp index 12

Proassignmenthelp index 27