绘制两个信号交叉密度

计算两个信号的交叉谱密度

计算两个信号的交叉谱密度图示;

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. fig, (ax1, ax2) = plt.subplots(2, 1)
  4. # make a little extra space between the subplots
  5. fig.subplots_adjust(hspace=0.5)
  6. dt = 0.01
  7. t = np.arange(0, 30, dt)
  8. # Fixing random state for reproducibility
  9. np.random.seed(19680801)
  10. nse1 = np.random.randn(len(t)) # white noise 1
  11. nse2 = np.random.randn(len(t)) # white noise 2
  12. r = np.exp(-t / 0.05)
  13. cnse1 = np.convolve(nse1, r, mode='same') * dt # colored noise 1
  14. cnse2 = np.convolve(nse2, r, mode='same') * dt # colored noise 2
  15. # two signals with a coherent part and a random part
  16. s1 = 0.01 * np.sin(2 * np.pi * 10 * t) + cnse1
  17. s2 = 0.01 * np.sin(2 * np.pi * 10 * t) + cnse2
  18. ax1.plot(t, s1, t, s2)
  19. ax1.set_xlim(0, 5)
  20. ax1.set_xlabel('time')
  21. ax1.set_ylabel('s1 and s2')
  22. ax1.grid(True)
  23. cxy, f = ax2.csd(s1, s2, 256, 1. / dt)
  24. ax2.set_ylabel('CSD (db)')
  25. plt.show()

下载这个示例