% Dr. Durant, EE3220, W4D1, January 6, 2014
% Demonstrate aliased sampling of cosine.
fs = 40; % sampling frequency, Hz
f = 30; % frequency of input cosine (no antialias filter; AA filter would wipe out component above fs/2)
N = 10; % number of samples to illustrate
n = 0:N-1;
t_samp = linspace(0, (N-1)/fs, N); % sampling time: start, finish, number of samples
y_dig = cos(2*pi*f*t_samp); % samples of cosine
t = linspace(0, (N-1)/fs, N*100); % approximate continuous time; 100 is arbitrary but should be large
y_ana = cos(2*pi*f*t); % values at highly sampled, continuous approximation
f_al = fs - f; % In Hz, same as alias at digital frequency of 2pi - w0, where w0 is too-fast sinusoid
y_al_ana = cos(2*pi*f_al*t); % reconstructed (reconstruction filter limits to 0-fs/2 Hz, or 0-pi in digital frequency)
figure
% Plot all analog ("continuous" time) and digital (sampled) signals vs. time
subplot(211)
h = plot(t ,y_ana ,'b-',...
t_samp,y_dig ,'bo',...
t ,y_al_ana,'r--');
% h is array of handles (like pointers) to the 3 traces (series) in the plot
set(h,'LineWidth',2) % Make all lines thicker; can also modify select lines, e.g., h([1 3])
set(h(2),'MarkerSize',10) % Make the circles on the 2nd series bigger
legend('Analog source','Sampled','Analog reconstructed (aliased)')
xlabel('Time (s)'), ylabel('Signal level')
title('W4D1: Aliasing Example')
% Plot frequency domain view
subplot(212)
h = plot( [-f f], [1 1], 'ro', ...
fs+[-f f], [1 1], 'g^', ...
-fs+[-f f], [1 1], 'bv');
set(h,'LineWidth',2,'MarkerSize',10)
legend('Original','Periodic copy at fs (2\times\pi)','Periodic copy at -fs (-2\times\pi)')
hold on
h = plot([-fs/2 -fs/2], [0 1], 'k-', ...
[ fs/2 fs/2], [0 1], 'k-');
set(h,'LineWidth',5)
xlabel('Frequency (Hz)'), ylabel('Signal level')
title('Frequency domain view - vertical bars indicate |f| < fs/2')