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 :-
- # import the libraries
- import matplotlib.pyplot as plot
- import numpy as np
- # Define the list of frequencies
- frequencies = np.arange(5,105,5)
- # Sampling Frequency
- samplingFrequency = 400
- # Create two ndarrays
- s1 = np.empty([0]) # For samples
- s2 = np.empty([0]) # For signal
- # Start Value of the sample
- start = 1
- # Stop Value of the sample
- stop = samplingFrequency+1
- for frequency in frequencies:
- sub1 = np.arange(start, stop, 1)
- # Signal - Sine wave with varying frequency + Noise
- sub2 = np.sin(2*np.pi*sub1*frequency*1/samplingFrequency)+np.random.randn(len(sub1))
- s1 = np.append(s1, sub1)
- s2 = np.append(s2, sub2)
- start = stop+1
- stop = start+samplingFrequency
- # Plot the signal
- plot.subplot(211)
- plot.plot(s1,s2)
- plot.xlabel('Sample')
- plot.ylabel('Amplitude')
- # Plot the spectrogram
- plot.subplot(212)
- powerSpectrum, freqenciesFound, time, imageAxis = plot.specgram(s2, Fs=samplingFrequency)
- plot.xlabel('Time')
- plot.ylabel('Frequency')
- plot.show()
Comments
Post a Comment