DSPRelated.com
Forums

problem with fft and actual time (matlab)

Started by BillJosephson November 24, 2006
Hi, for the following code in matlab:

t = (0:0.00048828:1);
y  = sin(2*pi*12*t);
z = abs(fft(y));
power = z(12)

which I hope produces a second of data for a sine wave at 12 Hz,
sampled at 2048 per second, and 'power' is equal to the power of
12 Hz.

Unfortunately,

figure; plot(z)

shows clearly that the peak is at 13, instead of 12, Hz.

Also, if I try 10 seconds of data,

t = (0:0.00048828:10);
y  = sin(2*pi*12*t);
z = abs(fft(y));
power = z(12)

now plotting z clearly shows the power peak at 121. I
tried varying the middle term of t (.00048828) by an
order of magnitude either way, and no change.

Matlab's help just doesn't have enough explanation or
examples for me to understand the function of and
relationship between the terms of t, or how to relate
them to real time.

I guess my real problem is I don't understand how to
interpret from the above code the x-axis in the resulting
fft. Is it frequency expressed in samples/second?
Or samples/(third term of t)?

I want to understand how to generate arbitrarily long
sine waves with sampling rate X, and when I do an fft,
see a power peak at X.

Thanks very much in advance for any assistance...

Hello Bill,

The resulting fft-field in matlab is the same as in any math book.
In you case the lowes frequency is 1Hz.
The x-axis for the result is
[0Hz, 1Hz, 2Hz, .... , half of sampling frequency, ...., 2Hz, 1Hz];
So it's a little bit unsymmetric.
One part of fft's result is symmetric to the half of sampling frequency.
The other part is also symmetric but sign reversed.
One part is the cosin-part of your signal
and the other part is the sin-part of your signal.

    Hope that helps a little bit.
    May Bronstein-Semendiajev is near anywhere ?

            Regards, Wolfgang


No Never wrote:
> Hello Bill, > > The resulting fft-field in matlab is the same as in any math book. > In you case the lowes frequency is 1Hz. > The x-axis for the result is > [0Hz, 1Hz, 2Hz, .... , half of sampling frequency, ...., 2Hz, 1Hz]; > So it's a little bit unsymmetric. > One part of fft's result is symmetric to the half of sampling frequency. > The other part is also symmetric but sign reversed. > One part is the cosin-part of your signal > and the other part is the sin-part of your signal. > > Hope that helps a little bit. > May Bronstein-Semendiajev is near anywhere ? > > Regards, Wolfgang
Thank you very much, Wolfgang. Does this mean that in the vector containing the fft result, the first number is 0 hz, the second is 1 hz, etc., so that if I want the power at 12 hz, I just need result(13)? It seems this also means we can only see the power up to the Nyquist frequency, since second part mirrors the first (the result is the same length as the sampling rate in my experience regardless of signal length). Thanks, can't tell you how badly I need the help :-)
BillJosephson wrote:
> No Never wrote: >> Hello Bill, >> >> The resulting fft-field in matlab is the same as in any math book. >> In you case the lowes frequency is 1Hz. >> The x-axis for the result is >> [0Hz, 1Hz, 2Hz, .... , half of sampling frequency, ...., 2Hz, 1Hz]; >> So it's a little bit unsymmetric. >> One part of fft's result is symmetric to the half of sampling frequency. >> The other part is also symmetric but sign reversed. >> One part is the cosin-part of your signal >> and the other part is the sin-part of your signal. >> >> Hope that helps a little bit. >> May Bronstein-Semendiajev is near anywhere ? >> >> Regards, Wolfgang > > > Thank you very much, Wolfgang. > > Does this mean that in the vector containing the fft result, the first > number is 0 hz, the second is 1 hz, etc., so that if I want the power > at 12 hz, I just need result(13)? >
Yes, that's right. Assuming that your FFT is scaled correctly (one Hz per bin), the power at 12Hz will be in bin 12 (starting from zero), or result(13) in MATLAB.
> It seems this also means we can only see the power up to the Nyquist > frequency, since second part mirrors the first (the result is the same > length as the sampling rate in my experience regardless of signal > length). >
Of course. If your signal is purely real, the second half of the FFT results will be a mirror of the first. Frequencies above the nyquist rate will be aliased into the lower frequency bins.
> Thanks, can't tell you how badly I need the help :-) >
Hello Bill,

