clear all;
close all;

%Create an -point cosine:

N=16;
Fs = 16;
Freq = 2;

x=cos(2*pi*Freq*(0:1:N-1)/N)';

figure(1)
subplot(4,1,1);
stem(0:N-1,x,'.');
axis([-0.2 N -1.2 1.2]);
legend('N-pt Sample Cosine signal x(n)');
xlabel('n \rightarrow');

% Take its Fourier transform and compute normalised Magnitude Spectrum:

X=abs(fft(x))/N;
subplot(4,1,2);
stem(0:N-1,X,'.');
axis([-0.2 N -0.1 1.1]);
legend('Magnitude spectrum |X(k)|');
xlabel('k \rightarrow')

% Note: Axes are still labelled a the points (same dimension as input signal)

% Let's relabel the x axes with frequencies,

X=abs(fft(x))/N;
f=((0:N-1)/N)*Fs;

subplot(4,1,3);stem(f,X,'.');
axis([0 (N-1)*Fs/N -0.1 1.1]);
legend('Magnitude spectrum |X(f)|');

xlabel('f in Hz \rightarrow')

%Let's relabel the x axes with frequencies, but now lets centre the FFT with fftshift() :


X=abs(fftshift(fft(x)))/N;

subplot(4,1,4);stem(-N/2:N/2-1,X,'.');
axis([-N/2 N/2 -0.1 1.1]);
legend('Magnitude spectrum |X(f)|');
xlabel('f in Hz\rightarrow')


% Now let's increase the sample rate, and change the nuimber of points, and frequency:

figure(2)
Fs=1024;
N=2*Fs;
Freq = 200;

x=cos(2*pi*(Freq/Fs)*(0:1:N-1))';
f=((0:N-1)/N)*Fs;
X=abs(fft(x))/N;
subplot(2,1,1);stem(f,X,'.');
axis([-0.2*Fs/N max(f) -0.1 1.1]);
legend('Magnitude spectrum |X(f)|');
ylabel('c)');
xlabel('f in Hz \rightarrow')

%And centred:

fshift=((-N/2:N/2-1)/N)*Fs;
X=abs(fftshift(fft(x)))/N;

subplot(2,1,2);stem(fshift,X,'.');
axis([-0.5*(N-1)*Fs/N 0.5*(N-1)*Fs/N -0.1 1.1]);
legend('Magnitude spectrum |X(f)|');
ylabel('b)');
xlabel('f in Hz\rightarrow')

