You are on page 1of 2

Exp.

1: Generation of PN sequence
Code-1 (Fixed sequence code)
clc;
clear all;
close all;

%% length of the shift register


m=4;

%% Assign Initial value for PN generator


x1= 1;
x2= 0;
x3 =0;
x4 =0;

%% period of the PN sequence


N = 2.^m-1;

for i=1:N
p=x4;
x4=x3;
x3=x2;
x2=x1;
%% feed back taps at [4 1]
x1=xor(x1,x4);
disp (i)
x = [x1 x2 x3 x4];
m_Seq(i)=p;
disp (x)
end
disp('the resultant m-sequence is :');
disp(m_Seq)

Code-2 (Generalized code)


clc;
clear all;
close all;
m=input('enter the length of shiftregister m= ');
N=2^m-1;
tp=input('enter the valid tap array :');
in=input('enter the sequence of input data array of length m : ');
if m~=length(in)
fprintf('\n \n error : length of input data array must be equal to the length of shift registers: \n
\n');
else
out=in;
for k=1:N
t_out=out(tp(1));
for i=1:length(tp)-1
t_out=xor(t_out,out(tp(i+1)));
end;
tm1=m;
M_seq(k)=out(m);
for j=1:m-1
out(tm1)=d_ff(out(tm1-1));
tm1=tm1-1;
end;
out(1)=t_out;
if k~=N
if out==in
fprintf('\n \n error : given tap input array is not valid : \n \n');
break;
end;
end;
if k==N
if out==in
fprintf('lenth of the m_sequence N = %d \n',length(M_seq));
fprintf('M_sequence is : \n');
disp(M_seq);
corre=correlation(M_seq);
t=-2*N:1:2*N;
grid on;
plot(t,corre);
grid on;axis([-2*N-1 2*N+1 -2 N+1]);
title('autocorrelation of M_sequence : ');
xlabel('timeperiod');
ylabel('amplitude');
else
fprintf(' \n \n error : given tap input array is not valid :\n \n');
end;
end;
end;
end;

You might also like