% 
% 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: Linear Convolution')
disp('Names: Nolan Bushnell, Federico Faggin, Gordon E. Moore')
disp(' ')

disp('PART 1: Continuous-Time Convolution =============================')
disp(' ')
disp('  ')

%create symbolic function
disp(' parameters and definitions:')
clear t x h y alpha
syms t x h y alpha 
x(t)=4*pulse((t-1.25)/0.5) 
h(t)= 2*pulse((t-2.25)/1.5)
y(t)=int(x(alpha)*h(t-alpha),alpha,-5,5)   %convolution

 
hh=figure(1);
sp=subplot(3,1,1);fplot(x,[-5 5],'LineWidth',3);grid on;sp.YLim=[0 10];
title('Continuous-time convolution: x(t)');xlabel('Time (s)');  
sp=subplot(3,1,2);fplot(h,[-5 5],'LineWidth',3);grid on;sp.YLim=[0 10];
title('h(t)');xlabel('Time (s)');  
sp=subplot(3,1,3);fplot(y,[-5 5],'LineWidth',3);grid on;sp.YLim=[0 10];
title('y(t) = x(t) * h(t)');xlabel('Time (s)');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 1. Continuous-time convolution. ')
disp(' ')
disp(' ')

 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q1. Change the simulation for h(t) to be a pulse with amplitude = 3 ')
disp('    beginning at t=1 s and ending at t=3 s.  Do not change x(t).')
disp('    (Fig. 1 simulation should change accordingly.)')
disp(' ')
disp(' ')
 

disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q2. After making the changes in Q1,')
disp('    observe Fig. 1, and use the cursor tool to measure the')
disp('    maximum amplitude of y(t).')
disp('    Also compute the theoretical maximum of y(t) by calculating the  ')
disp('    area of x(t) multiplied by h(t), when x(t) is centered in the middle ')
disp('    of h(t).   ')
disp('    Hint: multiply the functions point by point at each time t, ')
disp('    then take the area')
disp(' ')
disp('Answer: measured peak of y(t) = ???  ')
disp('Answer: calculated area = ???    ')
disp(' ')
disp(' ')

%create symbolic function
disp(' parameters and definitions:')
clear t x2 h2 y2 alpha
syms t x2 h2 y2 alpha 
x2(t)=10*triangle((t-0.5)/0.5)
x2(t)=4*pulse((t-0.5)/1) 
h2(t)= 1*pulse((t-3)/2) %sol
y2(t)=int(x2(alpha)*h2(t-alpha),alpha,-5,5)   %convolution

 
hh=figure(2);
sp=subplot(3,1,1);fplot(x2,[-5 5],'LineWidth',3);grid on;sp.YLim=[0 10];
title('Continuous-time convolution: x2(t)');xlabel('Time (s)');  
sp=subplot(3,1,2);fplot(h2,[-5 5],'LineWidth',3);grid on;sp.YLim=[0 10];
title('h2(t)');xlabel('Time (s)');  
sp=subplot(3,1,3);fplot(y2,[-5 5],'LineWidth',3);grid on;sp.YLim=[0 10];
title('y2(t)= x2(t) * h2(t)');xlabel('Time (s)');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 2. Continuous-time convolution. ')
disp(' ')
disp(' ')

 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q3. Change the simulation for h2(t) to be a pulse with amplitude = 2 ')
disp('    beginning at t=1 s and ending at t=2 s.')
disp('    (Fig. 2 simulation should change accordingly.)')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q4. After making the changes in Q3,')
disp('    observe Fig. 2, and use the cursor tool to measure the')
disp('    maximum amplitude of y2(t).  Be sure to measure the peak.')
disp('    Also compute the theoretical maximum of y2(t) by calculating the  ')
disp('    area of x2(t) multiplied by h2(t), when x2(t) is centered in the middle ')
disp('    of h2(t).   ')
disp('    Hint: multiply the functions point by point at each time t, ')
disp('    then take the area')
disp(' ')
disp('Answer: measured peak of y2(t) = ???  ')
disp('Answer: calculated area = ???    ')
disp(' ')
disp(' ')


disp('PART 2: Discrete-Time Linear Convolution =============================')
disp(' ')
disp('  ')


xn=[0 0 0 0    4 4 0 0    0 0 0 0    0 0 0 0    0 0 0 0 ]
hn=[1 2 3 2    1 0 0 0    0 0 0 0    0 0 0 0    0 0 0 0  ]  
yn=conv(xn,hn); %discrete-time  convolution
yn=yn(1:length(xn))
nn=0:length(yn)-1

