Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
FFT & Phase determination

Subject: FFT & Phase determination

From: Muhammad Irfan Zafar

Date: 3 Apr, 2014 08:58:10

Message: 1 of 15

Hi.

I have been trying to calculate FFT for a Limit Cylce Oscillation and phase angle corresponding to the frequency value of maximum amplitude.

I have tried to take sample that starts and ends at zero(10e-12). Sampling frequency is 1000. Signal frequency is 8 Hz. There is a repetition of cycles and error between consecutive cycles is around 10e-11.
I have calculated frequency and phase angle from two samples; one with 500 steps and other with 1000 steps.Corresponding results are:

For 500 steps:
Freq. = 7.813
Ampl. = 0.0028
Phase angle = 1.4554


For 1000 steps:
Freq. = 7.813
Ampl. = 0.0027
Phase angle = 1.7483

Amplitude difference could be acceptable, but i don't know why there is a difference in phase values. Can somebody elaborate?

CODE:

M=dlmread('Rigid.Forces.log');

My=M(501:1001,6);

subplot(212);
plot(My),grid on;

Fs=1000;
L=length(My);
NFFT=2^nextpow2(L);

y=fft(My,NFFT)/L;
f=Fs/2.*linspace(0,1,NFFT/2+1);
Y= 2*abs(y(1:NFFT/2+1));

figure;
subplot(211);
plot(f,Y), grid on;

Yt=Y(3:NFFT/2+1,1);
[mx,indx]=max(Yt)
                                          
phase=angle(y);

subplot(212)
plot(f,phase(1:NFFT/2+1)), grid on;

Phi=phase(indx+2)

 

Subject: FFT & Phase determination

From: Nasser M. Abbasi

Date: 3 Apr, 2014 13:08:00

Message: 2 of 15

On 4/3/2014 3:58 AM, Muhammad Irfan Zafar wrote:
> Hi.
>

<DSP question>

You can try asking on comp.dsp
  

Subject: FFT & Phase determination

From: Muhammad Irfan Zafar

Date: 3 Apr, 2014 13:22:08

Message: 3 of 15

"Nasser M. Abbasi" wrote in message <lhjmfb$l67$1@speranza.aioe.org>...
>
> You can try asking on comp.dsp
>


Thanks. I have done it now.

Subject: FFT & Phase determination

From: dpb

Date: 3 Apr, 2014 13:50:16

Message: 4 of 15

On 4/3/2014 3:58 AM, Muhammad Irfan Zafar wrote:
> Hi.
>
> I have been trying to calculate FFT for a Limit Cylce Oscillation and
> phase angle corresponding to the frequency value of maximum amplitude.
>
> I have tried to take sample that starts and ends at zero(10e-12).
> Sampling frequency is 1000. Signal frequency is 8 Hz. There is a
> repetition of cycles and error between consecutive cycles is around 10e-11.
> I have calculated frequency and phase angle from two samples; one with
> 500 steps and other with 1000 steps.Corresponding results are:
>
...

> Amplitude difference could be acceptable, but i don't know why there is
> a difference in phase values. Can somebody elaborate?
>
> CODE:
>
...

> My=M(501:1001,6);
> L=length(My); NFFT=2^nextpow2(L);
> y=fft(My,NFFT)/L;
...

You've zero-padded with two different lengths...

--

Subject: FFT & Phase determination

From: Muhammad Irfan Zafar

Date: 3 Apr, 2014 14:06:11

Message: 5 of 15

dpb <none@non.net> wrote in message <lhjout$sa6$1@speranza.aioe.org>...
> On 4/3/2014 3:58 AM, Muhammad Irfan Zafar wrote:
> > Hi.
> >
> > I have been trying to calculate FFT for a Limit Cylce Oscillation and
>> ...
>
>
> You've zero-padded with two different lengths...
>
>

The response gets three time during 500 sample and seven times during 1000. It's basically repetition of same cycle after every 125 steps.

Question was that why if we take more number of same cycles together, values we obtain for amplitude and phase are different?

Subject: FFT & Phase determination

From: dpb

Date: 3 Apr, 2014 14:08:27

Message: 6 of 15

On 4/3/2014 9:06 AM, Muhammad Irfan Zafar wrote:
> dpb <none@non.net> wrote in message <lhjout$sa6$1@speranza.aioe.org>...
>> On 4/3/2014 3:58 AM, Muhammad Irfan Zafar wrote:
>> > Hi.
>> >
>> > I have been trying to calculate FFT for a Limit Cylce Oscillation and
>>> ...
>>
>>
>> You've zero-padded with two different lengths...
>>
>>
>
> The response gets three time during 500 sample and seven times during
> 1000. It's basically repetition of same cycle after every 125 steps.
>
> Question was that why if we take more number of same cycles together,
> values we obtain for amplitude and phase are different?

Because what you did the fft() on wasn't _just_ the sampled waveform but
the zero-padded one...try w/ N=length(signal) instead

--

Subject: FFT & Phase determination

