MATLAB: FFT seems to be not calculating Nyquist component

digital signal processingfftfrequencyMATLAB

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.

Best Answer

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
Related Question