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:
Dynamic spectrum of a chirped signal. 
Here's the code:

#!/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()

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This Nice blog is very informative. I’m looking and reading your points is so impressive. Regards,
    Blue prism Training in Chennai | Blue prism Training in Besant Nagar

    ReplyDelete
  3. Excellent and useful blog admin, I would like to read more about this topic.
    UIPath Training in Chennai | UIPath Training in Guindy

    ReplyDelete
  4. Nice article… I am sure I will share this information to my friends and relatives. Best Electrical Project Center in ChennaI | Best Electrical Project Center in Tambaram

    ReplyDelete
  5. Excellent information with unique content and it is very useful to know about the information based on blogs...Best MBA Project Center in Chennai |Best MBA Project Center in Perungudi

    ReplyDelete
  6. Nice article… I am sure I will share this information to my friends and relatives. Thanks for sharing the useful blog
    Ethical Hacking Training in Chennai | Ethical Hacking Training in Taramani

    ReplyDelete
  7. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
    Best Automation Anywhere Training Institute in Chennai | Best Automation Anywhere Training Institute in Velachery

    ReplyDelete
  8. Really nice article!!! I got more info to your innovate post. I like more addition info to your blog, keep posting....
    No.1 Ethical Hacking Training institute in Chennai | No.1 Ethical Hacking Training institute in Velachery

    ReplyDelete
  9. 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.
    Blueprism Exam Center in Chennai | Blueprism Exam Center in Velachery

    ReplyDelete
  10. Great post! This is very useful for me and gain more information, Thanks for sharing with us.
    No.1 UIPath Training Institute in Chennai | No.1 UIPath Training Institute in Velachery

    ReplyDelete

Post a Comment

Popular Posts