% =========================================================================
% This script generates a figure to illustrate the aliasing phenomenon for 
% the Mechatronics Computer Data Collection Lab.  
%
% Ben Shores
% Mechatronics TA, Fall 2003
% =========================================================================
clear, close, clc

% Create a High-Resolution Sine Wave 'Signal'
freq = 10;        %Hz ---- signal frequency
w    = freq*2*pi; %rad/s - angular frequency
T    = 1/freq;    %s ----- period of signal
A    = 4;         %V ----- signal Amplitude
N    = 1000;      % ------ Number of samples (high-res)
n    = 10;         % ------ Number of cycles of the signal

t = 0:n*T/(N-1):n*T; % - time values
V = A*sin(w*t);      % - Voltage values

% Create Sampled Frequencies
% 
% First Alias f_alias = 1 Hz -> f_sample = 9 Hz
f_sam= 9;
N_1  = round(n*T*f_sam) + 1;
dt_1 = 1/f_sam;
for j = 1:N_1
    t_1(j) = dt_1*(j-1);
    v_1(j) = A*sin(w*t_1(j));
end 

% % Second Alias f_alias = 3 Hz -> f_sample = 7 Hz
% f_sam= 7;
% N_2  = round(n*T*f_sam) + 1;
% dt_2 = 1/f_sam;
% for j = 1:N_2
%     t_2(j) = dt_2*(j-1);
%     v_2(j) = A*sin(w*t_2(j));
% end 

%,t_2,v_2,'*--'

% Plot the data 
figure(gcf); clf
plot(t,V,'-',t_1,v_1,'o--',[0 .25 .5 .75 1],zeros(5,1),':k');
legend('10 Hz signal', '9 Hz sample rate',3)
xlabel('Time [s]')
ylabel('Voltage [V]')
