FFT seems to be not calculating Nyquist component?

13 views (last 30 days)
I am trying to understand the output of Matlab's FFT function - in particular which bins in the output vector correspond to which frequencies.
I decided to create a simple signal composed of a DC offset, a 2Hz sine wave, and a 4Hz sine wave. The signal is then sampled with a sampling frequency of 8Hz over 1 second. As I understand it, in the case that the number of samples in the input signal is even, the output of Matlab's FFT function, X, should be a vector made up of the following:
  • X[1] = DC component
  • X[2:N/2] = Positive frequency components
  • X[N/2+1] = Nyquist frequency component
  • X[N/2+1 : N] = Negative frequency components (will be discarded later due to symmetric redundancy, if input signal is real).
Therefore, I would expect that I should see a non-zero component in the 5th frequency bin in the output of the FFT, which should correspond to the component of the input signal at the Nyquist frequency. As can be seen below, the spectrum shows only the DC term (bin 1), the 2Hz (bin 3), the mirror of the 2Hz (bin 7), but nothing for the 4Hz...
FFTquestion.png
Here is the snippet:
f = 2; %frequency of the sine wave
A = 1; %amplitude of the sine wave
Fs = 8; %sampling rate
Ts = 1/Fs; %sampling time interval
t = 0:Ts:1-Ts; %time vector for signal
% Create sine wave with components at 2Hz,
% the Nyquist (Fs/2 = 4Hz), and a DC offset.
x = A*sin(2*pi*f*t) + A*sin(2*pi*Fs/2*t) + 0.1;
X_abs = abs(fft(x)); % Calculate DFT
% Plotting
figure;
subplot(1,2,1)
stem(t,x)
xlabel('Time [s]')
ylabel('x')
subplot(1,2,2);
stem(X_abs)
xlabel('Frequency Bin')
ylabel('abs[ fft(x) ]')
Can anyone tell me if I made a mistake, or am I misunderstanding something more fundamental?
Thanks.

Accepted Answer

Bruno Luong
Bruno Luong on 3 Dec 2018
Edited: Bruno Luong on 3 Dec 2018
Change
A*sin(2*pi*Fs/2*t)
to
A*cos(2*pi*Fs/2*t)
Or alternativey you must put cos expression in the imaginary part of your signal
  2 Comments
teeeeee
teeeeee on 3 Dec 2018
Edited: teeeeee on 3 Dec 2018
Thanks for your comment - this change does indeed make the Nyquist component appear as expected.
Could you elaborate a little as to why this only works when using a cosine signal?
Bruno Luong
Bruno Luong on 3 Dec 2018
Edited: Bruno Luong on 3 Dec 2018
If you want to introduce a Niquist (f=Fs/2) signal, the you shoud add the complex signal
exp(pi*Fs*t + phase).
When you takes only the sinus in the real part, it's incorrect and since the phase is pi/2, the sin(pi*Fs*t) is just equals to sin (k*pi) with k integer, which is 0, but you ignore the imaginary part of the signal (oscillating).
The cosine correspont to phase = 0, and you get
exp(pi*Fs*t) = cos(pi*Fs*t).
Remember, FFT, Fourier, etc... are complex analysis.

Sign in to comment.

More Answers (0)

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!