plotting of FFT of given signal

Status
Not open for further replies.

suru

Full Member level 3
Joined
Aug 31, 2007
Messages
157
Helped
4
Reputation
14
Reaction score
1
Trophy points
1,298
Activity points
1,934
plz help me . refer the following program,

....main aim. i want to generate 2000hz frequncy and plotting time and frequncy spectrum for the same. i have got all the result but frequncy plot result is not correct.



% Calculate data
n1=2000;
n2=500;
t=0:0.1:50
x = sin(2*pi*n1*t) + sin(2*pi*n2*t);
y = fft(x,512);
m = y.*conj/512;
f = 1000*(0:256)/512;

% Create frequency plot

plot(f,m(1:257))
grid on


response should be correct. i got plot but showing wrong values.
 

there are some parts i don't understand in your program. For example that

Code:
m=y.*conj(y)/512;

i think you also have a little concept error to what sampling freqency concerns... or i just might be mistaken and didn't fully understand what you tried to do. You need at least a sampling frequency 2 times greater than the greatest frequency you are going to sample. So if you want to sample a 2kHz signal, you need a 4kHz sampling frequency for the least, to avoid aliasing.

Anyways, here is an example code of signal sampling and frequency plotting. I suggest you use fftshift() for centering the spectrum, as shown below:

Code:
% frequencies of the signals (F1 and F2)
% and sampling frequency (Fs)
F1=500;
F2=2000;
Fs=4000;

% time range: 0 to 2ms, 1us step
t=0:1e-6:2e-3;
x1=cos(2*pi*F1*t);
x2=cos(2*pi*F2*t);
x3=x1+x2;

hold on;
subplot(3,3,1);
plot(t,x1);
subplot(3,3,4);
plot(t,x2);
subplot(3,3,7);
plot(t,x3);

% number of samples for our signal
n=(1:100);
f1=F1/Fs; % digital freqency is the sampled frequency
f2=F2/Fs;
x1=cos(2*pi*f1*n);
subplot(3,3,2);
stem(n,x1);
x2=cos(2*pi*f2*n);
subplot(3,3,5);
stem(n,x2);
x3=x1+x2;
subplot(3,3,8);
stem(n,x3);

% number of samples for the FFT
N=512;

% we calculate just the magnitude (abs), not the phase
X1=abs(fft(x1,N));
X2=abs(fft(x2,N));
X3=abs(fft(x3,N));

% now shift the spectrum to be centered
X1=fftshift(X1);
X2=fftshift(X2);
X3=fftshift(X3);

% digital frequency range will be from -1/2 to 1/2, so
% there is a need to unnormalize it to our sampling
% frequency, which is Fs.
% this means our freq range will go from -Fs/2 to Fs/2
f=Fs.*[-N/2:N/2-1]/N;

% finally we just plot the positive part of everything
subplot(3,3,3);
plot(f(N/2:end),X1(N/2:end));
subplot(3,3,6);
plot(f(N/2:end),X2(N/2:end));
subplot(3,3,9);
plot(f(N/2:end),X3(N/2:end));

I hope this helps.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…