DSPRelated.com
Forums

finding frequencies and their amplitude

Started by aiman April 3, 2008
Eric Jacobsen wrote:
> On Thu, 3 Apr 2008 14:11:20 -0700 (PDT), "Ron N." <rhnlogic@yahoo.com> > wrote: > >> On Apr 3, 12:33 pm, dbell <bellda2...@cox.net> wrote: >>> On Apr 3, 6:20 am, "aiman" <mni...@eleceng.adelaide.edu.au> wrote: >>> >>> >>> >>>> Hello, >>>> I am working with random signal analysis in time domain. If the signal >>>> consists of two different frequencies with two different amplitude, How >>>> can I extract the frequencies and the amplitude. >>>> For example, I try to generate frequencies 50Hz and 70Hz with amplitude 10 >>>> and 5 respectively using MATLAB as follows: >>>> clear >>>> ts=0.001; >>>> t=[0:ts:0.2]; >>>> x1=10*sin(2*pi*50*t); >>>> x2=5*sin(2*pi*70*t); >>>> x=x1+x2; >>>> fs=1/0.001; >>>> N1=256; >>>> X1=abs(fft(x,N1)); >>>> F1=[0:(N1-1)]/N1*fs; >>>> subplot(3,1,1) >>>> plot(t,x) >>>> subplot(3,1,3); >>>> plot(F1,X1,'-x'), axis([0 100]) >>>> Can anybody tell me how to obtain back the magnitude ie(10 and 5) >>>> -aiman- >>> One problem is that you have generated 201 data points but are doing a >>> 256-pt FFT. In MATLAB that means the input data is effectively zero- >>> padded up to length 256 prior to the FFT. So if you are expecting each >>> signal to show up in a single bin as implied by Ron, they aren't going >>> to. >> Eh. Your right. I missed the fact that the OP changed >> the FFT length to something different from the data >> vector length. The OP will find rectangular window >> artifacts and thus have to use some estimation method. > > I missed that as well, but the problem is avoided by using a dft > matched to the vector length instead of an fft. Not sure whether that > fits with the assumptions for the OP or not
Why not an FFT matched to the vector length? Jerry -- Engineering is the art of making what you want from things you can get. &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
On Thu, 03 Apr 2008 20:51:22 -0400, Jerry Avins <jya@ieee.org> wrote:

