% 
% copyright 2020 by Thomas P. Weldon
% all rights reserved
clear all
close all hidden
clc

%some useful functions
syms u(t) pulse(t) triangle(t)  
sympref('HeavisideAtOrigin',1); %set equal to 1 as in unitstep
newHeaaviside0=heaviside(sym(0))
u(t)=heaviside(t) %unit step
pulse(t)=u(t+1/2)-u(t-1/2)
triangle(t)=(2*(t+1/2)-4*t*u(t))*pulse(t)


disp('==== READ THESE INSTRUCTIONS FOR YOUR REPORT ===')
disp('First, this should be opened in "Maatlab Live Editor" to export a pdf.' ) 
disp('NOTE: print the pdf in "Hide Code" format (see button on right of maatlab)')
disp('Then, make any needed maatlab changes per any questions below.') 
disp('You must re-run the script for changes to take effect.') 
disp('Dont forget to add the names of the group members as shown below.')
disp('Answer all numbered questions below, Q1, Q2, etc.') 
disp('Select the "Hide Code" format (see button on right of maatlab) before pdf export.')
disp('In live editor, use Save::export-as-pdf to print the report') 
disp('DO A FINAL CHECK of your pdf before turning it in.') 
disp(' ======================================================.')
disp(' ')
disp('ECGR 4124 Computer Project: Sampling')
disp('Names: Nolan Bushnell, Federico Faggin, Gordon E. Moore')
disp(' ')

disp('PART 1: Sampling =============================')
disp(' ')
disp('  ')

%create symbolic function
disp(' parameters and definitions:')
clear t x Ts xn tim n
syms t x Ts xn tim n
f1=40  
x(t)=1*sin(2*pi*f1*t) %sol
Ts=1/2000
tim=0:Ts:50*Ts;
n=0:length(tim)-1;
xn=x(tim);

