Guest User

Untitled

a guest
Jun 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import numpy as np
  2. from scipy.signal import lfilter
  3. import matplotlib.pyplot as plt
  4.  
  5. # Construct the input signal
  6. idx = np.arange(512)
  7. w0 = [2*np.pi/(80 + x/8) for x in idx]
  8. x0 = np.sin(w0*idx)
  9. # Calculate the transforms
  10. h = hilbert(x0)
  11. I = x0 # normally it should be np.roll(x0, 3)
  12. Q = lfilter([0.25, 0, 0.75, 0, -0.25, 0, -0.75], [1], x0)
  13. # Calculate amplitude arrays
  14. A1 = np.abs(h)
  15. A2 = np.sqrt(I**2 + Q**2)
  16. # Calcuclate phase arrays
  17. ph1 = np.angle(h)
  18. ph2 = -np.arctan2(Q, I)
  19. # Calculate instant. frequency
  20. w1 = np.diff(np.unwrap(ph1))
  21. w2 = np.diff(np.unwrap(ph2))
  22.  
  23. # Plot the results
  24. fig = plt.figure()
  25. ax = plt.subplot(311)
  26. plt.plot(x0, lw=3, color='b', label='signal')
  27. plt.plot(A1*np.cos(ph1), lw=2, color='g', label='x1')
  28. plt.plot(A1, lw=2, ls='dotted', color='g', label='A1')
  29. plt.plot(A2*np.cos(ph2), lw=1, color='k', label='x2')
  30. plt.plot(A2, lw=1, ls='dotted', color='k', label='A2')
  31. plt.legend(bbox_to_anchor=(1, 1), loc=2, borderaxespad=0.)
  32. plt.subplot(312, sharex=ax)
  33. plt.plot(ph1, lw=2, color='g', label='ph1')
  34. plt.plot(ph2, lw=1, color='k', label='ph2')
  35. plt.legend(bbox_to_anchor=(1, 1), loc=2, borderaxespad=0.)
  36. plt.subplot(313, sharex=ax)
  37. plt.plot(w0, lw=3, color='b', label='w0')
  38. plt.plot(w1, lw=2, color='g', label='w1')
  39. plt.plot(w2, lw=1, color='k', label='w2')
  40. plt.legend(bbox_to_anchor=(1, 1), loc=2, borderaxespad=0.)
  41. fig.show()
Add Comment
Please, Sign In to add comment