>Eric Jacobsen wrote: >> On Thu, 3 Apr 2008 14:11:20 -0700 (PDT), "Ron N." <rhnlogic@yahoo.com> >> wrote: >> >>> On Apr 3, 12:33 pm, dbell <bellda2...@cox.net> wrote: >>>> On Apr 3, 6:20 am, "aiman" <mni...@eleceng.adelaide.edu.au> wrote: >>>> >>>> >>>> >>>>> Hello, >>>>> I am working with random signal analysis in time domain. If the signal >>>>> consists of two different frequencies with two different amplitude, How >>>>> can I extract the frequencies and the amplitude. >>>>> For example, I try to generate frequencies 50Hz and 70Hz with amplitude 10 >>>>> and 5 respectively using MATLAB as follows: >>>>> clear >>>>> ts=0.001; >>>>> t=[0:ts:0.2]; >>>>> x1=10*sin(2*pi*50*t); >>>>> x2=5*sin(2*pi*70*t); >>>>> x=x1+x2; >>>>> fs=1/0.001; >>>>> N1=256; >>>>> X1=abs(fft(x,N1)); >>>>> F1=[0:(N1-1)]/N1*fs; >>>>> subplot(3,1,1) >>>>> plot(t,x) >>>>> subplot(3,1,3); >>>>> plot(F1,X1,'-x'), axis([0 100]) >>>>> Can anybody tell me how to obtain back the magnitude ie(10 and 5) >>>>> -aiman- >>>> One problem is that you have generated 201 data points but are doing a >>>> 256-pt FFT. In MATLAB that means the input data is effectively zero- >>>> padded up to length 256 prior to the FFT. So if you are expecting each >>>> signal to show up in a single bin as implied by Ron, they aren't going >>>> to. >>> Eh. Your right. I missed the fact that the OP changed >>> the FFT length to something different from the data >>> vector length. The OP will find rectangular window >>> artifacts and thus have to use some estimation method. >> >> I missed that as well, but the problem is avoided by using a dft >> matched to the vector length instead of an fft. Not sure whether that >> fits with the assumptions for the OP or not > >Why not an FFT matched to the vector length? > >Jerry
If one fits, absolutely. A DFT can always be made to fit without zero-padding, an FFT only when an algorithm is available at the needed length . Eric Jacobsen Minister of Algorithms Abineau Communications http://www.ericjacobsen.org
On Apr 4, 1:02&#4294967295;am, Eric Jacobsen <eric.jacob...@ieee.org> wrote:
> On Thu, 03 Apr 2008 20:51:22 -0400, Jerry Avins <j...@ieee.org> wrote: > >Eric Jacobsen wrote: > >> On Thu, 3 Apr 2008 14:11:20 -0700 (PDT), "Ron N." <rhnlo...@yahoo.com> > >> wrote: > > >>> On Apr 3, 12:33 pm, dbell <bellda2...@cox.net> wrote: > >>>> On Apr 3, 6:20 am, "aiman" <mni...@eleceng.adelaide.edu.au> wrote: > > >>>>> Hello, > >>>>> I am working with random signal analysis in time domain. If the signal > >>>>> consists of two different frequencies with two different amplitude, How > >>>>> can I extract the frequencies and the amplitude. > >>>>> For example, I try to generate frequencies 50Hz and 70Hz with amplitude 10 > >>>>> and 5 respectively using MATLAB as follows: > >>>>> clear > >>>>> ts=0.001; > >>>>> t=[0:ts:0.2]; > >>>>> x1=10*sin(2*pi*50*t); > >>>>> x2=5*sin(2*pi*70*t); > >>>>> x=x1+x2; > >>>>> fs=1/0.001; > >>>>> N1=256; > >>>>> X1=abs(fft(x,N1)); > >>>>> F1=[0:(N1-1)]/N1*fs; > >>>>> subplot(3,1,1) > >>>>> plot(t,x) > >>>>> subplot(3,1,3); > >>>>> plot(F1,X1,'-x'), axis([0 100]) > >>>>> Can anybody tell me how to obtain back the magnitude ie(10 and 5) > >>>>> -aiman- > >>>> One problem is that you have generated 201 data points but are doing a > >>>> 256-pt FFT. In MATLAB that means the input data is effectively zero- > >>>> padded up to length 256 prior to the FFT. So if you are expecting each > >>>> signal to show up in a single bin as implied by Ron, they aren't going > >>>> to. > >>> Eh. &#4294967295;Your right. &#4294967295;I missed the fact that the OP changed > >>> the FFT length to something different from the data > >>> vector length. &#4294967295;The OP will find rectangular window > >>> artifacts and thus have to use some estimation method. > > >> I missed that as well, but the problem is avoided by using a dft > >> matched to the vector length instead of an fft. &#4294967295;Not sure whether that > >> fits with the assumptions for the OP or not > > >Why not an FFT matched to the vector length? > > >Jerry > > If one fits, absolutely. &#4294967295; A DFT can always be made to fit without > zero-padding, an FFT only when an algorithm is available at the needed > length > > . > > Eric Jacobsen > Minister of Algorithms > Abineau Communicationshttp://www.ericjacobsen.org- Hide quoted text - > > - Show quoted text -
Whichever is used, to get the previously mentioned result of the energy for each frequency signal (x1,x2) in 1 positive and 1 negative frequency bin, the input vector length needs to be adjusted to be an integer number of periods for both waveforms, and then use a dft/fft of the vector length. Changing from 201 to 200 samples by removing the last sample, then using a 200-point transform, will work. Dirk
The following document explains things nicely:

