Fast Fourier Transform function
5 views (last 30 days)
Show older comments
I tried to make a FFT of a sine wave but I didn't get what I expected.
t = 0:0.01:5; % time interval
w = 4*pi; % rad/s , so w = 2*pi*f, f=frequency
x = sin(w*t);
r = fft(x); % fast fourier transform function
R = real(r);
plot(R) % plotted only the real part of r
What I don't understand is that the only frequency should be 2 Hz, since f=w/(2*pi) = (4*pi)/(2*pi) = 2, but I get a total different value.
Can anyone help me understand the result?
Thanks!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/988410/image.png)
0 Comments
Accepted Answer
Paul
on 5 May 2022
The code in the question is plotting R vs index, but it sounds like the goal is to plot R vs frequency (Hz). So we need to compute the frequency vector that corresponds to the samples of r.
t = 0:0.01:5; % time interval
Ts = t(2); % sampling period
w = 4*pi; % rad/s , so w = 2*pi*f, f=frequency
x = sin(w*t);
r = fft(x); % fast fourier transform function
R = real(r);
The corresponding frequency vector in Hz is:
fhz = (0:(numel(r)-1))/numel(r)/Ts;
Plot the real part as in the Question
figure;plot(fhz,R),xlabel("Hz")
We see two peaks at around 2Hz and 98 Hz, whchi corresponds to -2Hz.
Zoom in
figure;plot(fhz,R,'-o'),xlim([0 10])
We see a peak at 2 Hz, but small non-zero values around 2Hz. These values come about because the samples of x do not exactly an integer number of periods of the underlying sine wave. The easiest way to get the expetect result to make the number of samples of x equal to an integer multiple of P/Ts, where P is the period of the sine wave
P = 1/2;
P/Ts
But the actual number of samples is
N = numel(t)
Because we really want 500 samples of data, jsut chop off the last point in the time vector
t(end) = []; % time interval
x = sin(w*t);
r = fft(x); % fast fourier transform function
R = real(r);
fhz = (0:(numel(r)-1))/numel(r)/t(2);
For the sine wave the real part of its FFT is zero, so also plot the imaginary part.
figure;
subplot(211);plot(fhz,R,'-o'),xlim([0 10])
subplot(212);plot(fhz,imag(r),'-o'),xlim([0 10])
xlabel('Hz')
As expected the real, part is basically zero and the imaginary part has a spike at 2Hz.
0 Comments
More Answers (1)
Mitch Lautigar
on 5 May 2022
Couple things i'd like to comment on to try and help you. Please see bullets below.
- You're original graph is setup correctly, but i'd recommend the following change to allow for more samples and therefore better end results. "t = 0:0.01:5;" should probably be changed to "t = 0:0.001:5;"
- When you take the FFT, you need to set the axis up correctly. This is a common complexity in signals and systems, but looking at the MATLAB FFT command will help you set it up (https://www.mathworks.com/help/matlab/ref/fft.html).
Try these two tips, and if you are still stuck, let me know.
0 Comments
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!