% maatlab intro
% 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 4123 Computer Project: Fourier Transform')
disp('Names: Nolan Bushnell, Federico Faggin, Gordon E. Moore')
disp(' ')

disp('PART 1: Convolution ================================')
%create symbolic function
syms omega t x(t) y(t) g(t) h(t) alpha
f=500
omega= f*2*pi

x(t)=pulse(t/2)
h(t)=exp(-4*abs(t))
y(t)=int(x(alpha)*h(t-alpha),alpha,-5,5)
g(t)=exp(-t/2)*u(t)

hh=figure(1);
sp=subplot(2,2,1);fplot(x,[-2 2],'LineWidth',3);grid on;sp.YLim=[-1 1];
title('x(t)');xlabel('Time (s)'); %ylabel('x(t)');
sp=subplot(2,2,2);fplot(h,[-2 2],'LineWidth',3);grid on;sp.YLim=[-1 1];
title('h(t)');xlabel('Time (s)'); 
set(findall(hh,'Type','text'),'FontSize',16);
sp=subplot(2,2,3);fplot(y,[-2 2],'LineWidth',3);grid on;sp.YLim=[-1 1];
title('y(t)');xlabel('Time (s)'); 
sp=subplot(2,2,4);fplot(g,[-2 2],'LineWidth',3);grid on;sp.YLim=[-1 1];
title('g(t)');xlabel('Time (s)');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 1.  Time functions. ')
disp(' ')
disp(' ')




disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q1. Change h(t) to e^-|8t| using matlab exp() function,')
disp('     and observe the new plots. ')
disp('     Your turned-in plots should change accordingly.')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q2. After making the changes in Q1,')
disp('     use a dataCursor tool on the plot to measure the peak value of  ')
disp('     y(t) at t=0, where y(t) is the convolution of x(t) with h(t).')
disp(' ')
disp(' Answer: peak value of y(t)= ??? ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q3. After making the changes in Q1,')
disp('     lookup the fourier transform of h(t) in the book, ')
disp('     to obtain H(f),and find the dc gain at f=0.  ')
disp('     Multiply the input x(t) at t=0 by this dc gain, and compare this')
disp('     value to the answer in Q2. ')
disp(' ')
disp(' Answer: dc gain H(0)= ???     ')
disp(' part2: x(0)*H(0) approximately equals answer to Q2: yes/no ')
disp(' ')



disp('PART 2: Fourier transform  ==========================')
clear t f r(t) s(t) f R(f) S(f) Rmag(f) Smag(f)
syms t f r(t) s(t) f R(f) S(f) Rmag(f) Smag(f)

r(t)=pulse(10*t)
s(t)=pulse(20*t)

hh=figure(2);
sp=subplot(2,1,1);fplot(r,[-0.2 0.2],'LineWidth',3);grid on;sp.YLim=[-2 2];
title('r(t)');xlabel('Time (s)');  
sp=subplot(2,1,2);fplot(s,[-0.2 0.2],'LineWidth',3);grid on;sp.YLim=[-2 2];
title('s(t)');xlabel('Time (s)');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 2. Pulse functions. ')
disp(' ')
disp(' ')

R(f)=int(r(t)*exp(-i*2*pi*f*t),t,-0.5,0.5)
S(f)=int(s(t)*exp(-i*2*pi*f*t),t,-0.5,0.5)
Rmag(f)=abs(R(f))
Smag(f)=abs(S(f));

hh=figure(3);
sp=subplot(2,1,1);fplot(Rmag,[-40 40],'LineWidth',3);grid on;sp.YLim=[0 0.1];
title('|R(f)|');xlabel('Freq (Hz)');  
sp=subplot(2,1,2);fplot(Smag,[-40 40],'LineWidth',3);grid on;sp.YLim=[0 0.1];
title('|S(f)|');xlabel('Freq (Hz)');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 3. Pulse Fourier transforms. ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q4. What is the magnitude of the dc response at f=0 for |S(f)| and  ')
disp('    what is the area under the pulse s(t)?')
disp(' ')
disp(' Answer: |S(0)|= ???   area under s(t)= ??? ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q5. If the pulse r(t) is used to send binary data at one bit per pulse, ')
disp('    how many bits per second can be sent? ')
disp('    What is the zero-crossing bandwidth of R(f)? ')
disp('    (bandwidth up to first zero-crossing of sinc function)')
disp(' ')
disp(' Answer: bits per second= ???   zero-crossing bandwidth= ??? Hz ')
disp(' ')
disp(' ')


disp('PART 3: Fourier transform of convolution =============')
clear t f q(t) v(t) w(t) f Q(f) V(f) W(f) Qmag(f) Vmag(f) Wmag(f)
syms t f q(t) v(t) w(t) f Q(f) V(f) W(f) Qmag(f) Vmag(f) Wmag(f)


q(t)=pulse(20*t)
v(t)=pulse(40*t)
w(t)=int(q(alpha)*v(t-alpha),alpha,-0.5,0.5)

