About FFT of a wave

4 views (last 30 days)
Daniel Wanesh
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 Comments
Daniel Wanesh
Daniel Wanesh on 4 Jan 2022
Thank you very much for your help
Very much appreciated
William Rose
William Rose on 4 Jan 2022
You're welcome, @Daniel Wanesh. Good luck with your work!

Sign in to comment.

Accepted Answer

William Rose
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 Comments
William Rose
William Rose on 30 Dec 2021
Edited: William Rose on 30 Dec 2021
I notice that your signal is badly aliased. By that I mean that the sampling interval, 5.5e-7, is too slow to accurately represent this sine wave, whose frequency is Fm=2x10^6.
Fm = 2*10^6; Em = 4;
t1 = 0 : 5.5e-7 : 1e-5; %the original sampling, which is not fast enough
y1 = Em.*cos(2*pi*Fm*t1);
plot(t1,y1,'-r*'); hold on; xlabel('Time (s)');
Try sampling the signal at a faster rate:
t2=0:5e-8:1e-5;
y2 = Em.*cos(2*pi*Fm*t2);
plot(t2,y2,'-b.');
The original sampling rate produces the red line signal above, which is obviously an incorrect representation of the signal.
William Rose
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.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!