% Simulated digital data acquisition with potential aliasing
% Barukyah Shaparenko and John M. Cimbala

clear all
close all

% Instructions: 
%   -The following variables are user-adjustable - C, A, f, phi, f_s
%   -Set sampling frequency f_s <  2*f to simulate aliasing.

% Continuous (analog) signal: g(t) = C + A*sin(2*pi*f*t - phi)
C = 2; % V (DC offset of signal)
A = 3; % V (amplitude of signal)
f = 10; % Hz (frequency of signal)
phi = 0; % rad (phase shift of signal)
T = 1/f; % s (period of signal)
del_t = T/20; % s (small time step for the plot)

% Discretely sampled (digital) signal:
f_s = 65; % Hz (sampling frequency)
del_t_sample = 1/f_s; % s (time step for sampling)

t_analog = [0:del_t:round(20*T/del_t)*del_t]; % s
g_analog = C + A*sin(2*pi*f*t_analog - phi);

t_sample = [0:del_t_sample:round(20*T/del_t_sample)*del_t_sample]; % s
g_discrete = C + A*sin(2*pi*f*t_sample - phi);


% Discrete sampling:
scrsz = get(0,'ScreenSize');
figure('Position',[scrsz(3)/5 scrsz(4)*3/8 scrsz(3)*3/5 scrsz(4)/2])
hold on;
plot(t_analog,g_analog,'b-','LineWidth',2)
plot(t_sample,g_discrete,'ro-','LineWidth',2,'MarkerFaceColor','r')
grid on;
xlabel('time, \itt \rm(s)')
ylabel('signal (V)')
xlim([0 10*T])
ylim([C-A C+A])
pbaspect([2 1 1])
legend('analog signal','discrete signal','Location','SouthOutside','Orientation','Horizontal')
hold off;


