### Dynamic spectrum (spectrogram) using Python

The signal processing course has reached a stage where the discrete Fourier transform has been introduced. Once all the theorems are proved, the applications begin. Here's a little python code to analyze time-frequency content of a signal. The signal is divided into snippets of time, which can be overlapping in time. Each snippet is discrete Fourier transformed using a windowed and zero padded FFT.

Here's an example output:

Here's the code:

Here's an example output:

Dynamic spectrum of a chirped signal. |

```
```

```
#!/usr/bin/env python
#
# (c) 2017 Juha Vierinen
import numpy as n
import matplotlib.pyplot as plt
import scipy.signal as s
# create dynamic spectrum
def spectrogram(x,M=1024,N=128,delta_n=100):
max_t=int(n.floor((len(x)-N)/delta_n))
t=n.arange(max_t)
X=n.zeros([max_t,M],dtype=n.complex64)
w=s.hann(N)
xin=n.zeros(N)
for i in range(max_t):
xin[0:N]=x[i*delta_n+n.arange(N)]
X[i,:]=n.fft.fft(w*xin,M)
return(X)
# sample rate (Hz)
fs=4096.0
# sample indexes (one second of signal)
nn=n.arange(4096)
# generate a chirp signal
x=n.sin(0.15e-14*nn**5.0)
# time step
delta_n=25
M=2048
# create dynamic spectrum.
# Use
# - 2048 point FFT
# - 128 samples for each spectra
# - 100 sample increments in time
S=spectrogram(x,M=M,N=128,delta_n=delta_n)
freqs=n.fft.fftfreq(2048,d=1.0/fs)
time=delta_n*n.arange(S.shape[0])/fs
# plot signal
plt.figure(figsize=(12,10))
plt.subplot(211)
plt.plot(nn/fs,x)
plt.title("Signal $x[n]$")
plt.xlabel("Time (s)")
plt.ylabel("Signal amplitude")
plt.subplot(212)
plt.title("Spectrogram")
plt.pcolormesh(time,freqs[0:(M/2)],n.transpose(10.0*n.log10(n.abs(S[:,0:(M/2)])**2.0)),vmin=0)
plt.xlim([0,n.max(time)])
plt.ylim([0,fs/2.0])
plt.xlabel("Time (s)")
plt.ylabel("Frequency (Hz)")
cb=plt.colorbar(orientation="horizontal")
cb.set_label("dB")
plt.tight_layout()
plt.savefig("dynspec.png")
plt.show()
```

```
```

Great post! I am actually getting ready to across this information, is very helpful. Keep up the good work you are doing here.

ReplyDeleteSelenium Training Institute in Chennai | Selenium Training Institute in Velachery

Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area.

ReplyDeleteBlue Prism Automation Robotic course in Chennai | Blue Prism Automation Robotic course in Velachery

Nice post... Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating...

ReplyDeleteNo.1 MCSE and MCSA Certificate Exam Centre in Chennai |No.1 MCSE and MCSA Certificate Exam Centre in Chennai.

Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on updating...

ReplyDeleteBest Android & IOS Training Institute in Chennai | Best Android & IOS Training Institute in Porur

Thanks a lot very much for the high your blog post quality and results-oriented help.Keep updating more info..

ReplyDeleteNo.1 Automation Anywhere Training Institute in Chennai | No.1 Automation Anywhere Training Institute in Kanchipuram | No.1 Automation Anywhere Training Institute in Velachery

I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.

ReplyDeleteNo.1 Blue Prism Training Institute in Chennai | No.1 Blue Prism Training Institute in Velachery | No.1 Blue Prism Training Institute in Kanchipuram