0

I have applied following codes to create spectogram plot

from scipy import signal
import matplotlib.pyplot as plt
import numpy as np

# Define the signal
Fs = 1e4     # Sampling Frequency


# Sin wave with Amplite 1 unit, 50Hz, for duration 0-5 seconds
N1 = (1e5)/2
time1 = np.arange(N1) / float(Fs)          
x1 = 1*np.sin(2*np.pi*50*time1)


# Sin wave with Amplite 0.008 units, 100Hz, for duration 5-10 seconds
time2 = np.arange(N1,2*N1) / float(Fs)
x2 = 0.008*np.sin(2*np.pi*100*time2)


# Concatenate above two waves
T    =np.concatenate((time1,time2))
Y    =np.concatenate((x1,x2))



# Plot the waves
plt.plot(T , Y)
plt.title('Time Data')
plt.ylabel('Amplitude')
plt.xlabel('Time [sec]')
plt.show()


# spectrogram 
freq_axis, time_axis, Sxx = signal.spectrogram(Y, Fs)


# Plot the spectrogram 
plt.pcolormesh(time_axis, freq_axis, Sxx)
plt.title('Spectrogram')
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.xlim(0,10)
plt.ylim(0,200)
plt.show()

The results of the spectogram plot are as such:

enter image description here

In this plot, it seems that, the dominant frequencies are 37Hz to 75Hz from 0-5sec (yellow region). However, in the signal created, the only dominant freq. is 50 Hz from 0-5sec.

Also, the signal with 0.008 units amplitude, 100Hz created for duration 5-10 sec, cannot be identified.

Can somebody please let me know where am I going wrong?

Also, is there any way out to depict a colour bar just besides the plot window of the spectrogram so that the amplitude values corresponding to the colour can be seen?

1 Answer 1

2

First the answer to the question of the colorbar. Matplotlib.pyplot provides a function for creating colorbars. You can just add

plt.colorbar()

to add a standard colorbar to your plot.

Then to your initial question. You might need a better understanding of what the signal.spectrogram() does. To get an intuition on the result, you have to know that the function used to transform the signal from the time in a frequency-time domain is limited in the provided accuracy. So, you can either have a very accurate calculation of the frequencies and not know exactly when the frequency is present, or you are very accurate in the when but not in the which frequency.

In your example, the frequency is not calculated very accurately, resulting in so called frequency bins. The bin size is the height of the yellow block. So the spectrogram identifies the two frequencies in their respective bins, for this reason one bin is yellow and the other is blue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.