> Does this mean that in the vector containing the fft result, the first > number is 0 hz, the second is 1 hz, etc., so that if I want the power > at 12 hz, I just need result(13)?
I step gently: "Power" has a special meaning. You've only numbers. sin(2*pi*12*t) could also be the lengh of a shadow of a rotating stick. I wouldn't say this shadow could have any "Power". But when you're interested in the amplitude of your signal don't forget to devide z(12) by n or n/2 (n=length of array/number of samples). I forgot which factor is right but you can test with your sin signal.
> It seems this also means we can only see the power up to the Nyquist > frequency, since second part mirrors the first (the result is the same > length as the sampling rate in my experience regardless of signal > length).
Must not be "power", but that's right. Regards, Wolfgang
Marc Brooker wrote:
> BillJosephson wrote: > > No Never wrote: > >> Hello Bill, > >> > >> The resulting fft-field in matlab is the same as in any math book. > >> In you case the lowes frequency is 1Hz. > >> The x-axis for the result is > >> [0Hz, 1Hz, 2Hz, .... , half of sampling frequency, ...., 2Hz, 1Hz]; > >> So it's a little bit unsymmetric. > >> One part of fft's result is symmetric to the half of sampling frequency. > >> The other part is also symmetric but sign reversed. > >> One part is the cosin-part of your signal > >> and the other part is the sin-part of your signal. > >> > >> Hope that helps a little bit. > >> May Bronstein-Semendiajev is near anywhere ? > >> > >> Regards, Wolfgang > > > > > > Thank you very much, Wolfgang. > > > > Does this mean that in the vector containing the fft result, the first > > number is 0 hz, the second is 1 hz, etc., so that if I want the power > > at 12 hz, I just need result(13)? > > > > Yes, that's right. Assuming that your FFT is scaled correctly (one Hz > per bin), the power at 12Hz will be in bin 12 (starting from zero), or > result(13) in MATLAB.
Thanks, but I think that relates back to my original question. How do I know if it's 1 Hz per bin? Thanks...
BillJosephson wrote:
> Hi, for the following code in matlab: > > t = (0:0.00048828:1); > y = sin(2*pi*12*t); > z = abs(fft(y)); > power = z(12) > > which I hope produces a second of data for a sine wave at 12 Hz, > sampled at 2048 per second, and 'power' is equal to the power of > 12 Hz. > > Unfortunately, > > figure; plot(z) > > shows clearly that the peak is at 13, instead of 12, Hz.
Matlab indexing strikes again! It's off by one. Bin 1 is DC, and bin 13 is 12 Hz. Rest easy. Jerry -- Engineering is the art of making what you want from things you can get. �����������������������������������������������������������������������
Jerry Avins wrote:
> BillJosephson wrote: > > Hi, for the following code in matlab: > > > > t = (0:0.00048828:1); > > y = sin(2*pi*12*t); > > z = abs(fft(y)); > > power = z(12) > > > > which I hope produces a second of data for a sine wave at 12 Hz, > > sampled at 2048 per second, and 'power' is equal to the power of > > 12 Hz. > > > > Unfortunately, > > > > figure; plot(z) > > > > shows clearly that the peak is at 13, instead of 12, Hz. > > Matlab indexing strikes again! It's off by one.
of course not! MATLAB is not off by one, only the users of MATLAB are off by one.
> Bin 1 is DC, and bin 13 is 12 Hz. Rest easy.
sure, why not? since we can rest assured that: " MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation." and " MATLAB integrates numerical analysis, matrix computation, signal processing, and graphics in an easy-to-use environment where problems and solutions are expressed just as they are written mathematically. " since it says that in the manuals, it's gotta be true, and if anybody is off by one, it certainly cannot be MATLAB. r b-j
BillJosephson wrote:
> Marc Brooker wrote: > > BillJosephson wrote: > > > No Never wrote: > > >> Hello Bill, > > >> > > >> The resulting fft-field in matlab is the same as in any math book. > > >> In you case the lowes frequency is 1Hz. > > >> The x-axis for the result is > > >> [0Hz, 1Hz, 2Hz, .... , half of sampling frequency, ...., 2Hz, 1Hz]; > > >> So it's a little bit unsymmetric. > > >> One part of fft's result is symmetric to the half of sampling frequency. > > >> The other part is also symmetric but sign reversed. > > >> One part is the cosin-part of your signal > > >> and the other part is the sin-part of your signal. > > >> > > >> Hope that helps a little bit. > > >> May Bronstein-Semendiajev is near anywhere ? > > >> > > >> Regards, Wolfgang > > > > > > > > > Thank you very much, Wolfgang. > > > > > > Does this mean that in the vector containing the fft result, the first > > > number is 0 hz, the second is 1 hz, etc., so that if I want the power > > > at 12 hz, I just need result(13)? > > > > > > > Yes, that's right. Assuming that your FFT is scaled correctly (one Hz > > per bin), the power at 12Hz will be in bin 12 (starting from zero), or > > result(13) in MATLAB. > > > Thanks, but I think that relates back to my original question. How do I > know if it's 1 Hz per bin?
only if you know that the length of your data that goes into the FFT is exactly as long as 1 second. r b-j