]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/prbs_iterator.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / prbs_iterator.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 ## This function generates the output bits from the PRBS
17 ## state, for the number of iterations specified.
18 ##
19 ## First argument is the PRBS structure obtained from prbs_generator.
20 ## PRBS iterations is specified in the second argument.
21 ## PRBS start state is taken from the prbs.sregs.
22 ##
23 ## Second argument of the output is PRBS structure with a new
24 ## state. This allows usage like: 
25 ## 
26 ## [ seq, prbs ] =  prbs_iterator( prbs, niterations );
27 ## 
28 ## while the state of the PRBS is updated.
29 ## 
30 ## Example: If you had a PRBS shift register like the diagram
31 ## below with 4 registers we use representation by polynomial
32 ## of [ 1 2 3 4], and feedback connections between [ 1 3 4 ].
33 ## The output PRBS sequence is taken from the position 4.
34 ## 
35 ##  +---+    +----+   +---+   +---+
36 ##  | D |----| D  |---| D |---| D |
37 ##  +---+    +----+   +---+   +---+
38 ##    |                 |       |
39 ##    \                 /      /
40 ##    [+]---------------+------+
41 ##   1   +    0.D   + 1.D^2 + 1.D^3
42 ##
43 ## The code to implement this PRBS will be 
44 ## prbs=prbs_generator([1 3 4],{[1 3 4]},[1 0 1 1]);
45 ## x = prbs_iterator(prbs,15)
46 ## 
47 ## See Also: This function is to be used along with functions 
48 ## prbs_iterator, prbs_generator and prbs_sequence.
49
50 function [outputseq, prbs] = prbs_iterator (prbs, iterations = 2^(prbs.reglen)-1)
51
52   if ( nargin < 1 || nargin > 2 )
53     print_usage;
54   endif
55   outputseq=zeros(1,iterations);
56   nstate=zeros(1,prbs.reglen);
57   
58   ## For each iteration, shift the output bit. Then compute the xor pattern of connections. 
59   ## Finally apply feedback the stuff. Insert the computed pattern.
60   for itr=1:iterations
61     ## save output.
62     outputseq(itr)=prbs.sregs(prbs.reglen);
63     
64     ## compute the feedback.
65     for itr2=1:prbs.conlen
66       val=0;
67       L=length(prbs.connections{itr2});
68       for itr3=2:L
69         val=bitxor(val,prbs.sregs(prbs.connections{itr2}(itr3)));
70       endfor
71       nstate(prbs.connections{itr2}(1))=val;
72     endfor
73     
74     ## rotate the output discarding the last output.
75     prbs.sregs=[0 prbs.sregs(1:prbs.reglen-1)];
76
77     ## insert the feedback.
78     for itr2=1:prbs.conlen
79       prbs.sregs(itr2)=nstate(itr2);
80       nstate(itr2)=0; # reset.
81     endfor
82     
83   endfor
84 endfunction
85
86 ##
87 ##  TEST CASES FOR PRBS.
88 ##
89 ##
90 ##  2^31 -1 : D31 + D28 + 1 =0  inverted 
91 ##  2^23 -1 : D23 + D18 + 1 = 0 ,
92 ##  2^15 -1 : D15 + D14 + 1 = 0,
93 ##  2^10 -1 : D10 + D7 + 1 = 0,
94 ##  2^7  -1 : D7 + D6 + 1 = 0,
95 ##  2^4  -1 : D3 + D2 + 1 = 0,
96 ##
97 ##  +---+    +----+   +---+   +---+
98 ##  | D |----| D  |---| D |---| D |
99 ##  +---+    +----+   +---+   +---+
100 ##    |                 |       |
101 ##    \                 /      /
102 ##    [+]---------------+------+
103 ##   1   +    0.D   + 1.D^2 + 1.D^3
104 ##
105 ##
106 ## prbs=prbs_generator([1 3 4],{[1 3 4]},[1 0 1 1]);
107 ## x = prbs_iterator(prbs,15)
108 ## y = prbs_iterator(prbs,30)(16:end)
109 ## z = prbs_sequence(prbs)
110 ## exit
111 ## break
112
113 ##
114 ## Multiple Tap, Simple Sequence Generator.
115 ##
116 ## n=10;
117 ## k=8;
118 ## inits=round(abs(rand(1,n)*1.0))
119 ## prbs=prbs_generator([n 1],{[1 2 k n-1 n]},inits);
120 ## prbs_iterator(prbs,1023)
121 ## prbs_seqlength(prbs,inits)
122
123 ##prbs=prbs_generator([1 2 3],{[1 2 3]},[1 1 1]);
124 ##prbs_iterator(prbs,7)
125
126 ##
127 ##  2^4  -1 : D4 + D1 + 1 = 0,
128 ##
129 ##  +---+    +----+   +---+   +---+
130 ##  | D |----| D  |---| D |---| D |
131 ##  +---+    +----+   +---+   +---+
132 ##    |        |                |
133 ##    \        /                /
134 ##    [+]---------------+------+
135 ##   1   +    0.D   + 1.D^2 + 1.D^3
136 ##
137 ##prbs=prbs_generator([1 3 4],{[1 2 4]},[1 0 1 1]);
138 ##prbs_iterator(prbs,16)
139 ##prbs_iterator(prbs,32)
140
141
142 ##prbs=prbs_generator([7],{[1 7 6]},[1 0 0 1 0 0 0]);
143 ##y=prbs_iterator(prbs,128);
144 ##x=prbs_iterator(prbs,256);