% This programme computes and plots the autocorrelation of a PN Sequence
% Generator function. It drives PNGen.mdl, which must be openned at the time of running  (15.10.2008)
clear;clc ;clf reset;close all;warning off MATLAB:divideByZero
m = 3;set_param('PNGenExp3/PN Sequence Generator','poly','[1 1 0 1]');
set_param('PNGenExp3/PN Sequence Generator','ini_sta','[1 0 0]');% Alternative max length generator polynýmial from Proakis
m = 3;set_param('PNGenExp3/PN Sequence Generator1','poly','[1 0 1 1]');
set_param('PNGenExp3/PN Sequence Generator1','ini_sta','[1 0 0]');
% m = 5;set_param('PNGenExp3/PN Sequence Generator','poly','[5 3 2 1 0]');
% set_param('PNGenExp3/PN Sequence Generator','ini_sta','[1 0 0 0 0]');
% %m = 5;set_param('PNGenExp3/PN Sequence Generator','poly','[5 4 3 2 0]');
% set_param('PNGenExp3/PN Sequence Generator1','ini_sta','[1 0 0 0 0]');
% % m = 5;set_param('PNGenExp3/PN Sequence Generator','poly','[5 3 0]'); %% Alternative 
% % m = 5;set_param('PNGenExp3/PN Sequence Generator','poly','[5 4 3 1 0]'); %% Alternative
% m = 5;set_param('PNGenExp3/PN Sequence Generator1','poly','[5 4 2 1 0]'); %% Alternative
% m = 6;set_param('PNGenExp3/PN Sequence Generator','poly','[6 5 0]');
% m = 6;set_param('PNGenExp3/PN Sequence Generator','poly','[6 4 3 1 0]')
% m = 6;set_param('PNGenExp3/PN Sequence Generator','poly','[6 5 3 2 0]')
% set_param('PNGenExp3/PN Sequence Generator','ini_sta','[0 0 0 0 0 1]');% Alternative max length generator polynýmial from Proakis
% m = 6;set_param('PNGenExp3/PN Sequence Generator1','poly','[6 1 0]');
% set_param('PNGenExp3/PN Sequence Generator1','ini_sta','[0 0 0 0 0 1]');
% m = 7;set_param('PNGenExp3/PN Sequence Generator','poly','[7 6 0]');
% set_param('PNGenExp3/PN Sequence Generator','ini_sta','[0 0 0 0 0 0 1]');
% m = 7;set_param('PNGenExp3/PN Sequence Generator','poly','[7 1 0]'); % Alternative max length generator polynýmial from Proakis
% set_param('PNGenExp3/PN Sequence Generator','ini_sta','[0 0 0 0 0 0 1]');
% m = 8;set_param('PNGenExp3/PN Sequence Generator','poly','[8 6 5 4 0]');
% set_param('PNGenExp3/PN Sequence Generator','ini_sta','[0 0 0 0 0 0 0 1]');
% m = 8;set_param('PNGenExp3/PN Sequence Generator','poly','[8 6 5 1 0]');% Alternative max length generator polynýmial from Proakis
% set_param('PNGenExp3/PN Sequence Generator','ini_sta','[0 0 0 0 0 0 0 1]');
% m = 12;set_param('PNGenExp3/PN Sequence Generator','poly','[8 6 5 4 0]');
% set_param('PNGenExp3/PN Sequence Generator','ini_sta','[0 0 0 0 0 0 0 1]');
% m = 12;set_param('PNGenExp3/PN Sequence Generator','poly','[8 6 5 1 0]');% Alternative max length generator polynýmial from Proakis
% set_param('PNGenExp3/PN Sequence Generator','ini_sta','[0 0 0 0 0 0 0 1]');
T_b = 2^m - 1;TN = 1*T_b;
sim('PNGenExp3');
e1 = pnB(1:TN);e2 = pnB1(1:TN);
for i = 1:TN
    autocorc1(i) = e1' * [e1(i:end);e1(1:i-1)];
    autocorc2(i) = e2' * [e2(i:end);e2(1:i-1)];
    crosscorc1c2(i) = e1' * [e2(i:end);e2(1:i-1)]; 
end
autocorc1 = autocorc1 / TN;autocorc2 = autocorc2 / TN;crosscorc1c2 = crosscorc1c2 / TN;
figure(1);plot(0:TN - 1,autocorc1,'-k','LineWidth',3);
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',16)
xlabel('Time ( in number of \itT_c\rm\bf )','FontSize',16,'FontWeight','bold');
ylabel('\bfR_e, \rmautocorrelation for PN sequence  \bfe_1','FontSize',16,'FontWeight','bold')
%axis ([0 1*TN min(autocorc1)*1.05 max(autocorc1)*1.05]);set(gca,'FontSize',12)
figure(2);plot(0:TN - 1,autocorc2,'-k','LineWidth',3);
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',16)
xlabel('Time ( in increments of \itT_c\rm\bf )','FontSize',16,'FontWeight','bold');
ylabel('\bfR_e , \rmautocorrelation for PN sequence  \bfe_2','FontSize',16,'FontWeight','bold')
figure(3);plot(0:TN - 1,crosscorc1c2,'-k','LineWidth',3);
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',16)
xlabel('Time ( in increments of \itT_c\rm\bf )','FontSize',16,'FontWeight','bold');
ylabel('\bfR_e\fontsize{7}_1\fontsize{16}_e\fontsize{7}_2\fontsize{16}  crosscorrelation between PN seqeunce e_1 and e_2','FontSize',16,'FontWeight','bold')