From: Muhammad Irfan Zafar

Date: 3 Apr, 2014 14:29:08

Message: 7 of 15

dpb <none@non.net> wrote in message <lhjq0v$u9e$1@speranza.aioe.org>...

> Because what you did the fft() on wasn't _just_ the sampled waveform but
> the zero-padded one...try w/ N=length(signal) instead
>
> --

Is there anyway i can show the plot of response?

I am not zero-padding it. It's the actual response. I am just taking it for certain interval of time. Is it zero padding? Correct me if i am wrong.

Subject: FFT & Phase determination

From: dpb

Date: 3 Apr, 2014 14:50:16

Message: 8 of 15

On 4/3/2014 9:29 AM, Muhammad Irfan Zafar wrote:
> dpb <none@non.net> wrote in message <lhjq0v$u9e$1@speranza.aioe.org>...
>
>> Because what you did the fft() on wasn't _just_ the sampled waveform
>> but the zero-padded one...try w/ N=length(signal) instead
>>
>> --
>
> Is there anyway i can show the plot of response?
>
> I am not zero-padding it. It's the actual response. I am just taking it
> for certain interval of time. Is it zero padding? Correct me if i am wrong.

Yes you _are_ zero-padding when you set N

 From

help fft--

fft(X,N) is the N-point fft, padded with zeros if X has less
     than N points and truncated if it has more.

For your case above

My=M(501:1001,6);
L=length(My);
NFFT=2^nextpow2(L);
y=fft(My,NFFT)/L;

we find

 >> length(501:1001)
ans =
    501
 >> 2^nextpow2(ans)
ans =
    512
 >>

So, in the first case you've padded 11 zeros to the end, in the other
it'll be 23 presuming you also have the off-by-one issue in the actual
number of point selected (501:1001 should be 501:1000).

Also, your normalization is inconsistent in you divided by L instead of
NFFT although it'll be fairly small.

--

Subject: FFT & Phase determination

From: Muhammad Irfan Zafar

Date: 3 Apr, 2014 15:06:11

Message: 9 of 15

dpb <none@non.net> wrote in message <lhjsfd$6a2$1@speranza.aioe.org>...
>
> Yes you _are_ zero-padding when you set N
>
> From
>
> help fft--
>
> fft(X,N) is the N-point fft, padded with zeros if X has less
> than N points and truncated if it has more.
>
> For your case above
>
> My=M(501:1001,6);
> L=length(My);
> NFFT=2^nextpow2(L);
> y=fft(My,NFFT)/L;
>
> we find
>
> >> length(501:1001)
> ans =
> 501
> >> 2^nextpow2(ans)
> ans =
> 512
> >>
>
> So, in the first case you've padded 11 zeros to the end, in the other
> it'll be 23 presuming you also have the off-by-one issue in the actual
> number of point selected (501:1001 should be 501:1000).
>
> Also, your normalization is inconsistent in you divided by L instead of
> NFFT although it'll be fairly small.
>
> --


Thanks, I have corrected both mistakes you asked about above. now the ranges are [501:1012] and [501:1524], but phase difference is still there.

Now the phase for first case is 1.4937 and for second case is 1.7999. I have tried to alter many things but it isn't working.

Subject: FFT & Phase determination

From: dpb

Date: 3 Apr, 2014 17:01:30

Message: 10 of 15

On 4/3/2014 10:06 AM, Muhammad Irfan Zafar wrote:
> dpb <none@non.net> wrote in message <lhjsfd$6a2$1@speranza.aioe.org>...
>>
>> Yes you _are_ zero-padding when you set N
>>
>> From
>>
>> help fft--
>>
>> fft(X,N) is the N-point fft, padded with zeros if X has less
>> than N points and truncated if it has more.
>>
>> For your case above
>>
>> My=M(501:1001,6);
>> L=length(My);
>> NFFT=2^nextpow2(L);
>> y=fft(My,NFFT)/L;
>>
>> we find
>>
>> >> length(501:1001)
>> ans =
>> 501
>> >> 2^nextpow2(ans)
>> ans =
>> 512
>> >>
>>
>> So, in the first case you've padded 11 zeros to the end, in the other
>> it'll be 23 presuming you also have the off-by-one issue in the actual
>> number of point selected (501:1001 should be 501:1000).
>>
>> Also, your normalization is inconsistent in you divided by L instead
>> of NFFT although it'll be fairly small.
>>
>> --
>
>
> Thanks, I have corrected both mistakes you asked about above. now the
> ranges are [501:1012] and [501:1524], but phase difference is still there.
>
> Now the phase for first case is 1.4937 and for second case is 1.7999. I
> have tried to alter many things but it isn't working.

Would need some data to see why, precisely. I also note you didn't
window the signal first...

--

Subject: FFT & Phase determination

From: TideMan

Date: 3 Apr, 2014 19:31:13

Message: 11 of 15

