0% found this document useful (0 votes)
186 views2 pages

Function To Generate PN Sequence

This MATLAB code defines a function to generate a PN sequence. It takes in three inputs: the number of flip-flops, the tap function as a vector, and the initial state vector. It generates the PN sequence by performing XOR operations on the current and previous states based on the tap function polynomial and stores the output in a vector. The function outputs the generated PN sequence.

Uploaded by

Fetsum Lakew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views2 pages

Function To Generate PN Sequence

This MATLAB code defines a function to generate a PN sequence. It takes in three inputs: the number of flip-flops, the tap function as a vector, and the initial state vector. It generates the PN sequence by performing XOR operations on the current and previous states based on the tap function polynomial and stores the output in a vector. The function outputs the generated PN sequence.

Uploaded by

Fetsum Lakew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Function to generate PN sequence

To use this function just define a variable say var,


var = pnseq(a, b, c);
for a, b and c refer to the zip file
%This code generates PN sequence and is written by Ashwini Patankar.
%You can reach me through [email protected]
%My Home page: www.ashwinipatankar.com
%My Personal Blog: wwww.ashinipatankar.wordpress.com
%Blog on Wireless Engineering : www.wirelesscafe.wordpress.com
% Feel free to ask querries related to wirelss engineering, matlab and ns2
function [op_seq] = pnseq (a, b, c)
% a : no of fliflops; b = tapp _ function starting frm highest order; c =
initial stae
%tapp functions or genrator polynomial
%e.g. no of flip flops 4 ==> a = 4
%generator polynomial x4+x+1 ==> b = [1 0 0 1]
%initial state [1 0 0 0] ==> c = [1 0 0 0]
%refere figure to set a relation between tap function and initial state
%
%
%
%
% <
% -----------X____________________________________
% | ___ | _____ ____ _____ |
% |> | | | | | | | | | ^|
% -----| |--------| |---| |-------| |------------->o/p
% ----- ----- ----- ----
% x1 + x2 + x3 + x4
%initial state
% 1 0 0 1
%
%take care of the reverse order of genrator polynomial and intiial states
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all;
clc;
x = a;
tap_ff =b;
int_stat= c;
for i = 1:1: length(int_stat)
old_stat(i) = int_stat(i);
gen_pol(i) = tap_ff(i);
end
len = (2 ^x)-1;
gen_pol(i+1)= 1;
gen_l = length(gen_pol);
old_l = length(old_stat);
for i1 = 1: 1:len
% feed back input genration
t = 1;
for i2 = 1:old_l
if gen_pol(i2)==1
stat_str(t) = old_stat(gen_l - i2);
i2 = i2+1;
t = t+1;
else
i2 = i2+1;
end
end
stat_l = length(stat_str);
feed_ip = stat_str(1);
for i3 = 1: stat_l-1
feed_ip = bitxor(feed_ip,stat_str(i3 + 1));
feed_ipmag(i1) = feed_ip;
i3 = i3+1;
end
% shifting elements
new_stat = feed_ip;
for i4 = 1:1:old_l
new_stat(i4+1) = old_stat(i4);
old_stat(i4)= new_stat(i4);
end
op_seq(i1) = new_stat(old_l +1);
end
%op_seq;

Matlab code to generate 1023 PN sequence.


I am using this code to generate a PN sequence with 1023 length.

h = commsrc.pn(GenPoly, [[10 7 0]], Mask, [0 0 0 0 0 0 0 0 0 1]);


set(h, NumBitsOut, 1023);
pn = generate(h);
len = length(pn);
for i=1:len
if(pn(i) == 0)
pn(i) = -1;
end
end
peak = xcorr(pn);
plot(peak,DisplayName,peak,YDataSource,peak);figure(gcf);

You might also like