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.
1 次查看(过去 30 天)
显示 更早的评论
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]);
0 个评论
回答(1 个)
Matt Cohen
2015-7-31
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!