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
  11. A good blog. Thanks for sharing the information. It is very useful for my future. keep sharing.
    Advanced Ethical Hacking course in Chennai | Certified Ethical Hacking Training in Velachery

    ReplyDelete
  12. It's very great post... Really you are... done a wonderful job Keep up the good work and continue sharing like this.
    No.1 RPA Training institute in Chennai | No.1 RPA Training institute in Velachery

    ReplyDelete
  13. This blog is really helpful and interesting and well appreciated! Thank you so much for sharing this great article
    No.1 Automation Anywhere Training Institute in Chennai | No.1 Automation Anywhere Training Institute in Velachery

    ReplyDelete
  14. Thanks for giving Good Example. Fantastic article, Viral. Very well written, clear and concise.
    No.1 UIPath Training Institute in Chennai | No.1 UIPath Training Institute in Velachery

    ReplyDelete
  15. Thank you for this brief explanation and ver nice information. well, got a good knowledge. Have a nice idea, this is more helpful to me. This can gives the better performance.
    No.1 RPA Training institute in Chennai | No.1 RPA Training institute in Velachery

    ReplyDelete
  16. Thank you for sharing wonderful information with us to get some idea about that content. check it once through.
    No.1 Blue Prism Training Institute in Chennai | No.1 Blue Prism Training Institute in Velachery

    ReplyDelete
  17. Thank you for your information. I have got some important suggestions from it. Keep on sharing. Best MBA project center in Chennai| Best MBA Project Center Tharamani

    ReplyDelete
  18. Best Electrical project center in Chennai| Best Electrical Project Center Perungudi This blog is really useful and it is very interesting thanks for sharing, it is really good and exclusive.

    ReplyDelete
  19. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge.
    Blue Prism Training in Chennai | Blue Prism Training in Taramani | Blue Prism Training in Velachery

    ReplyDelete
  20. Excellent information with unique content and it is very useful to know about the information based on blogs...
    Selenium course in Taramani | Selenium Certification in Tambaram | Selenium Training in Chennai | Selenium Training Center in Velachery

    ReplyDelete
  21. This is really too useful and have more ideas from yours. keep sharing many techniques and thanks for sharing the information.
    Blue Prism Training in Chennai | Blue Prism Certification in Medavakkam | Blue Prism Training Center in Velachery

    ReplyDelete
  22. Your Blog is really awesome with useful and helpful content for us. Thanks for sharing. Keep updating more information.
    Selenium Training in Tambaram | Selenium Training Center in Chennai | Selenium Certification in Velachery | Selenium Exam Center in Medavakkam

    ReplyDelete
  23. Nice blog. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    Best Power System Project in Chennai | Best Power System Project in Guindy

    ReplyDelete
  24. Nice blog, really I feel happy to see this useful blog… Thanks for sharing this valuable information to our vision.I’m expecting much of these useful things. JAVA Certification Course in Chennai | JAVA Certification Course in Velachery

    ReplyDelete
  25. Its really informative blog.I found useful. Thanks for sharing

    awesome post. JAVA Training Course in Chennai | JAVA Training Course in Velachery

    ReplyDelete
  26. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Selenium Training Institute in Chennai | Selenium Certification in Pallikaranai | Selenium Course in Velachery | Selenium Training Center in Medavakkam

    ReplyDelete
  27. It was wonderful article and I was really impressed by reading this blog.keep doing share this kind of articles.
    C / C++ Training Institute in Chennai | C / C++ Training Institute in Velachery

    ReplyDelete
  28. Really i enjoyed very much. And this may helpful for lot of peoples. So you are provided such a nice and great article within this.
    Android Training Center in Chennai | Android Training Centre in Velachary | Android Training Centre in Kancheepuram |

    ReplyDelete
  29. This is really too useful and have more ideas from yours. keep sharing many techniques and thanks for sharing the information.
    Best Graphic Designing Training Institute in Chennai | Best Graphic Designing Training Institute in Kanchipuram

    ReplyDelete
  30. This is really too useful and have more ideas from yours. Keep sharing many techniques and thanks for sharing the information.
    Android Training in Chennai | Android Training Center in Velachery | Android Training Institute in Pallikaranai

    ReplyDelete
  31. Good post..Thanks for sharing this wonderful post..Keep blogging.
    RPA Training in Chennai | RPA Training in Kanchipuram

    ReplyDelete

Post a Comment

Popular Posts