How does numpy.fft.fft work?

I'm currently trying to understand the fft function from numpy. To do this, I tested the following assumption:
I have two functions: f(x) = x^2 and g(x) = f'(x) = 2*x . According to the laws of the Fourier transform and tungsten alpha, it must be that G(w) = 2pi*i*F(w) (prefactors can vary, but there should be only a constant factor). When implementing this in python, I write

 import numpy as np def x2(x): return x*x def nx(x): return 2*x a = np.linspace(-3, 3, 16) a1 = x2(a) a2 = nx(a) b1 = np.fft.fft(a1) b2 = np.fft.fft(a2) c = b1/b2 

Now I expect an almost constant value for c , but I get

 array([ 1.02081592e+16+0.j , 1.32769987e-16-1.0054679j , 4.90653893e-17-0.48284271j, -1.28214041e-16-0.29932115j, -1.21430643e-16-0.2j , 5.63664751e-16-0.13363573j, -5.92271642e-17-0.08284271j, -4.21346622e-16-0.03978247j, -5.55111512e-16-0.j , -5.04781597e-16+0.03978247j, -6.29288619e-17+0.08284271j, 8.39500693e-16+0.13363573j, -1.21430643e-16+0.2j , -0.00000000e+00+0.29932115j, -0.00000000e+00+0.48284271j, 1.32769987e-16+1.0054679j ]) 

Where is my mistake, and what can I do to use fft as intended?

+5
source share
1 answer

The above properties relate to the continuous Fourier transform (CFT). What FFT computes is the Discrete Fourier Transform (DFT), which is related to CFT, but not quite equivalent.

It is true that DFT is proportional to CFT under certain conditions: namely, with a sufficient sample of a function that is equal to zero outside the limits of the sample (see, for example, Appendix E of this book ).

None of the conditions are satisfied for the functions that you offer above, so the DFT is not proportional to the CFT, and your numerical results reflect this.


Here is some code that confirms through FFT the relation that interests you, using the corresponding function limited by the range:

 import numpy as np def f(x): return np.exp(-x ** 2) def fprime(x): return -2 * x * f(x) a = np.linspace(-10, 10, 100) a1 = f(a) a2 = fprime(a) b1 = np.fft.fft(a1) b2 = np.fft.fft(a2) omega = 2 * np.pi * np.fft.fftfreq(len(a), a[1] - a[0]) np.allclose(b1 * 1j * omega, b2) # True 
+5
source

Source: https://habr.com/ru/post/1236518/


All Articles