% 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 = 4;set_param('PNGen/PN Sequence Generator','poly','[1 0 0 1 1]');
set_param('PNGen/PN Sequence Generator','ini_sta','[1 0 0 0]');
m = 4;set_param('PNGen/PN Sequence Generator1','poly','[1 1 0 0 1]');
set_param('PNGen/PN Sequence Generator1','ini_sta','[1 0 0 0]');

T_b = 2^m - 1;TN = 2*T_b;
sim('PNGen');e1 = pnB(1:T_b)';e2 = pnB1(1:T_b)';Rc = [e1*e1' e1*e2';e2*e1' e2*e2'];
x = pnB(1:TN);y = pnB1(1:TN); 
for i = 1:TN
    autocorx(i) = x' * [x(i:end);x(1:i-1)];
    autocory(i) = y' * [y(i:end);y(1:i-1)];
    crosscorxy(i) = x' * [y(i:end);y(1:i-1)]; % or
    %crosscoryx(i) = y' * [x(i:end);x(1:i-1)];
end
autocorx = autocorx / TN;autocory = autocory / TN;crosscorxy = crosscorxy / TN;
figure(1);plot(0:TN - 1,autocorx);
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',16)
xlabel('Time ( in number of \itT_c\rm\bf )','FontSize',16,'FontWeight','bold');
ylabel('Normalized autocorrelation for PN sequence e1','FontSize',16,'FontWeight','bold')
figure(2);plot(0:TN - 1,autocory);
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',16)
xlabel('Time ( in number of \itT_c\rm\bf )','FontSize',16,'FontWeight','bold');
ylabel('Normalized autocorrelation PN sequence e2','FontSize',16,'FontWeight','bold')
figure(3);plot(0:TN - 1,crosscorxy);
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',16)
xlabel('Time ( in number of \itT_c\rm\bf )','FontSize',16,'FontWeight','bold');
ylabel('Normalized crosscorrelation between PN seqeunce e1 and e2','FontSize',16,'FontWeight','bold')