I have a cosine function - Hence it is even. Considering only the real parts of the DFT, on performing DFT, I am getting something like this. Could anybody tell me where I am going wrong: Cos wave


enter image description here

Please let me where I am going wrong with the implementation


Now assume you have a signal x[n], that is even on the paper. Say it spans from n=-L to n=L (it has a total of N= 2L+1 samples). Now when you introduce this signal to matlab for its N-point DFT computation via FFT, matlab will assume this signal to begin from index n=0 to end at n= N-1 =2L. Therefore we can think of this new signal as x2[n] = x[n-L] a time shifted version of the original signal on the paper. (ok, it is actually a circular shift, but intuitively this is satisfying)

Now, from DFT time circuar shift property, you know that $X2[k] = e^{-j2\pi Lk/(2L+1)} X[k]$
This X2[k] is what you are seeing on Matlab, but what you compute on the paper is X[k] and it is readily related to X2[k] from above as $X[k] = e^{j2\pi Lk/(2L+1)} X2[k]$

Finaly it is this signal X[k] that I guess you were looking for, In other words, DFT of a real and even signals being real. See the sample matlab code below:

L = 127;
N= 2*L+1;
x = cos(2*pi*12*n/1024); % this signal is even on the paper;)
X2 = fft(x,N);  % but its DFT is actually DFT of x2[n]
X = exp(j*2*pi*[0:N-1]*L/(2*L+1)).*X;  % lets recover X[k] from X2[k]

figure, stem(n,x);
figure,plot(real(X2)); % 
figure,plot(real(X));  % 
figure,plot(imag(X2)); % Here1, X2 has imaginary part?...
figure,plot(imag(X));  % now it is ok, X[k] is a real.
figure,stem(real(ifft(X2))) % reconstruct back signal from DFT X2[k]
figure,stem(real(ifft(exp(-j*2*pi*[0:N-1]*(L)/(2*L+1)).*X))) % do it from X[k]
| improve this answer | |

The real question is: what did you expect to see and why? I give you a few ideas and hints to help you understand the problem:

  • if you use the time domain data as shown in the top figure, then the first (left-most) point is interpreted as time index $n=0$ (there goes the cosine ...). Just look at the formula for the DFT and you'll see why.
  • Second, the implicit periodic continuation of the time domain data in the DFT means that you don't have a pure sinusoid (just append the first data point after the last data point and so on, and you'll see the problem). What you probably wanted is an integer number of periods in your time window (= DFT length), because then the periodic continuation will not change your nice sinusoid.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.