%This demonstrates the 1-D sampling theorem on 
%2 Hz and 12 Hz sinusoids sampled at 20 samples/s.
%Waveforms and spectra of the sampled sinusoids are shown.
clear;T=0:0.001:1;N=0:0.05:1;
%2 Hz sinusoid sampled at 20 samples/s (no aliasing)
X1=cos(2*pi*2*T);%2 Hz sinusoid
figure,plot(T,X1),xlabel('t(s)'),ylabel('x(t)'),title('2 Hz sinusoid x(t)')
FX1=[0.5 0 0 0 0.5];%Spectrum of 2 Hz sinusoid
figure,stem([-35:34],[zeros(1,33) FX1 zeros(1,32)],'^'),...
xlabel('f(Hz)'),ylabel('X(f)(impulses;height=area)'),...
title('X(f) for 2 Hz sinusoid'),axis([-35 34 0 0.5])

X2=cos(2*pi*2*N);%2 Hz sampled at 20 Hz
figure,stem([0:0.05:1],X2,'^'),xlabel('t(s)'),...
ylabel('x_s(t)(impulses;height=area)'),...
title('x_s(t)(and x(t) in red) for 2 Hz sinusoid sampled at 20 samples/s')
hold on,plot(T,X1,'r')
%Spectrum of Sampled 2 Hz (sampling multiplies spectrum by sampling rate=20)
figure,stem([-35:34],[zeros(1,13) 20*FX1 zeros(1,15) 20*FX1 zeros(1,15) 20*FX1 zeros(1,12)],...
'^'),xlabel('f(Hz)'),ylabel('X_s(f)(impulses;height=area)'),axis([-35 34 0 10]),...
title('X_s(f) for 2 Hz sinusoid sampled at 20 samples/s. Note no aliasing.')

%12 Hz sinusoid sampled at 20 samples/s (aliasing)
X3=cos(2*pi*12*T);%12 Hz sinusoid
figure,plot(T,X3),xlabel('t(s)'),ylabel('x(t)'),title('12 Hz sinusoid x(t)')
FX3=[0.5 zeros(1,23) 0.5];
figure,stem([-35:34],[zeros(1,23) FX3 zeros(1,22)],'^'),...
xlabel('f(Hz)'),ylabel('X(f)(impulses;height=area)'),...
title('X(f) for 12 Hz sinusoid'),axis([-35 34 0 0.5])

X4=cos(2*pi*12*N);%12 Hz sampled 20 Samples/s.
figure,stem([0:0.05:1],X4,'^'),xlabel('t(s)'),...
ylabel('x_s(t)(impulses;height=area)'),....
title('x_s(t)(and x(t) in red) for 12 Hz sinusoid sampled at 20 samples/s')
hold on,plot(T,X3,'r')
%Spectrum of Sampled 12 Hz (sampling multiplies spectrum by sampling rate=20)
figure,stem([-35:34],[zeros(1,23) 20*FX3 zeros(1,22)],'^'),...
xlabel('f(Hz)'),ylabel('X(f)(impulses;height=area)'),axis([-35 34 0 10]),...
title('X_s(f) for 12 Hz sinusoid sampled at 20 samples/s. Note the aliasing')
hold on,stem([-35:34],[zeros(1,3) 20*FX3 zeros(1,15) 20*FX3 zeros(1,2)],'^r')




