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

disp('PART 1: line codes =============================')
disp(' ')
disp('  ')

%create symbolic function
disp(' parameters and definitions:')
clear t k h y f x s p alpha delta dat ptk gs gtk(t,k) gs(t)
syms t k h y f x s palpha delta dat ptk gs gtk(t,k) gs(t)  

T0=0.001;
Ts=T0/16
%x(t)=pulse(t/(tau+Ts))*2*t/tau
%x(t)=pulse(t/(tau+Ts))*exp(-16*t^2/tau^2)
%x(t)=2*(pulse(t/tau)*(t+tau/2)/tau+pulse((t-tau/4)/(tau/2))*(-2*t)/tau)
data=[1 0 1 1 0 0 1 1 0 1 0 0 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 1 0  0 1 1 0 1 0 0 0 0 1 1 1 0 0 1 0 1 1 0 1 ];
%data=randi([0 1],50,1);
tau=length(data)*T0
ptk(t,k)=pulse((t-T0/4-k*T0)/(T0/2));
p(t)=ptk(t,0)
tim=[0:Ts:tau];
dt=(tim(2)-tim(1))

%y(t)=symsum(ptk,k,1,length(data))
out=[num2str(2*data(1)-1)  ' * pulse((t-T0/4-' num2str(0) '*T0)/(T0/2))'  ];
out2=[num2str(2*data(1)-1)  ' * pulse((t-T0/2-' num2str(0) '*T0)/(T0))'  ];
for k=2:length(data)
  out=[out ' + ' num2str(2*data(k)-1)  ' * pulse((t-T0/4-' num2str(k-1) '*T0)/(T0/2))' ];
  out2=[out2 ' + ' num2str(data(k))  ' * pulse((t-T0-' num2str(k-1) '*T0)/(T0))' ];
end
y(t)=str2sym(out);
dat(t)=str2sym(out2);
yt=eval(subs(y(tim)));                    %time samples of y(t)
datat=eval(subs(dat(tim)));                     %time samples of data
% ffty=fft(yt)*dt;%  
% 
% %faster convolution method
% ff=[1:length(tim)];
% ff=(ff-1)/length(ff)*1/dt;
 pt=eval(p(tim));                   %time samples of x(t)
% fftp=fft(pt)*dt;
% %ffty=fft(yt)*dt;

disp('sampling period and frequency:')
Ts=Ts
% fs=1/Ts
% disp('dc values:')
% fftpdc=fftp(1)
% fftydc=ffty(1)






 
hh=figure(1);
sp=subplot(3,1,1);plot(tim,pt,'b','LineWidth',2);grid on;%sp.YLim=[-2 2];
title('p(t)');xlabel('Time (s)'); %ylabel('x(t)');
sp=subplot(3,1,2);plot(tim,yt,'b','LineWidth',2);grid on;%sp.YLim=[-2 2];
title('y(t)');xlabel('Time (s)'); %ylabel('x(t)');
sp=subplot(3,1,3);plot(tim,datat,'b','LineWidth',2);grid on;%sp.YLim=[-2 2];
title('data(t)');xlabel('Time (s)'); %ylabel('x(t)');
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 1. Line code p(t) and y(t). ')
disp(' ')
disp(' ')



disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q1. What is the line code of the signal y(t) in Fig. 1,')
disp('    given that the binary data is at the bottom of Fig. 1 ?')
disp('     ')
disp('  Answer: ???? ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q2. What is data rate in bits/s for the signal y(t) in Fig. 1 ?')
disp('     ')
disp('  Answer: data rate= ??? bits/s ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q3. What is the baud rate in symbols/s for the signal y(t) in Fig. 1 ?')
disp('     ')
disp('  Answer: data rate= ??? symbols/s ')
disp(' ')
disp(' ')

disp('PART 2: pulse shaping =============================')
disp(' ')
disp('  ')

disp(' parameters and definitions:')
clear t k h y f x s p alpha delta dat ptk gs gtk(t,k) gs(t) pnyqt PNYQ
syms t k h y f x s palpha delta dat ptk gs gtk(t,k) gs(t)  
pnyqt(t)=sin(pi*t/T0)/(pi*t/T0)/(1-t/T0)
PYNQ(f)=int( pnyqt(t)*exp(i*2*pi*f*t),t,-5*T0,5*T0)/2/pi;
%pnyq2f(f)=int( t*exp(i*2*pi*f*t),t,-5*T0,5*T0)/2/pi;

hh=figure(2);
sp=subplot(2,1,1);fplot(pnyqt,[-5*T0 5*T0],'LineWidth',3);grid on;%sp.YLim=[0 0.1];
title('pnyqt(t)');xlabel('Freq (Hz)');  
sp=subplot(2,1,2);fplot(abs(PYNQ),[-2/T0 2/T0],'LineWidth',3);grid on;%sp.YLim=[0 0.1];
title('|PYNQ(f)|');xlabel('Freq (Hz)');  
set(findall(hh,'Type','text'),'FontSize',16);
disp('           Fig. 2. Nyquiist pulse. ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q4. For the Nyquist 2 pulse pnyqt(t) in Fig. 2 above, ')
disp('    wwhat is the period T0 of the data?')
disp(' ')
disp('  Answer: T0= ??? ms ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q5. What is data rate in bits/s for the signal pnyqt(t) in Fig. 2')
disp('     ')
disp('  Answer: data rate= ??? bits/s ')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q6. The Nyquist pulse pnyqt(t) in Fig. 2 has zero ISI.')
disp('     ')
disp('  Answer: True/False')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q7. What is the bandwidth of the Nyquist pulse pnyqt(t) in Fig. 2?')
disp('     ')
disp('  Answer: ??? Hz')
disp(' ')
disp(' ')


disp('QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ ')
disp('Q8. What is the capacity in bits/s of a communication channel')
disp('    with 10 kHz bandwidth, 0.001 watts noise, and 0.128 watts signal?')
disp('     ')
disp('  Answer: ???? bits/s')
disp(' ')
disp(' ')