http://www.optics.rochester.edu/users/noodles/papers/FFT_scaling.pdf

Turns out MATLAB defines their fft and ifft differently and actually using
ifft() for your fft will produce the correct amplitudes.  This also goes
for calling fft() for executing an ifft.  Strange yes, but this is your
solution.  Note that there is a slight amount of loss, I have not
accounted for that yet.

I've updated your code accordingly.  I've also added some better
functionality that you hopefully will find useful.  Essentially I adjusted
your vector sizes to be of a power of 2, this increases the speed at which
the fft is performed.  Also, I set your frequency axis to -fs/2:fs/N:fs/2
- fs/N;  This allows N freq bins (the size of your fft) to go from the
negative and positive frequency axis.  This is the proper way to display a
signal's spectrum in general.  I also performed an fftshift (or iffshift()
in this case).  This will allow you to properly display the spectrum as I
described above.

Keep in mind here that you will get peaks at the positive and negative
frequencies with the amplitude being split in half.  The signal energy is
split between the positive and negative frequency bands.  So your 10 and 5
amplitude sinusoids will have 5 and 2.5 respectively.  Here's the modified
code.  Hope this was useful...

~Dave Hague
clear all;
ts=0.001;
t=[0:ts:.255];
x1=10*sin(2*pi*50*t);
x2=5*sin(2*pi*70*t);
x=x1 + x2;

fs=1/0.001;
N=256;
f=[-fs/2:fs/N:fs/2 - fs/N];

X=ifft(x,N); 
X=ifftshift(X);

figure;
subplot(3,1,1);
plot(t,x);
xlabel('Time (s)');

subplot(3,1,2);
plot(f,abs(X));
xlabel('Frequency (Hz)');
ylabel('Magnitude of Signal Spectrum');

subplot(3,1,3);
plot(f,angle(X));
xlabel('Frequency (Hz)');
ylabel('Phase of Signal Spectrum');

