3

While trying to understand Fast Fourier Transform I encountered a problem with the phase. I have broken it down to the simple code below. Calculating one period of a 50Hz sinewave, and applying an fft algorithm:

    fs = 1600;
    dt = 1/fs;
    L  = 32;      
    t=(0:L-1)*dt; 
    signal  = sin(t/0.02*2*pi); 

    Y = fft(signal);
    myAmplitude = abs(Y)/L *2 ;
    myAngle     = angle(Y);

    Amplitude_at_50Hz = myAmplitude(2);
    Phase_at_50Hz     = myAngle(2);

While the amplitude is ok, I don't understand the phase result. Why do I get -pi/2 ? As there is only one pure sinewave, I expected the phase to be 0. Either my math is wrong, or my use of Matlab, or both of them... (A homemade fft gives me the same result. So I guess I am stumbling over my math.)

There is a similar post here: MATLAB FFT Phase plot. However, the suggested 'unwrap' command doesn't solve my problem.

Thanks and best regards,

DanK

Community
  • 1
  • 1
DanK
  • 33
  • 3

3 Answers3

3

The default waveform for an FFT phase angle of zero is a cosine wave which starts and ends in the FFT window at 1.0 (not a sinewave which starts and ends in the FFT window at 0.0, or at its zero crossings.) This is because the common nomenclature is to call the cosine function components of the FFT basis vectors (the complex exponentials) the "real" components. The sine function basis components are called "imaginary", and thus infer a non-zero complex phase.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • Thanks for the hint concerning the default waveform. That's my error. As an electronics engineer I am used to sine, because sampling often starts at the zero crossing of the signal. Here I found similar information: [link] (https://ch.mathworks.com/matlabcentral/newsreader/view_thread/257306?requestedDomain=www.mathworks.com). @Mahsa & Harold: Your answer probably contains the same Information. hotpaw2 broke it down to my math Level. – DanK Aug 11 '16 at 05:38
1

That is what it should be. If you used cosine, you would have found a phase of zero.

Ignoring numerical Fourier transforms for a moment and taking a good old Fourier transform of sin(x), which I am too lazy to walk through, we get a pair of purely imaginary deltas.

As for an intuitive reason, recall that a discrete Fourier transform is averaging a bunch of points along a curve in the complex plane while turning at the angular frequency of the bin you're computing and using the amplitude corresponding to the sample. If you sample a sine curve while turning at its own frequency, the shape you get is a circle centered on the imaginary axis (see below). The average of that is of course going to be right on the imaginary axis.

polar sine is circle

Plot made with wolfram alpha.

harold
  • 61,398
  • 6
  • 86
  • 164
1

Fourier transform of a sine function such as A*sin((2*pi*f)*t) where f is the frequency will yield 2 impulses of magnitude A/2 in the frequency domain at +f and -f where the associated phases are -pi/2 and pi/2 respectively. You can take a look at its proof here: http://mathworld.wolfram.com/FourierTransformSine.html

So the code is working fine.