Guest User

Untitled

a guest
Sep 15th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. #%%
  4. """EDIT THESE"""
  5. def dft(x):
  6.     return np.fft.fft(x)
  7.  
  8. def d_idft(coef, x, slope=True):
  9.     N = len(coef)
  10.     coef = coef * (1 - np.exp(-1j * 2 * np.pi / N * np.arange(N)))
  11.     slope = (x[-1] - x[0]) if slope else 0
  12.     coef += slope
  13.     return np.fft.ifft(coef)[1:], coef
  14.  
  15. #%%###########################################################################
  16. N = 128
  17. x = np.zeros(N)
  18. x[0]  =  1
  19. x[-2] = -1
  20. #%%
  21. coef = dft(x)
  22. dx   = np.diff(x)
  23. dx_est1, dcoef1 = d_idft(coef, x, slope=False)
  24. dx_est2, dcoef2 = d_idft(coef, x, slope=True)
  25. dx_est3, dcoef3 = np.fft.ifft(np.fft.fft(dx)), np.fft.fft(dx)
  26. #%%
  27. print("{:.64}".format(np.mean(np.abs(dx - dx_est1))))
  28. print("{:.64}".format(np.mean(np.abs(dx - dx_est2))))
  29. print("{:.64}".format(np.mean(np.abs(dx - dx_est3))))
  30. #%%###########################################################################
  31. theta,   r = np.angle(dcoef1), np.abs(dcoef1)
  32. theta2, r2 = np.angle(dcoef2), np.abs(dcoef2)
  33. theta3, r3 = np.angle(dcoef3), np.abs(dcoef3)
  34. #%%
  35. plt.plot(r); plt.plot(r2); plt.plot(r3); plt.show()  # can comment line below
  36. theta = np.unwrap(theta); theta2 = np.unwrap(theta2); theta3 = np.unwrap(theta3)
  37. plt.plot(theta); plt.plot(theta2); plt.plot(theta3); plt.show()
  38. #%%###########################################################################
  39. print("Y[0]:\nw/o slope = {}\nw/slope = {}\nground truth = {}\n".format(
  40.     dcoef1[0], dcoef2[0], dcoef3[0]))
  41. #%%
  42. th = 1e-14
  43. print("Slope term:",
  44.       "PASSED" if abs(dcoef2[0] - dcoef3[0]) < th else "FAILED")
  45. print("Reconstruction:",
  46.       "PASSED" if sum(np.abs(dx_est2 - dx))  < th else "FAILED")
  47. if len(dcoef2) == len(dx):
  48.     print("len(Y): PASSED")
  49.     print("Coefficients:",
  50.           "PASSED" if sum(np.abs(dcoef2 - dcoef3)) < th else "FAILED")
  51. else:
  52.     print("len(Y): FAILED\nCoefficients: FAILED")
  53.  
Add Comment
Please, Sign In to add comment