>Hello, > >I am working with random signal analysis in time domain. If the signal >consists of two different frequencies with two different amplitude, How >can I extract the frequencies and the amplitude. > >For example, I try to generate frequencies 50Hz and 70Hz with amplitude
10
>and 5 respectively using MATLAB as follows: > >clear >ts=0.001; >t=[0:ts:0.2]; >x1=10*sin(2*pi*50*t); >x2=5*sin(2*pi*70*t); >x=x1+x2; > >fs=1/0.001; >N1=256; >X1=abs(fft(x,N1)); >F1=[0:(N1-1)]/N1*fs; > >subplot(3,1,1) >plot(t,x) >subplot(3,1,3); >plot(F1,X1,'-x'), axis([0 100]) > >Can anybody tell me how to obtain back the magnitude ie(10 and 5) >-aiman- > > >
On Apr 5, 12:39&#4294967295;pm, "stewie3142" <david.a.ha...@gmail.com> wrote:
> The following document explains things nicely: > > http://www.optics.rochester.edu/users/noodles/papers/FFT_scaling.pdf > > Turns out MATLAB defines their fft and ifft differently and actually using > ifft() for your fft will produce the correct amplitudes. &#4294967295;This also goes > for calling fft() for executing an ifft. &#4294967295;Strange yes, but this is your > solution. &#4294967295;Note that there is a slight amount of loss, I have not > accounted for that yet. > > I've updated your code accordingly. &#4294967295;I've also added some better > functionality that you hopefully will find useful. &#4294967295;Essentially I adjusted > your vector sizes to be of a power of 2, this increases the speed at which > the fft is performed. &#4294967295;Also, I set your frequency axis to -fs/2:fs/N:fs/2 > - fs/N; &#4294967295;This allows N freq bins (the size of your fft) to go from the > negative and positive frequency axis. &#4294967295;This is the proper way to display a > signal's spectrum in general. &#4294967295;I also performed an fftshift (or iffshift() > in this case). &#4294967295;This will allow you to properly display the spectrum as I > described above. > > Keep in mind here that you will get peaks at the positive and negative > frequencies with the amplitude being split in half. &#4294967295;The signal energy is > split between the positive and negative frequency bands. &#4294967295;So your 10 and 5 > amplitude sinusoids will have 5 and 2.5 respectively. &#4294967295;Here's the modified > code. &#4294967295;Hope this was useful... > > ~Dave Hague > clear all; > ts=0.001; > t=[0:ts:.255]; > x1=10*sin(2*pi*50*t); > x2=5*sin(2*pi*70*t); > x=x1 + x2; > > fs=1/0.001; > N=256; > f=[-fs/2:fs/N:fs/2 - fs/N]; > > X=ifft(x,N); > X=ifftshift(X); > > figure; > subplot(3,1,1); > plot(t,x); > xlabel('Time (s)'); > > subplot(3,1,2); > plot(f,abs(X)); > xlabel('Frequency (Hz)'); > ylabel('Magnitude of Signal Spectrum'); > > subplot(3,1,3); > plot(f,angle(X)); > xlabel('Frequency (Hz)'); > ylabel('Phase of Signal Spectrum'); > > > > > > >Hello, > > >I am working with random signal analysis in time domain. If the signal > >consists of two different frequencies with two different amplitude, How > >can I extract the frequencies and the amplitude. > > >For example, I try to generate frequencies 50Hz and 70Hz with amplitude > 10 > >and 5 respectively using MATLAB as follows: > > >clear > >ts=0.001; > >t=[0:ts:0.2]; > >x1=10*sin(2*pi*50*t); > >x2=5*sin(2*pi*70*t); > >x=x1+x2; > > >fs=1/0.001; > >N1=256; > >X1=abs(fft(x,N1)); > >F1=[0:(N1-1)]/N1*fs; > > >subplot(3,1,1) > >plot(t,x) > >subplot(3,1,3); > >plot(F1,X1,'-x'), axis([0 100]) > > >Can anybody tell me how to obtain back the magnitude ie(10 and 5) > >-aiman-- Hide quoted text - > > - Show quoted text -
Stewie, When you say MATLAB defines the FFT "differently" I assume you mean different from the paper that you presented. However MATLAB defines the FFT exactly like one of the earliest and very widely used books on DSP (O&S, 1975). Your implication that you should know the definition of any FFT routine you are using is exactly correct, since there are different definitions with different scalings and different signs on the complex exponent. The different definitions have been discussed here (comp.dsp) before. You can search for them. I think you will find that the slight amount of loss, that you have "not accounted for" yet, is related to a non-integer number of cycles of the two frequency components fitting in your analysis window, ie. the frequencies fall between bin freqencies. This was discussed earlier in this thread. You should have also noticed the large number of non-zero bins. Same situation. Dirk
Dirk, 

Yes, I should have specified more clearly what I meant by the definition
of fft.  There are many different versions out there, I was more referring
to what MATLAB implements.  I also realized after posting that I had the
loss in amplitude due to the freq bins not lining up and later noticed
that it was brought up in the thread as well.  Guess I was rushing ot post
my comment before heading out for the weekend.  Thanks for bringing that to
my attention though...

Sincerely,

~Stewie (aka Dave)

