]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/prbs_sequence.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / prbs_sequence.m
1 ## Copyright (C) 2006 Muthiah Annamalai <muthiah.annamalai@uta.edu>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## Implement book keeping for a Pseudo-Random Binary Sequence ( PRBS )
17 ## also called as a Linear Feedback Shift Register.
18 ## 
19 ## For the given PRBS in a intial state, compute the PRBS sequence length.
20 ## Length is period of output when the PRBS state is same as 
21 ## the start state of PRBS.
22 ## 
23 ## Example: If you had a PRBS shift register like the diagram
24 ## below with 4 registers we use representation by polynomial
25 ## of [ 1 2 3 4], and feedback connections between [ 1 3 4 ].
26 ## The output PRBS sequence is taken from the position 4.
27 ## 
28 ##  +---+    +----+   +---+   +---+
29 ##  | D |----| D  |---| D |---| D |
30 ##  +---+    +----+   +---+   +---+
31 ##    |                 |       |
32 ##    \                 /      /
33 ##    [+]---------------+------+
34 ##   1   +    0.D   + 1.D^2 + 1.D^3
35 ##
36 ## The code to implement this PRBS will be 
37 ## prbs=prbs_generator([1 3 4],{[1 3 4]},[1 0 1 1]);
38 ## x = prbs_sequence(prbs) #gives 15
39 ## 
40 ##
41 ## See Also: This function is to be used along with functions 
42 ## prbs_generator.
43
44 function [itrs,seq]=prbs_sequence(prbs)
45   if nargin != 1
46     print_usage;
47   endif
48   nstate=zeros(1,prbs.reglen);
49   itrs=0; seq = [];
50   inits = prbs.sregs;
51   
52   ## For each iteration, shift the output bit. Then compute the xor pattern of connections. 
53   ## Finally apply feedback the stuff. Insert the computed pattern.
54   while( true )
55     itrs = itrs + 1;
56     
57     ## compute the feedback.
58     for itr2=1:prbs.conlen
59       val=0;
60       L=length(prbs.connections{itr2});
61       for itr3=2:L
62         val=bitxor(val,prbs.sregs(prbs.connections{itr2}(itr3)));
63       endfor
64       nstate(prbs.connections{itr2}(1))=val;
65     endfor
66     
67     ## rotate the output discarding the last output.
68     seq = [seq, prbs.sregs(end)];
69     prbs.sregs=[0 prbs.sregs(1:prbs.reglen-1)];
70
71     ## insert the feedback.
72     for itr2=1:prbs.conlen
73       prbs.sregs(itr2)=nstate(itr2);
74       nstate(itr2)=0; # reset.
75     endfor
76     
77     if(isequal(prbs.sregs,inits))
78       break
79     endif
80   endwhile
81 end