Prev: Image Upload in GUI
Next: .mdl to m-file
From: Abdullah on 26 May 2010 23:25 Hi there, I am trying to generate sin wave with fo =2000 Hz to obtain a spectrum with 2000 Hz. However, I obtained a spectrum with two peaks one around 0 and another around 2000 Hz. while I am looking for 2000 Hz. the following the script I used. Please Help me if you can? Regards, Abdull clc; clear all Fo=2000; To=1/Fo; t=0:To/50:5*To; xt=sin(2*pi*Fo*t); Lx=length(xt); figure(2) vdata=xt; L=length(xt); % N = 2^nextpow2(L); % Next power of 2 from length of vdata N=1024; X = fft(vdata,N); M =abs(X)/N; f=(0:N-1)*Fo/N; plot(f,M);
From: TideMan on 27 May 2010 00:31 On May 27, 3:25 pm, "Abdullah " <zabdull...(a)hotmail.com> wrote: > Hi there, > > I am trying to generate sin wave with fo =2000 Hz to obtain a spectrum with 2000 Hz. > However, I obtained a spectrum with two peaks one around 0 and another around 2000 Hz. while I am looking for 2000 Hz. the following the script I used. Please Help me if you can? > > Regards, > Abdull > > clc; > clear all > Fo=2000; > To=1/Fo; > t=0:To/50:5*To; > xt=sin(2*pi*Fo*t); > Lx=length(xt); > > figure(2) > vdata=xt; > L=length(xt); > % N = 2^nextpow2(L); % Next power of 2 from length of vdata > N=1024; > X = fft(vdata,N); > M =abs(X)/N; > > f=(0:N-1)*Fo/N; > > plot(f,M); Here is a list of things you've got wrong: 1. What you've plotted is the periodogram, not the spectrum. The spectrum is power spectral density vs frequency (usually plotted log- log). See here: http://en.wikipedia.org/wiki/Periodogram 2. You should only be using half of the Fourier transform abs(X(2:N/ 2)) (start at 2 to avoid the DC component) You can throw away the other half. It adds nothing. 3. You are using the freq of the sine as the sampling freq which it is not, so this line is wrong: f=(0:N-1)*Fo/N; And the sequence needs to go from 1 to N/2-1, otherwise the max freq is twice the Nyquist, which is rubbish. 4. The length of the data is Lx and L (why you define the two of them escapes me), yet you have set N for the FFT as 1024. Why?
From: Walter Roberson on 27 May 2010 00:48 Abdullah wrote: > I am trying to generate sin wave with fo =2000 Hz to obtain a spectrum > with 2000 Hz. However, I obtained a spectrum with two peaks one around 0 > and another around 2000 Hz. while I am looking for 2000 Hz. The first point returned by fft() corresponds to a constant factor. I do not recall at the moment whether that constant is the DC offset or a constant scale multiplier. In either case, the first point should not be considered as a peak.
From: Abdullah on 27 May 2010 01:09 TideMan <mulgor(a)gmail.com> wrote in message <3c83ddc2-47bd-4ca0-8585-619ddf986aa1(a)s4g2000prh.googlegroups.com>... > On May 27, 3:25 pm, "Abdullah " <zabdull...(a)hotmail.com> wrote: > > Hi there, > > > > I am trying to generate sin wave with fo =2000 Hz to obtain a spectrum with 2000 Hz. > > However, I obtained a spectrum with two peaks one around 0 and another around 2000 Hz. while I am looking for 2000 Hz. the following the script I used. Please Help me if you can? > > > > Regards, > > Abdull > > > > clc; > > clear all > > Fo=2000; > > To=1/Fo; > > t=0:To/50:5*To; > > xt=sin(2*pi*Fo*t); > > Lx=length(xt); > > > > figure(2) > > vdata=xt; > > L=length(xt); > > % N = 2^nextpow2(L); % Next power of 2 from length of vdata > > N=1024; > > X = fft(vdata,N); > > M =abs(X)/N; > > > > f=(0:N-1)*Fo/N; > > > > plot(f,M); > > Here is a list of things you've got wrong: > 1. What you've plotted is the periodogram, not the spectrum. The > spectrum is power spectral density vs frequency (usually plotted log- > log). See here: http://en.wikipedia.org/wiki/Periodogram > 2. You should only be using half of the Fourier transform abs(X(2:N/ > 2)) (start at 2 to avoid the DC component) > You can throw away the other half. It adds nothing. > 3. You are using the freq of the sine as the sampling freq which it is > not, so this line is wrong: > f=(0:N-1)*Fo/N; > And the sequence needs to go from 1 to N/2-1, otherwise the max freq > is twice the Nyquist, which is rubbish. > 4. The length of the data is Lx and L (why you define the two of them > escapes me), yet you have set N for the FFT as 1024. Why? > > I used the followed parameters as suggested and obtain different spectrum. this time no 2000 Hz. figure(2) N = 2^nextpow2(L); % Next power of 2 from length of vdata X = fft(xt,N); M =abs(X(2:N/2)); f=1:N/2-1; plot(f,M); Regards and thanks
From: Abdullah on 27 May 2010 01:56
hi , Strange!! this time instead of obtaining a spectrum for the signal of 1.5 Mhz and obtaining 3 Mhz even (2 I get 4 MHz) strange!!. I used the suggested script (Tideman) , please help me ?? clc; clear all Fo=10e6; To=1/Fo; t=0:To/50:5*To; xt=sin(2*pi*150000000*t); L=length(xt); figure(1); subplot(311); plot(t,xt,'r');grid; xlabel('Cosine Signal xt = cos(4000*pi*t)'); figure(2) N = 2^nextpow2(L); % Next power of 2 from length of vdata X = fft(xt,N); M =abs(X(2:N/2)); f=(1:N/2-1)*Fo/N; plot(f,M); Regards and Thanks |