On Friday, April 4, 2014 4:06:11 AM UTC+13, Muhammad Irfan Zafar wrote:
> dpb <none@non.net> wrote in message <lhjsfd$6a2$1@speranza.aioe.org>...
>
> >
>
> > Yes you _are_ zero-padding when you set N
>
> >
>
> > From
>
> >
>
> > help fft--
>
> >
>
> > fft(X,N) is the N-point fft, padded with zeros if X has less
>
> > than N points and truncated if it has more.
>
> >
>
> > For your case above
>
> >
>
> > My=M(501:1001,6);
>
> > L=length(My);
>
> > NFFT=2^nextpow2(L);
>
> > y=fft(My,NFFT)/L;
>
> >
>
> > we find
>
> >
>
> > >> length(501:1001)
>
> > ans =
>
> > 501
>
> > >> 2^nextpow2(ans)
>
> > ans =
>
> > 512
>
> > >>
>
> >
>
> > So, in the first case you've padded 11 zeros to the end, in the other
>
> > it'll be 23 presuming you also have the off-by-one issue in the actual
>
> > number of point selected (501:1001 should be 501:1000).
>
> >
>
> > Also, your normalization is inconsistent in you divided by L instead of
>
> > NFFT although it'll be fairly small.
>
> >
>
> > --
>
>
>
>
>
> Thanks, I have corrected both mistakes you asked about above. now the ranges are [501:1012] and [501:1524], but phase difference is still there.
>
>
>
> Now the phase for first case is 1.4937 and for second case is 1.7999. I have tried to alter many things but it isn't working.

I'm joining this thread late, but if you're interested in the phase, you shouldn't be zero-padding at all.
AKAIK, when you zero pad, the routine adds zeros to the beginning and end, meaning that the phase will change depending on the amount of padding.
With modern FFTs, there's no need to use dyadic numbers.
Just use the actual number of data:
y=fft(My)/L;

Subject: FFT & Phase determination

From: Muhammad Irfan Zafar

Date: 4 Apr, 2014 10:10:12

Message: 12 of 15

dpb <none@non.net> wrote in message <lhk45e$r60$1@speranza.aioe.org>...
> On 4/3/2014 10:06 AM, Muhammad Irfan Zafar wrote:
>
> Would need some data to see why, precisely. I also note you didn't
> window the signal first...
>
> --

@dpb
how can i send you data? what type of window do you recommend? will it make much difference?

Subject: FFT & Phase determination

From: Muhammad Irfan Zafar

Date: 4 Apr, 2014 10:16:10

Message: 13 of 15

TideMan <mulgor@gmail.com> wrote in message <8bc7f675-b938-414c-82d9-19d405851a4b@googlegroups.com>...
> On Friday, April 4, 2014 4:06:11 AM UTC+13, Muhammad Irfan Zafar wrote:
>
> I'm joining this thread late, but if you're interested in the phase, you shouldn't be zero-padding at all.
> AKAIK, when you zero pad, the routine adds zeros to the beginning and end, meaning that the phase will change depending on the amount of padding.
> With modern FFTs, there's no need to use dyadic numbers.
> Just use the actual number of data:
> y=fft(My)/L;


@TideMan
Points noted but they also doesn't change things.

Subject: FFT & Phase determination

From: dpb

Date: 4 Apr, 2014 13:40:38

Message: 14 of 15

On 4/4/2014 5:10 AM, Muhammad Irfan Zafar wrote:
> dpb <none@non.net> wrote in message <lhk45e$r60$1@speranza.aioe.org>...
>> On 4/3/2014 10:06 AM, Muhammad Irfan Zafar wrote:
>>
>> Would need some data to see why, precisely. I also note you didn't
>> window the signal first...
>>
>> --
>
> @dpb
> how can i send you data? what type of window do you recommend? will it
> make much difference?

Are the two signals _exactly_ commensurate inside the windows (ie, if
you plot two of those windows end-to-end is there no discontinuity at
all between the two)? If not, then yes, windowing will make a
difference. If there is any, then that discontinuity introduces
artifacts into the DFT just as if it were real.

--

Subject: FFT & Phase determination

From: Muhammad Irfan Zafar

Date: 11 Apr, 2014 16:19:08

Message: 15 of 15

dpb <none@non.net> wrote in message <lhmcor$4k8$1@speranza.aioe.org>...
> Are the two signals _exactly_ commensurate inside the windows (ie, if
> you plot two of those windows end-to-end is there no discontinuity at
> all between the two)? If not, then yes, windowing will make a
> difference. If there is any, then that discontinuity introduces
> artifacts into the DFT just as if it were real.
>
> --



Thanks all. I got the problem corrected few days back. Today i thought i should mention it here for future help to anyone.

What i have observed is that using NFFT in evaluating FFT doesn't help the phase values, as 'dpb' mentioned earlier with zero-padding. For getting good phase plot, best way is to use simple fft command without using NFFT. Also take exact number of cycles. Try to start and end your sample at zero. Try to add couple of cycles in one sample although these cycles may be exact repetition of themselves.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us