频谱表示图

该图显示了具有加性噪声的正弦信号的不同频谱表示。 通过利用快速傅立叶变换(FFT)计算离散时间信号的(频率)频谱。

频谱表示图例

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. np.random.seed(0)
  4. dt = 0.01 # sampling interval
  5. Fs = 1 / dt # sampling frequency
  6. t = np.arange(0, 10, dt)
  7. # generate noise:
  8. nse = np.random.randn(len(t))
  9. r = np.exp(-t / 0.05)
  10. cnse = np.convolve(nse, r) * dt
  11. cnse = cnse[:len(t)]
  12. s = 0.1 * np.sin(4 * np.pi * t) + cnse # the signal
  13. fig, axes = plt.subplots(nrows=3, ncols=2, figsize=(7, 7))
  14. # plot time signal:
  15. axes[0, 0].set_title("Signal")
  16. axes[0, 0].plot(t, s, color='C0')
  17. axes[0, 0].set_xlabel("Time")
  18. axes[0, 0].set_ylabel("Amplitude")
  19. # plot different spectrum types:
  20. axes[1, 0].set_title("Magnitude Spectrum")
  21. axes[1, 0].magnitude_spectrum(s, Fs=Fs, color='C1')
  22. axes[1, 1].set_title("Log. Magnitude Spectrum")
  23. axes[1, 1].magnitude_spectrum(s, Fs=Fs, scale='dB', color='C1')
  24. axes[2, 0].set_title("Phase Spectrum ")
  25. axes[2, 0].phase_spectrum(s, Fs=Fs, color='C2')
  26. axes[2, 1].set_title("Angle Spectrum")
  27. axes[2, 1].angle_spectrum(s, Fs=Fs, color='C2')
  28. axes[0, 1].remove() # don't display empty ax
  29. fig.tight_layout()
  30. plt.show()

下载这个示例