Guest User

Untitled

a guest
Jun 9th, 2022
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. import numpy as np
  2. from numpy.fft import fft, ifft, ifftshift
  3. import matplotlib.pyplot as plt
  4.  
  5. def fft_conv_and_center(x0, x1):
  6. return ifftshift(ifft(fft(x0) * fft(x1))).real
  7.  
  8. def formula(t, A, B):
  9. s = lambda x: np.sign(x)
  10. p = A + B
  11. m = A - B
  12.  
  13. K = (1/4)*s(A*B)
  14. o0 = (p - 2*t)*s(p - 2*t) - (m + 2*t)*s(m/2 + t)
  15. o1 = (m - 2*t)*s(-m/2 + t) + (p + 2*t)*s(p/2 + t)
  16. return K*(o0 + o1)
  17.  
  18. def boxcar(A, t):
  19. A = abs(A)
  20. return ((t >= -A/2) * (t <= A/2)).astype(int)
  21.  
  22. N = 4096
  23. A, B = 10, 4
  24. t = np.linspace(-A*B, A*B, N, 0)
  25.  
  26. x0 = boxcar(A, t)
  27. x1 = boxcar(B, t)
  28.  
  29. o_fftconv = fft_conv_and_center(x0, x1) * 2 * np.abs(A * B) / N
  30. o_formula = formula(t, A, B)
  31.  
  32. plot(o_fftconv, w=.7, h=.9)
  33. plot(o_formula, title="FFTconv(x0, x1), formula(x0, x1) | N=%s" % N,
  34. show=1)
  35. plot(x0, w=.7, h=.9)
  36. plot(x1, title="x0, x1 | A=%s, B=%s" % (A, B))
Advertisement
Add Comment
Please, Sign In to add comment