Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy.fft() what is the return value amplitude + phase shift OR angle?

Tags:

python

numpy

fft

The np.fft.fft() returns a complex array .... what is the meaning of the complex number ? I suppose the real part is the amplitude ! The imaginary part is phase-shift ? phase-angle ? Or something else !

I figured out the position in the array represent the frequency.

like image 878
sten Avatar asked Feb 19 '15 22:02

sten


3 Answers

It's not really a programming question, and is not specific to numpy. Briefly, the absolute value of the complex number (sqrt(x.real**2 + x.imag**2), or numpy.abs()) is the amplitude.

More detailed, when you apply FFT to an array X (which, say, contains a number of samples of a function X(t) at different values of t), you try to represent it as a sum of "plane waves" exp(i w t) (where i is an imaginary unit, and w is a real-valued frequency) with different values of w. That is, you want something like

X = A exp(i w1 t) + B exp(i w2 t) + ...

An FFT returns you these coefficients A, B etc corresponding to some fixed frequencies w1, w2 etc (in numpy, you can get their values from fftfreq()).

Now, these coefficients are, in general, complex. A complex number A can be represented as a combination of "amplitude" and "phase" as:

A = r exp(i p)

where r (== numpy.abs(A)) is the amplitude, and p (== numpy.angle(A)) is the phase, both real values. If you substitute it into the term in the FFT expansion, you get

r exp(i p) exp(i w t) == r exp(i (w t + p))

So, the amplitude r changes the absolute value of the term, and the phase p, well, shifts the phase. Therefore, in order to get the array of amplitudes from the result of an FFT, you need to apply numpy.abs to it.


But I would really suggest you to read something on FFT theory, there's plenty of information around, for instance wiki.

like image 199
fjarri Avatar answered Oct 19 '22 21:10

fjarri


The array of values you obtain is, as for any DFT implementation I am aware of, an array of complex numbers. A complex number has a norm, which corresponds to the amplitude. And it has an angle in the complex plane (sometimes called argument), according to the real and imaginary parts. This angle corresponds to the phase. The complex plane (from Wolfram docs):

enter image description here

So, your array contains x and y, the real and imaginary parts. You are interested in the angle theta. It can be calculated like so:

tan(theta) = y/x

theta = arctan(y/x)

This yields the angle in radians. You might also want to have a look at numpy.angle().

like image 10
Dr. Jan-Philip Gehrcke Avatar answered Oct 19 '22 20:10

Dr. Jan-Philip Gehrcke


The magnitude, r, at a given frequency represents the amount of that frequency in the original signal. The complex argument represents the phase angle, theta.

x + i*y = r * exp(i*theta)

Where x and y are the numbers that that the numpy FFT returns.

like image 3
wrdeman Avatar answered Oct 19 '22 21:10

wrdeman