About FFT of a wave

3 views (last 30 days)
Daniel Wanesh on 30 Dec 2021
Commented: William Rose on 4 Jan 2022
The code below is what I have to create a cos wave in the time domain. I was just wondering how I could go about using the fft function to convert it to the frequency domain.
Fm = 2*10^6;
Em = 4;
Wm = 2*pi*Fm;
t = 0 : 5.5*10^-7 : 1*10^-4;
Vm = Em.*cos(Wm*t);
subplot(3,1,1);
xm = t;
ym = Vm;
plot(xm,ym);
Many thanks in advance,
Daniel
2 CommentsShowHide 1 older comment
William Rose on 4 Jan 2022
You're welcome, @Daniel Wanesh. Good luck with your work!

William Rose on 30 Dec 2021
Edited: William Rose on 30 Dec 2021
Fm = 2*10^6; Em = 4;
t = 0 : 5.5*10^-7 : 1*10^-4;
ym = Em.*cos(2*pi*Fm*t);
Ym=fft(ym); %compute the discrete Fourier transform by the FFT algorithm
Ym() is a complex array. To plot the magnitudes:
plot(abs(Ym));
To compute a frequency axis for the plot above:
Fs=1/5.5e-7; %use the constant above to compute sampling frequency
L=length(ym); %length of ym = length of Ym
f=Fs*(0:L-1)/L;
plot(f,abs(Ym)); xlabel('Frequency (Hz)'); ylabel('abs(Ym)');
Note the symmetry in the plot above. The FFT of a real sequence is conjugate-symmetric about the Nyquist frequency, where fNyquist=Fs/2. Therefore you may limit your plot frequency range to [0,fNyquist] without losing any meaningful information. You may wonder why the FFT is not zero at frequencies other than Fm. The answer is that your sequence does not wrap around smoothly. Also, Fm is not exactly one of the frequencies in the DFT. You can reduce the effect of the wrap-around by windowing the signal.
2 CommentsShowHide 1 older comment
William Rose on 30 Dec 2021
@Daniel Wanesh, you can repeat the plotting of the function, and the computation and plotting in the frequency domain, by using the new vector of times above. Compute Fs using the new sampling interval, before you re-compute the vector of frequencies for plotting.
With the new time vector, you will notice that the new FFT is almost exactly zero at frequencies other than Fm. This is because the signal wraps around almost perfectly from its end to its beginning, with the new time vector. It is not exactly perfect, because the final point is a repeat of the initial point.

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by