I was wondering if there is anyone who would like to help me Plotting FFT using my code. I have done enough work on it, but I am not getting the result I was expecting and at this point, I have no clue what I am doing wrong.

2 views (last 30 days)
Hello friends.
I need help with this Matlab code. In the code, I have a value of Cn, which is just a bunch zeros and ones (N= 1024 IFFT). I am trying to plot the DFT equation, and expected to get 64 peaks within the 100 MHz, but unfortunately I only see one peak at the 0 Hz and its two copies at -100MHz and 100MHz. To make the matter worst, the peak at 0Hz and its copies are not exactly at 0Hz, -100MHz and 100MHz respectively. I was wondering if there is someone here who would like to go through my code and point out to me what is that I am doing wrong. I will really appreciate your help.
Thanks in advance,
Mohamud.
Matlab code
N=1024;
fs=100*10^6;
T=1/fs;
vec=zeros(N,1);
positions=[512 128 192 256 320 384 448 512 576 640 704 768 832 896 960 1024];
vec(positions)=1;
Cn=vec;
one_sided_span = 200*10^6;
num_steps = 500;
step_value = (2*one_sided_span)/num_steps;
f = -one_sided_span:step_value:one_sided_span;
Sf = zeros(1,length(f));
for n=0:N-1
Sf_n = zeros(1,length(f));
for k=0:N-1
Sf_n = Sf_n + exp((-1i*(2*pi*k))*((f/fs)-n/N));
end
Sf = Cn(n+1).*Sf_n;
Sf = Sf + Sf_n;
end
figure(2)
plot(f,20*log10(abs(Sf_n)));
title('Sf in log scale');
xlabel('Frequency(Hz)');
ylabel('Amplitude (dB)');
axis([-2*10^8 2*10^8 -10 40]);
figure(1)
plot(Cn)
title('The values of Cn');
xlabel('NFFT-Points');
ylabel('Amplitude');
axis([0 1050 0 1.5]);

Answers (1)

Matt Cohen
Matt Cohen on 31 Jul 2015
Hi Mohamud,
I understand that you are trying to compute and plot the DFT of the array Cn that contains zeros and ones, and you are expecting to find 64 peaks in the DFT output. Based on the code you provided, it appears that the issue is with the way you are computing the DFT. Fortunately, MATLAB provides the functions "fft" and "ifft" to compute the discrete Fourier Transform and its inverse, respectively, so you should not have to compute it the long way.
The issue in your code is in how you are computing the 'Sf_n' term. You do not need to include (f/fs) in this computation process, and you do not need to vectorize this line since it is only computing a single value in the overall DFT. I have provided example code below that corrects this portion of your code; additionally, I have a commented-out line of code below this computation that would take care of this in one line of code.
N=1024;
fs=100*10^6;
T=1/fs;
vec=zeros(N,1);
positions=[512 128 192 256 320 384 448 512 576 640 704 768 832 896 960 1024];
vec(positions)=1;
Cn=vec;
one_sided_span = 200*10^6;
num_steps = N;
step_value = (2*one_sided_span)/num_steps;
f = -one_sided_span:step_value:one_sided_span-1;
Sf = zeros(1,length(f));
for k=0:N-1
Sf_n = 0;
for n=0:N-1
Sf_n = Sf_n + Cn(n+1)*exp((-1i*(2*pi*k))*(n/N));
end
Sf(k+1) = Sf_n;
end
% DFT computation in one line
%Sf = fft(Cn,N);
figure(1)
plot(Cn)
title('The values of Cn');
xlabel('NFFT-Points');
ylabel('Amplitude');
axis([0 1050 0 1.5]);
figure(2)
plot(f,20*log10(abs(Sf)));
title('Sf in log scale');
xlabel('Frequency(Hz)');
ylabel('Amplitude (dB)');
axis([-2*10^8 2*10^8 -10 40]);
The plot of the DFT shows 64 peaks over the specified frequency range, which corresponds to what you were expecting.
I hope this helps.
Matt
  1 Comment
Mohamud Abdi
Mohamud Abdi on 4 Aug 2015
Hi Matt, Thanks a lot for replying to me sooner than I expected. Actually the peaks I am expecting are Sinc function. I will attach the same problem solved in Mathematica, but I want to do it in Matlab as well. So, with the new code here, I am almost getting there, but my problem is that, the peak that is centred at 0MHz is broken, it is not continuous. If you run the code, you will see what am talking about. My other concern is that, when I multiply with the filter P, which is a raised cosine filter, I don't see my signals, but if I multiply the filter with a gain factor of 1000, I will be able to see the signals. So, I don't know why the filter has to only work when a gain factor is used??
Here is the new code, I am using N=1024 and 16 peaks.
clear all; close all;
N=1024; fs=100*10^6; T=1/fs; a=0.5;
vec=zeros(N,1); positions=[64 128 192 256 320 384 448 512 576 640 704 768 832 896 960 1024];
vec(positions)=1;
Cn=vec;
one_sided_span = 200*10^6; num_steps = 1000; step_value = (2*one_sided_span)/num_steps; f = -one_sided_span:step_value:one_sided_span;
Sf = zeros(1,length(f)); for n=1:N Sf_n = zeros(1,length(f)); for k=0:N-1
Sf_n = Sf_n + exp((-1i*(2*pi*k))*((f/fs)-(n-1)/N));
end
Sf = Cn(n)*Sf_n + Sf;
end
figure(1)
plot(f,20*log10(abs(Sf_n))); title('Sf_n in log scale'); xlabel('Frequency(Hz)'); ylabel('Amplitude (dB)'); axis([-2*10^8 2*10^8 -10 70]); grid on
figure(2)
plot(f,20*log10(abs(Sf))); title('Sf in log scale'); xlabel('Frequency(Hz)'); ylabel('Amplitude (dB)'); axis([-2*10^8 2*10^8 -10 70]); grid on
P =(T)*(abs(f)<((1-a)/(2*T)))+ 0*(abs(f)>=((1+a)/(2*T)))+((T)/2)*((1-sin(pi*T*(abs(f)-1/(2*T))/(a)))).*(((1-a)/(2*T))<=(abs(f))).*((abs(f))<=((1+a)/(2*T))); Sf= Sf.*P/(N);
figure(3) plot(f,P); title('Raised-Cosine-Pulse(Filter)'); axis([-1.5*10^8 1.5*10^8 0 1050]); xlabel('Frequency (Hz)'); ylabel('Amplitude (T)');
figure(4) plot(f,20*log10(abs(Sf))); title('Sf in log scale'); xlabel('Frequency(Hz)'); ylabel('Amplitude (dB)'); axis([-2*10^8 2*10^8 -10 70]); grid on
figure(5) plot(Cn) title('The values of Cn'); xlabel('NFFT-Points'); ylabel('Amplitude'); axis([0 1050 0 1.5]); grid on

Sign in to comment.

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!