hh=figure(4);
sp=subplot(3,1,1);fplot(q,[-0.2 0.2],'LineWidth',3);grid on;sp.YLim=[-2 2];
title('q(t)');xlabel('Time (s)'); 
sp=subplot(3,1,2);fplot(v,[-0.2 0.2],'LineWidth',3);grid on;sp.YLim=[-2 2];
title('v(t)');xlabel('Time (s)'); 
set(findall(hh,'Type','text'),'FontSize',16);
sp=subplot(3,1,3);fplot(w,[-0.2 0.2],'LineWidth',3);grid on;sp.YLim=[-0.05 0.05];
title('w(t)');xlabel('Time (s)'); 
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 4. Function w(t) equals convolution of q(t) with v(t). ')
disp(' ')
disp(' ')

Q(f)=int(q(t)*exp(-i*2*pi*f*t),t,-0.5,0.5)
V(f)=int(v(t)*exp(-i*2*pi*f*t),t,-0.5,0.5)
W(f)=int(w(t)*exp(-i*2*pi*f*t),t,-0.5,0.5)
Qmag(f)=abs(Q(f))
Vmag(f)=abs(V(f));
Wmag(f)=abs(W(f));

hh=figure(5);
sp=subplot(3,1,1);fplot(Qmag,[-40 40],'LineWidth',3);grid on;sp.YLim=[0 0.05];
title('|Q(f)|');xlabel('Freq (Hz)');  
sp=subplot(3,1,2);fplot(Vmag,[-40 40],'LineWidth',3);grid on;sp.YLim=[0 0.05];
title('|V(f)|');xlabel('Freq (Hz)');  
set(findall(hh,'Type','text'),'FontSize',16);
sp=subplot(3,1,3);fplot(Wmag,[-40 40],'LineWidth',3);grid on;%sp.YLim=[0 0.05];
title('|W(f)|');xlabel('Freq (Hz)');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 5. Convolution Fourier transforms. ')
disp(' ')
disp(' ')

disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q6. Does dc response at f=0 for |W(f)| equal the product of the  ')
disp('    dc responses of |Q(f)| and |V(f)|?')
disp('    (Ignore small errors less than 5%)')
disp(' ')
disp(' |W(0)|= |Q(0)|*|V(0)|    Answer: yes/no    ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q7. Does |W(f)| equal zero whenever |Q(f)| equals zero?   ')
disp(' ')
disp(' Answer: yes/no    ')
disp(' ')
disp(' ')



disp('PART 4: Frequency shift =============')
clear t f m(t) n(t) p(t) M(f) N(f) P(f) Mmag(f) Nmag(f) Pmag(f)
syms  t f m(t) n(t) p(t) M(f) N(f) P(f) Mmag(f) Nmag(f) Pmag(f)


m(t)=exp(-20*abs(t))
n(t)=sin(2*pi*100*t)
p(t)=m(t)*n(t)


hh=figure(6);
sp=subplot(3,1,1);fplot(m,[-0.2 0.2],'LineWidth',3);grid on;sp.YLim=[-2 2];
title('m(t)');xlabel('Time (s)'); 
sp=subplot(3,1,2);fplot(n,[-0.2 0.2],'LineWidth',3);grid on;sp.YLim=[-2 2];
title('n(t)');xlabel('Time (s)'); 
set(findall(hh,'Type','text'),'FontSize',16);
sp=subplot(3,1,3);fplot(p,[-0.2 0.2],'LineWidth',3);grid on;sp.YLim=[-2 2];
title('p(t)');xlabel('Time (s)'); 
set(findall(hh,'Type','text'),'FontSize',16);
disp('     Fig. 6. Function p(t) equals  m(t) times n(t). ')
disp(' ')
disp(' ')

M(f)=int(m(t)*exp(-i*2*pi*f*t),t,-0.5,0.5)
N(f)=int(n(t)*exp(-i*2*pi*f*t),t,-0.5,0.5)
P(f)=int(p(t)*exp(-i*2*pi*f*t),t,-0.5,0.5)
Mmag(f)=abs(M(f))
Nmag(f)=abs(N(f));
Pmag(f)=abs(P(f));

hh=figure(7);
sp=subplot(2,1,1);fplot(Mmag,[-200 200],'LineWidth',3);grid on;sp.YLim=[0 0.1];
title('|M(f)|');xlabel('Freq (Hz)');  
sp=subplot(2,1,2);fplot(Pmag,[-200 200],'LineWidth',3);grid on;sp.YLim=[0 0.1];
title('|P(f)|');xlabel('Freq (Hz)');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('     Fig. 7. Frequency shift Fourier transforms. ')
disp(' ')
disp(' ')
 

disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q8. What is the frequency of n(t), and what is the frequency shift   ')
disp('    of |M(f)| that is observed in |P(f)|')
disp(' ')
disp(' Answer: freq. of n(t)= ??? Hz,    freq. shift observed= ??? Hz   ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q9. In what year did Jean Baptiste Joseph Fourier present the notion ')
disp('    of Fourier series for heat flow in Theorie analytique de la chaleur?')
disp(' ')
disp(' Answer: year= ???  ')
disp(' ')
disp(' ')

disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q10. In what year did J. W. Cooley of IBM and John W. Tukey of Princeton')
disp('     publish their Fast Fourier Transform paper: An algorithm for the')
disp('     machine calculation of complex Fourier series?')
disp(' ')
disp(' Answer: year = ???   ')
disp(' ')
disp(' ')