A note about PN Sequence

PN sequences or Pseudo Noise sequence is a periodic binary code which is random in nature generated by the use of shift registers, but generated with taking into considerations some generator polynomials. For sequence to be a pseudo noise or pseudo random it should follow the following basic rules. The rules mentioned below are simply mentioned in brief:

  1. The relative frequency of 0’s and 1’s are each ½
  2. The run lengths of 0’s and 1’s are: ½ of all run lengths are of length 1; ¼ are of length 2; 1/8 are of length 3; and so on.
  3. If a PN sequence is shifted by any non zero number of elements, the resulting sequence will have an equal number of agreements and disagreements with respect to the original sequence.

These properties are known also known as balance property, run property, and correlation property respectively. This code is orthogonal in nature. PN sequence is also known as Maximal Length Sequences.

PN sequences are used in spread spectrum systems, like CDMA, WCDMA, and Radar etc. In CDMA IS 95, 64 ling PN sequence codes are used for the identification if the reverse link channels.

The matlab script for generating pn sequence is given below, and also can be downloaded from this link.

refer figure as a companion for the matlab file


block diagram of pn sequence generator

block diagram of pn sequence generator



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;


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);


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;


i2 = i2+1;



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;


% 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);


op_seq(i1) = new_stat(old_l +1);




Author: Ashwini Patankar

31 responses to “A note about PN Sequence

  1. hi ashwini…
    I’ve a problem in running this code.

    stat_str(t) = old_stat(gen_l – i2)

    in this expression matlab gives an error.
    Can you please re look at this code.

  2. hi ashwini

    (gen_l – i2)…in this matlab gives en error…
    it says the input character is not valid in MATLAB expression(the minus sign)

  3. hi dinesh,

    I tested the code, and i found you are getting error because you are passing -ve value to some argument. for test, i used the standard example define in the beginning of the code i.e. pnseq(a,b,c)
    a no of flipflops = 4
    b generator polynomial x^4+x+1 = [1 0 0 1]
    c initial state = [1 0 0 0]
    i think you had pass some negative value in the initial state.

    what are the inputs you had given ?

  4. i needed this type of code so i just copy the whole code n paste in matlab…n i came up with this error…

  5. what’s the function of if statement in this code….

    in the argument given below

    stat_str(t) = old_stat(gen_l – i2)

  6. Hello Dinesh,

    the function stat_str(t) = old_stat(gen_l -i2) is used for following reason:
    old_stat(i) = int_stat(i);
    gen_pol(i) = tap_ff(i);

    In old_stat we are saving the states and in gen_pol is used to save the tap function. say you are having a tap function x4+x+1 which simply means you will xor 1 flip flops’s state and fourth flip flop states and then feed the result to input. The function you asked for is doing the same, we define the flip flops to xor by 1, by checking which flip flops out of 4 (in above example) is to be xored (in tap function it is denoted by 1) we will save its current state in a temp variable or array (for more than 1 flip flop eg x4+x2+x+1) and then we will xor them. stat_str is that temp array and the statement you asked for is doing the same thing of saving into temp array according to the value in tap function.

  7. hallo Ashwini,

    i m a beginner in matlab and need some help. would you plz make some comments to ur matlab-code so i can understand how it works.


  8. hello Ashwini,

    i didnt understand how does ur matlab-code work?
    would u plz make some more comments about it..


  9. Hi Ashwini
    Do you know smth abt WIMAX. M doing a project on channel estimation of WIMAX. I dont know where to begin with. Can you help me with this.


  10. Hi Ashwani,

    Do you have any code or can you help me related to mimo-ofdm. Ive done the siso case but facing some doubts related to mimo case. I can e-mail you a brief description if you will to help me.


  11. Help!!!!!!!!!!!!!!!!!!!!!!!!!!!Dear Mr.Patankar,
    Help to first thank you for your willingness to help people.
    I am a student and study for my work, I had a PN generator with 12 sequences ==> a = 12
    and the initial state is [111111111111] ==> c = [111111111111]
    Now how should be the generator polynomial? ==> B =?
    I am very happy to be a quick answer and I thank you for the very forward to help.
    Best regards
    Mr. Camara

  12. hi Ashwini ..
    when i want to run this code by putting a= 4 , b= x^4+x+1 and c=[1 0 0 0]
    or any other combinations , i am getting the following error

    Attempted to access tap_ff(2); index out of bounds because

    help me

  13. Hello Ashwini
    I can your matlab code and check the results as well, but its not matching when i use inbuilt function (comm.pnsequence in matlab). Please can u help how to generate gold sequence with two polynomials

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s