Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. #%%###########################################################################
  5. def allfft(x):
  6.     coef = np.fft.fft(x)
  7.     return coef, np.angle(coef), np.abs(coef)
  8.  
  9. def plot(x, y=None, show=False, **kw):
  10.     if y is None:
  11.         plt.plot(x, **kw)
  12.     else:
  13.         plt.plot(x, y, **kw)
  14.     _scale(plt.gcf(), plt.gca(), show=show)
  15.  
  16. def scat(x, y=None, show=False, **kw):
  17.     if y is None:
  18.         plt.scatter(np.arange(len(x)), x, **kw)
  19.     else:
  20.         plt.scatter(x, y, **kw)
  21.     _scale(plt.gcf(), plt.gca(), show=show)
  22.  
  23. def _scale(fig, ax, show=False):
  24.     xmin, xmax = ax.get_xlim()
  25.     rng = xmax - xmin
  26.     ax.set_xlim(xmin + .02 * rng, xmax - .02 * rng)
  27.     if show:
  28.         plt.show()
  29.  
  30. #%%###########################################################################
  31. M = 1 + 128
  32. fc = 2 / M
  33. t  = np.linspace(0, 1, M)
  34. ti = np.arange(M)
  35. wh = .54 - .46 * np.cos(2 * np.pi * t)  # Hamming
  36. sinc = np.sin(2 * np.pi * fc * (ti - M / 2)) / (ti - M / 2)
  37. if M % 2 == 0:
  38.     sinc[M // 2] = 2 * np.pi * fc
  39.  
  40. whs = wh * sinc / np.pi
  41. #%%###########################################################################
  42. coef_hs, theta_hs, r_hs = allfft(whs)
  43. #%%
  44. scat(np.log10(r_hs)     * 20, show=1, color='g')
  45. scat(np.log10(r_hs[:9]) * 20, show=1, color='g')
  46. #%%###########################################################################
  47. tx = np.linspace(0, 1, 128, endpoint=False)
  48. s1 = 10 * np.cos(2 * np.pi * 1 * tx)
  49. s2 = 1  * np.cos(2 * np.pi * 5 * tx)
  50. s = s1 + s2
  51. coef_s, theta_s, r_s = allfft(s)
  52.  
  53. conv_len = len(s) + M - 1
  54. sp   = np.pad(s,   [0, conv_len - len(s)])
  55. whsp = np.pad(whs, [0, conv_len - M])
  56.  
  57. coef_sp,  theta_sp,  r_sp  = allfft(sp)
  58. coef_hsp, theta_hsp, r_hsp = allfft(whsp)
  59. #%%###########################################################################
  60. plot(s1)
  61. plot(s2, show=1)
  62. plot(s,  show=1, color='k')
  63. #%%
  64. out = np.convolve(sp, whsp)[:conv_len]
  65. coef_o, theta_o, r_o = allfft(out)
  66. #%%###########################################################################
  67. plot(s1)
  68. plot(out, color='purple')
  69. plt.gca().set_xlim(-2, 257)
  70. plt.show()
  71.  
  72. scat(r_o, color='purple', show=1)
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement