About FFT of cosine function

117 views (last 30 days)
MK kim
MK kim on 6 Aug 2020
Commented: MK kim on 24 Aug 2020
Hi, I have to calculate DFT of g(t)=cos(0.5*t). So I write the code below
>> N=128; %number of sampled time points
>> t=linspace(0,128,N); %time domain
>> Fs=1/(t(2)-t(1)); %sampling frequency
>> Fn=Fs/2; %Nyquist Frequency
>> g=cos(0.5*t); %input signal
>> G=fftshift(fft(g)/N); %Fourier transform of g
>> w=linspace(-Fn,Fn,N)*2*pi; %angular frequency domain
>> subplot(2,1,1)
>> plot(w,abs(G)); % to see amplitude
>> subplot(2,1,2)
>> plot(w,angle(G)); % to see phase
<The result of above code>
In this situation, my question is
1) In amplitude graph, I expected it has to have formation of two Dirac delta function Aδ(w-0.5)+Aδ(w+0.5), i.e., amplitude shoudn't have values at w=/0.5 and w=/-0.5. But it have values. How can I change the code to make this two delta functions?
2) In amplitude graph, I also expected the coordinates of peaks are (-0.5,0.5) and (0.5,0.5), but it is not. How can I change the code to make peaks at this coordinates??
Thank you..!!

Accepted Answer

David Goodmanson
David Goodmanson on 7 Aug 2020
Hi mk,
In order to get just the two sharp peaks you need to have exactly n oscillations in the time domain. Otherwise the wave is cut off partway through an oscillation and does not represent a continuous oscillatory wave.
The code below uses a total time of 4*pi so there are 8 oscillations in the time record. (Also, cos = 1 at t = 0, and exactly n oscillations means that there can't be a repeated point cos = 1 at the end of the time record).
w0 = .5;
T = 4*pi; % total time
N = 1000;
t = linspace(0,T,N+1); % N+1 points, N intervals
t(end) = []; % eliminate repeated point at the end
y = cos(w0*t);
z = fftshift(fft(y)/N);
% fft golden rule: delta_t*delta_w = 2*pi/N
% delta_w = 2*pi/(N*delta_t) = 2*pi/T
delw = 2*pi/T;
w = (-N/2:N/2-1)*delw;
r = [real(z);imag(z)];
stem(w,[real(z);imag(z)]')
xlim([-10 10])
  4 Comments
David Goodmanson
David Goodmanson on 20 Aug 2020
Hello mk,
There is nothing wrong here. The only two nonzero values in the spectrum occur at w = +-(1/2), with amplitude 1//2 and phase 0 (the amplitudes are real). If you look at the stem plot, the phase is zero at w = +-(1/2) as it should be.
For all other values of w, the amplitude is supposed to be zero, but because of usual numercal precision issues the values are actually down around 1e-16 for both the real and imaginary parts. The phase of such a calculaton varies randomly and is meaningless, so the phase plot is actually ok.
If you use sin instead of cos, then the phase at w = +-(1/2) is -+(pi/2) as you can check..
MK kim
MK kim on 24 Aug 2020
Thank you for your help..!!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!