>On Apr 5, 12:39=A0pm, "stewie3142" <david.a.ha...@gmail.com> wrote: >> The following document explains things nicely: >> >> http://www.optics.rochester.edu/users/noodles/papers/FFT_scaling.pdf >> >> Turns out MATLAB defines their fft and ifft differently and actually
using=
> >> ifft() for your fft will produce the correct amplitudes. =A0This also
goes=
> >> for calling fft() for executing an ifft. =A0Strange yes, but this is
your
>> solution. =A0Note that there is a slight amount of loss, I have not >> accounted for that yet. >> >> I've updated your code accordingly. =A0I've also added some better >> functionality that you hopefully will find useful. =A0Essentially I
adjust=
>ed >> your vector sizes to be of a power of 2, this increases the speed at
which=
> >> the fft is performed. =A0Also, I set your frequency axis to
-fs/2:fs/N:fs/=
>2 >> - fs/N; =A0This allows N freq bins (the size of your fft) to go from
the
>> negative and positive frequency axis. =A0This is the proper way to
display=
> a >> signal's spectrum in general. =A0I also performed an fftshift (or
iffshift=
>() >> in this case). =A0This will allow you to properly display the spectrum
as =
>I >> described above. >> >> Keep in mind here that you will get peaks at the positive and negative >> frequencies with the amplitude being split in half. =A0The signal
energy i=
>s >> split between the positive and negative frequency bands. =A0So your 10
and=
> 5 >> amplitude sinusoids will have 5 and 2.5 respectively. =A0Here's the
modifi=
>ed >> code. =A0Hope this was useful... >> >> ~Dave Hague >> clear all; >> ts=3D0.001; >> t=3D[0:ts:.255]; >> x1=3D10*sin(2*pi*50*t); >> x2=3D5*sin(2*pi*70*t); >> x=3Dx1 + x2; >> >> fs=3D1/0.001; >> N=3D256; >> f=3D[-fs/2:fs/N:fs/2 - fs/N]; >> >> X=3Difft(x,N); >> X=3Difftshift(X); >> >> figure; >> subplot(3,1,1); >> plot(t,x); >> xlabel('Time (s)'); >> >> subplot(3,1,2); >> plot(f,abs(X)); >> xlabel('Frequency (Hz)'); >> ylabel('Magnitude of Signal Spectrum'); >> >> subplot(3,1,3); >> plot(f,angle(X)); >> xlabel('Frequency (Hz)'); >> ylabel('Phase of Signal Spectrum'); >> >> >> >> >> >> >Hello, >> >> >I am working with random signal analysis in time domain. If the
signal
>> >consists of two different frequencies with two different amplitude,
How
>> >can I extract the frequencies and the amplitude. >> >> >For example, I try to generate frequencies 50Hz and 70Hz with
amplitude
>> 10 >> >and 5 respectively using MATLAB as follows: >> >> >clear >> >ts=3D0.001; >> >t=3D[0:ts:0.2]; >> >x1=3D10*sin(2*pi*50*t); >> >x2=3D5*sin(2*pi*70*t); >> >x=3Dx1+x2; >> >> >fs=3D1/0.001; >> >N1=3D256; >> >X1=3Dabs(fft(x,N1)); >> >F1=3D[0:(N1-1)]/N1*fs; >> >> >subplot(3,1,1) >> >plot(t,x) >> >subplot(3,1,3); >> >plot(F1,X1,'-x'), axis([0 100]) >> >> >Can anybody tell me how to obtain back the magnitude ie(10 and 5) >> >-aiman-- Hide quoted text - >> >> - Show quoted text - > >Stewie, > >When you say MATLAB defines the FFT "differently" I assume you mean >different from the paper that you presented. However MATLAB defines >the FFT exactly like one of the earliest and very widely used books on >DSP (O&S, 1975). Your implication that you should know the definition >of any FFT routine you are using is exactly correct, since there are >different definitions with different scalings and different signs on >the complex exponent. The different definitions have been discussed >here (comp.dsp) before. You can search for them. > >I think you will find that the slight amount of loss, that you have >"not accounted for" yet, is related to a non-integer number of cycles >of the two frequency components fitting in your analysis window, ie. >the frequencies fall between bin freqencies. This was discussed >earlier in this thread. You should have also noticed the large number >of non-zero bins. Same situation. > >Dirk >