hh=figure(1);
sp=subplot(2,1,1);fplot(x,[0 max(tim)],'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('x(t)');xlabel('Time (s)');  
sp=subplot(2,1,2);stem(n,xn,'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('x[n]');xlabel('n');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 1. Signal sampling. ')
disp(' ')
disp(' ')

 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q1. Fig. 1 shows ideal sampling of continuous-time signal x(t) ')
disp('    resulting in discrete-time signal x[n]. ')
disp('    Change the simulation code for x(t)  such that the ')
disp('    frequency of x(n) is changed to 250 Hz.')
disp('    Do not change the sample rate.')
disp('    (Fig. 1 simulation should change accordingly.)')
disp(' ')
disp(' ')
 

disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q2. After making the changes in Q1, observe Fig. 1.')
disp('    What is the sample rate in samples/s? ')
disp(' ')
disp('Answer: sample rate = ??? samples/s  ')
disp(' ')
disp(' ')

disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q3. After making the changes in Q1 observe Fig. 1, and use ')
disp('    the cursor tool to measure the phase change of the sinusoid x(t) ')
disp('    between the first and second samples, x[0] and x[1].  You may ')
disp('    use the arcsin function on a calculator to determine phase by ')
disp('    using the cursor tool to measure the amplitudes of the sinusoid ')
disp('    x[n] in the plot.   ')
disp('    Also, calculate the theoretical discrete-time frequency in  ')
disp('    radians/sample for the sampled sinusoid x[n]. ')
disp('    Are the two answers numerically equal (ignore units) to within +/-10%?')
disp(' ')
disp('Answer: measured phase change = ??? radians  ')
disp('Answer: discrete-time frequency of x[n] = ??? radians/sample ')
disp('Answer: numerically equal +/-10%: yes/no ')
disp(' ')
disp(' ')



disp('PART 2: Aliasing =============================')
disp(' ')
disp('  ')

%create symbolic function
disp(' parameters and definitions:')
clear t x2 x3 Ts xn tim n
syms t x2 x3 Ts xn tim n
f1=250  
f2=2250
%x(t)=4*pulse((t-0.001/2)/(0.001))*sin(2*pi*f1*t) %sol
x2(t)=1*sin(2*pi*f1*t) %sol
x3(t)=1*sin(2*pi*f2*t) %sol
Ts=1/2000
tim=0:Ts:20*Ts;
n=0:length(tim)-1;
x2n=x2(tim);
x3n=x3(tim);

hh=figure(2);
sp=subplot(4,1,1);fplot(x2,[0 max(tim)],'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('x2(t)');xlabel('Time (s)');  
sp=subplot(4,1,2);stem(n,x2n,'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('x2[n]');xlabel('n');  
sp=subplot(4,1,3);fplot(x3,[0 max(tim)],'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('x3(t)');xlabel('Time (s)');  
sp=subplot(4,1,4);stem(n,x3n,'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('x3[n]');xlabel('n');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 2. Signal sampling and aliasing. ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q4. Observe Fig. 2, and verify that sampled signals x2[n] and x3[n] ')
disp('    are equal to each other, even though continupus time signals x2(t) ')
disp('    and x3(t) are clearly not equal (use the data cursor to compare).   ')
disp('    This is an example of aliasing, where 2 different signals result in')
disp('    the same exact sampled values.   ')
disp('    Clearly, the aliased signal x3[n] does not resemble x3(t).   ')
disp('    We will study aliasing in more detail later in the course, but for now,')
disp('    we will just state that to avoid aliasing the sample rate must be greater')
disp('    than two times the frequency of the signal frequency.')
disp(' ')
disp('    What is the sample rate in samples/s?')
disp('    What is the frequency of x3(t) in Hz?')
disp('    Is the sample rate greater than two times the frequency of x3(t)?')
disp(' ')
disp('Answer: sample rate = ??? samples/s  ')
disp('Answer: frequency of x3(t) = ??? Hz')
disp('Answer: sample rate greater than two times the frequency of x3(t): Yes/No ')
disp(' ')
disp(' ')



disp('PART 3: Quantization noise =============================')
disp(' ')
disp('  ')

%create symbolic function
disp(' parameters and definitions:')
clear t g Ts gn tim n
syms t g  Ts gn tim n
%x(t)=4*pulse((t-0.001/2)/(0.001))*sin(2*pi*f1*t) %sol
%g(t)=1*sin(2*pi*f1*t)  
g(t)=0.1*t*pulse(t/10)  
 %sol
Ts=1/4
tim=-10:Ts:10;
n=0:length(tim)-1;
gn=g(tim);
gn=round(8*gn)/8;


hh=figure(3);
sp=subplot(2,1,1);fplot(g,[min(tim) max(tim)],'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('g(t)');xlabel('Time (s)');  
sp=subplot(2,1,2);stem(n,gn,'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('g[n]');xlabel('n');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 3. Simulation of ADC+DAC quantization. ')
disp(' ')
disp(' ')

 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q5. The plots in Fig. 3 simulate quantization error of an ADC ')
disp('    with finite resolution, where g(t) is the continous-time ')
disp('    signal voltage, and g[n] is the finite-precision ADC output.')
disp('    The finite step size delta-V is seen in the finite steps ')
disp('    in the output g[n] of the ADC, as it digitizes g(t).  The ')
disp('    ADC selects the nearest quantized voltage at each sample instant.')
disp('    What is the step size delta-V for this ADC?')
disp(' ')
disp('Answer: step size delta-V = ??? V  ')
disp(' ')
disp(' ')


 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q6. From the class notes, the step-size delta-V for the ADC ')
disp('    simulation of Fig. 3 determines the rms voltage of the ')
disp('    ADC quantization noise.')
disp('    What is the quantization noise in Vrms for this ADC?')
disp(' ')
disp('Answer: quantization noise = ??? Vrms  ')
disp(' ')
disp(' ')


 
%create symbolic function
disp(' parameters and definitions:')
clear t g Ts gn tim n
syms t g  Ts gn tim n
%x(t)=4*pulse((t-0.001/2)/(0.001))*sin(2*pi*f1*t) %sol
%g(t)=1*sin(2*pi*f1*t)  
g2(t)=10*sin(7/5*t+0.13)  
 %sol
Ts=1/4
tim=-10:Ts:10;
n=0:length(tim)-1;
g2n=g2(tim);
g2n=round(8*g2n)/8;
errn=eval(g2n-g2(tim));
meanError=mean(errn)
varError=var(errn)

hh=figure(4);
sp=subplot(3,1,1);fplot(g2,[min(tim) max(tim)],'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('g2(t)');xlabel('Time (s)');  
sp=subplot(3,1,2);stem(n,g2n,'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('g2[n]');xlabel('n');  
sp=subplot(3,1,3);stem(n,errn,'LineWidth',3);grid on;%sp.YLim=[-10 10];
title('error[n]=g2(n*Ts) - g2[n]');xlabel('n');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 4. Simulation of ADC+DAC quantization. ')
disp(' ')
disp(' ')

 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q7. The plots in Fig. 4 simulate quantization error of an ADC ')
disp('    with finite resolution, where g2(t) is the continous-time ')
disp('    signal voltage, g2[n] is the finite-precision ADC output, ')
disp('    and error[n] is the quantization error. ')
disp('    For the sinusoidal signal digitized by the ADC in FIg. 4,  ')
disp('    the error can seen to be random.')
disp('    Looking at the maatlab script, what is the step-size delta-V for')
disp('    the ADC? ')
disp('    Using the cursor tool on the plot, what is the maximum value of')
disp('    errror[n] in the simulation?    ')
disp('     ')
disp(' ')
disp('Answer: step-size delta-V= ??? V  ')
disp('Answer: maximum of errror[n]= ??? V  ')
disp(' ')
disp(' ')

 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q8. From class notes for an ADC with finite resolution,')
disp('    the error lies between +/- step-size divided by two, ')
disp('    that is +/- (delta-V)/2.  ')
disp('    Based on the answer to the previous question,  ')
disp('    is the maximum of errror[n] within +/- 20% of half the step-size?  ')
disp('     ')
disp(' ')
disp('Answer: max(error[n]) within +/- 20% of (delta-V)/2: yes/no ')
disp(' ')
disp(' ')

 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q9. From class notes for an ADC with finite resolution,')
disp('    the average (or mean) error is zero, and variance equals ')
disp('    (delta-V)^2/12. ')
disp('    Looking at the code and simulation, what is the measured ')
disp('    mean of error[n]?')
disp('    Looking at the code and simulation, what is the measured ')
disp('    variance of error[n]?')
disp('    What is the theoretical variance of error[n]?')
disp(' ')
disp('Answer: mean of error[n] = ??? V ')
disp('Answer: variance of error[n] = ???  ')
disp('Answer: theoretical variance of error[n] = ???  ')
disp(' ')
disp(' ')

 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q10. In what year did Claude Shannon write his MIT masters thesis,')
disp('    considered the foundation for digital circuit design theory? ')
disp('    Note: He is also considered the founder of information theory.')
disp('    Hint: use wiki.')
disp(' ')
disp('Answer: ???? ')
disp(' ')
disp(' ')