频谱图演示

频谱图的演示 (specgram())。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. # Fixing random state for reproducibility
  4. np.random.seed(19680801)
  5. dt = 0.0005
  6. t = np.arange(0.0, 20.0, dt)
  7. s1 = np.sin(2 * np.pi * 100 * t)
  8. s2 = 2 * np.sin(2 * np.pi * 400 * t)
  9. # create a transient "chirp"
  10. mask = np.where(np.logical_and(t > 10, t < 12), 1.0, 0.0)
  11. s2 = s2 * mask
  12. # add some noise into the mix
  13. nse = 0.01 * np.random.random(size=len(t))
  14. x = s1 + s2 + nse # the signal
  15. NFFT = 1024 # the length of the windowing segments
  16. Fs = int(1.0 / dt) # the sampling frequency
  17. fig, (ax1, ax2) = plt.subplots(nrows=2)
  18. ax1.plot(t, x)
  19. Pxx, freqs, bins, im = ax2.specgram(x, NFFT=NFFT, Fs=Fs, noverlap=900)
  20. # The `specgram` method returns 4 objects. They are:
  21. # - Pxx: the periodogram
  22. # - freqs: the frequency vector
  23. # - bins: the centers of the time bins
  24. # - im: the matplotlib.image.AxesImage instance representing the data in the plot
  25. plt.show()

频谱图示例

参考

此示例中显示了以下函数的使用方法:

  1. import matplotlib
  2. matplotlib.axes.Axes.specgram
  3. matplotlib.pyplot.specgram

下载这个示例