hh=figure(3);
sp=subplot(3,1,1);stem(nn,xn,'b','LineWidth',2);grid on;%sp.YLim=[-2 2];
title('Discrete-time linear convolution: x[n]');xlabel('n'); %ylabel('x(t)');
sp=subplot(3,1,2);stem(nn,hn,'b','LineWidth',2);grid on;%sp.YLim=[-2 2];
title('h[n]');xlabel('n'); %ylabel('x(t)');
sp=subplot(3,1,3);stem(nn,yn,'b','LineWidth',2);grid on;%sp.YLim=[-2 2];
title('y[n]= x[n] * h[n]');xlabel('n'); %ylabel('x(t)');
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 3. Discrete-time convolution. ')
disp(' ')
disp(' ')

 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q5. Change the simulation, setting ')
disp('    hn= [3 3 3 3  0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 0  ]')
disp('    (Fig. 3 simulation should change accordingly.)')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q6. After making the changes in Q5,')
disp('    observe Fig. 3, and use the cursor tool to measure the')
disp('    maximum amplitude of yn (denoted y[n] in the plot).')
disp('    Also compute the theoretical maximum of y[n] by calculating the  ')
disp('    sum of x[n] multiplied by h[n], when x[n] is centered in the middle ')
disp('    of h[n].   ')
disp('    Hint: multiply the functions point by point at each point n, ')
disp('    then take the sum. ')
disp(' ')
disp('Answer: measured peak of y[n] = ???  ')
disp('Answer: calculated sum =???    ')
disp(' ')
disp(' ')


xn2=[1 2 3 0    0 0 0 0    0 0 0 0    0 0 0 0    0 0 0 0  ]  
hn2=[0 0 0 1    0 0 0 0    1 0 0 0    0 0 0 0    0 0 0 0  ] %sol
yn2=conv(xn2,hn2); %discrete-time  convolution
yn2=yn2(1:length(xn2))
nn2=0:length(yn2)-1

hh=figure(4);
sp=subplot(3,1,1);stem(nn2,xn2,'b','LineWidth',2);grid on;sp.YLim=[0 10];
title('Discrete-time linear convolution: x2[n]');xlabel('n'); %ylabel('x(t)');
sp=subplot(3,1,2);stem(nn2,hn2,'b','LineWidth',2);grid on;sp.YLim=[0 10];
title('h2[n]');xlabel('n'); %ylabel('x(t)');
sp=subplot(3,1,3);stem(nn2,yn2,'b','LineWidth',2);grid on;sp.YLim=[0 10];
title('y2[n]= x2[n] * h2[n]');xlabel('n'); %ylabel('x(t)');
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 4. Discrete-time convolution. ')
disp(' ')
disp(' ')

 
disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q7. Change the simulation, setting ')
disp('    xn= [1 1 1 0  0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 0  ]')
disp('    hn= [3 3 3 0  0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 0  ]')
disp('    (Fig. 4 simulation should change accordingly.)')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q8. After making the changes in Q7,')
disp('    observe Fig. 4, and use the cursor tool to measure the')
disp('    maximum amplitude of yn2 (denoted y2[n] in the plot).')
disp('    Also compute the theoretical maximum of y2[n] by calculating the  ')
disp('    sum of x2[n ]multiplied by h2[n], when x2[n] is centered in the middle ')
disp('    of hn2.   ')
disp('    Hint: multiply the functions point by point at each point n, ')
disp('    then take the sum. ')
disp(' ')
disp('Answer: measured peak of yn = ???  ')
disp('Answer: calculated sum = ???    ')
disp(' ')
disp(' ')

clear A Ar Ai B Br Bi Cformula
syms A Ar Ai B Br Bi Cformula 
Cformula=(Ar*Br + Ar*Ai ) + j*( Ai*Bi - Ar*Br ) %fix this formula
Ar=2; Ai=3;  % Ar=real part of A, Ai=imaginary part
A=Ar+ j*Ai   %complex variable A
Br=5; Bi=7;
B=Br + j*Bi  %complex variable B
Cmult=A*B    %multiplication
Cformula=subs(Cformula)

disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q9. ')
disp('    Observe the values of Cmult and Cformula above, where Cformula')
disp('    is the explicit formula for complex multiplication WITH ERRORS.')
disp('    Correct the errors in the formula for Cformula, such that  ')
disp('    Cmult = Cformula above. Note: A*B is NOT the answer.')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q10. ')
disp('    What famous 1971 integrated circuit has Federico Faggin initials, ')
disp('    and what was the function this digital circuit? ')
disp('    Hint: see www.intel4004.com')
disp('    ')
disp('Answer: ???')
disp(' ')
disp(' ')

 