Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from numpy.fft import fft, ifft, ifftshift
- import matplotlib.pyplot as plt
- def fft_conv_and_center(x0, x1):
- return ifftshift(ifft(fft(x0) * fft(x1))).real
- def formula(t, A, B):
- s = lambda x: np.sign(x)
- p = A + B
- m = A - B
- K = (1/4)*s(A*B)
- o0 = (p - 2*t)*s(p - 2*t) - (m + 2*t)*s(m/2 + t)
- o1 = (m - 2*t)*s(-m/2 + t) + (p + 2*t)*s(p/2 + t)
- return K*(o0 + o1)
- def boxcar(A, t):
- A = abs(A)
- return ((t >= -A/2) * (t <= A/2)).astype(int)
- N = 4096
- A, B = 10, 4
- t = np.linspace(-A*B, A*B, N, 0)
- x0 = boxcar(A, t)
- x1 = boxcar(B, t)
- o_fftconv = fft_conv_and_center(x0, x1) * 2 * np.abs(A * B) / N
- o_formula = formula(t, A, B)
- plot(o_fftconv, w=.7, h=.9)
- plot(o_formula, title="FFTconv(x0, x1), formula(x0, x1) | N=%s" % N,
- show=1)
- plot(x0, w=.7, h=.9)
- plot(x1, title="x0, x1 | A=%s, B=%s" % (A, B))
Advertisement
Add Comment
Please, Sign In to add comment