Plot signal of DFT without using FFT function

4 views (last 30 days)
Nicholas Saint
Nicholas Saint on 4 Nov 2021
Commented: Nicholas Saint on 5 Nov 2021
I looked at so many blogs until I came here and still can't figure it out.
I have a signal given as:
The DFT of that signal is:
The analytical solution for fk is:
Where fk = 100 when k = 67 and 0 otherwise.
Here's what I have:
clc;clear;
N = 715;
k = 0:N-1;
n = k;
fj_func = @(N,n) (100*exp(2i.*pi.*67/N.*k)).*(n>=0 & n<=N-1);
fj = fj_func(N,n);
fk_func = @(N,k) (1/N).*fj.*(exp(-2i.*pi.*k.*n./N)).*(k>=0 & k<=N-1);
fk = fk_func(N,k);
stem(k,abs(fk)/N)
When I use the analytical solution above, I get:
clc;clear;
N = 715;
k = 0:N-1;
n = k;
fk_func = @(k) (100/N).*((1-exp(2i.*pi.*n.*(67-k)))./(1-exp(2i.*pi.*n.*(67-k)./N)));
fk = fk_func(k);
stem(k,abs(fk)/N)
I used the fft function for fj, and it works. My value is at
clc;clear;
N = 715;
k = 0:N-1;
n = k;
fj_func = @(N,k) (100*exp(2i.*pi.*67/N.*k)).*(k>=0 & k<=N-1);
fj = fj_func(N,k);
ft = fft(fj);
stem(k,abs(ft)/N)
I'm not sure what I'm doing wrong. Any ideas would be greatly appreciated. Thanks

Accepted Answer

Paul
Paul on 4 Nov 2021
Edited: Paul on 4 Nov 2021
The first approach to compute DFT directly using the definition can't be correct because there is no summation over n. Try it this way. First off there really isn't a need to define a function to compute fn.
N = 715;
n = 0:(N-1);
fn = 100*exp(2i*pi*67*n/N);
Now define a function to compute the DFT for a given value of k
dftfn = @(k,fn,n,N) (sum(fn .* exp(-1i*2*pi*k*n/N))/N);
Now test for a couple of values of k
dftfn(5,fn,n,N)
ans = -1.9339e-14 - 3.3649e-14i
dftfn(67,fn,n,N)
ans = 1.0000e+02 - 1.4181e-16i
The next step would be to write a loop to evaluate dftfn at the values of k of interest.
Note: there are other alternatives, but I think this is the clearest.
For the second approach, I don't think the closed form expression is correct. There shoudn't be an n in any term.
fk_func = @(k,N) (100/N).*((1-exp(2i.*pi.*(67-k)))./(1-exp(2i.*pi.*(67-k)./N)));
k = 0:(N-1);
fk = fk_func(k,N);
stem(k,abs(fk)) % no need to divide by N, that's already in fk_func
As expected all the reults are tiny, but what happened at k = 67?
stem(k,abs(fk));
xlim([60 70])
We see that there is a gap, because the formula at k = 67 yields NaN because it evaluates to 0/0.
fk_func(67,N)
ans = NaN
  1 Comment
Nicholas Saint
Nicholas Saint on 5 Nov 2021
I completely forgot about the summation which was staring me at the face. Thank you so much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!