Spectrogram example


In [1]:
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

In [2]:
fs = 10e3
N = 1e5
amp = 2 * np.sqrt(2)
noise_power = 0.01 * fs / 2
time = np.arange(N) / float(fs)
mod = 500*np.cos(2*np.pi*0.25*time)
carrier = amp * np.sin(2*np.pi*3e3*time + mod)
noise = np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
noise *= np.exp(-time/5)
x = carrier + noise

In [3]:
plt.plot(x)


Out[3]:
[<matplotlib.lines.Line2D at 0x7f02de76f9b0>]

In [4]:
f, t, Sxx = signal.spectrogram(x, fs)

In [5]:
print(t.shape) #(446,)
print(f.shape) #(129,)
print(Sxx.shape) #(129, 446)


(446,)
(129,)
(129, 446)

In [6]:
plt.figure()
plt.pcolormesh(t, f, Sxx)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()