]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/egolaydec.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / egolaydec.m
1 ## Copyright (C) 2007 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 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {}  egolaydec (@var{R})
18 ## 
19 ## Given @var{R}, the received Extended Golay code, this function tries to
20 ## decode @var{R} using the Extended Golay code parity check matrix.
21 ## Extended Golay code (24,12) which can correct upto 3 errors.
22 ##
23 ## The received code @var{R}, needs to be of length Nx24, for encoding. We can
24 ## decode several codes at once, if they are stacked as a matrix of 24columns,
25 ## each code in a separate row.
26 ##
27 ## The generator G used in here is same as obtained from the
28 ## function egolaygen. 
29 ##
30 ## The function returns the error-corrected code word from the received
31 ## word. If decoding failed, the second return value is 1, otherwise it is 0.
32 ## 
33 ## Extended Golay code (24,12) which can correct upto 3
34 ## errors. Decoding algorithm follows from Lin & Costello.
35 ## 
36 ## Ref: Lin & Costello, pg 128, Ch4, 'Error Control Coding', 2nd ed, Pearson.
37 ##
38 ## @example
39 ## @group
40 ##  M=[rand(10,12)>0.5]; 
41 ##  C1=egolayenc(M); 
42 ##  C1(:,1)=mod(C1(:,1)+1,2)
43 ##  C2=egolaydec(C1)
44 ## @end group
45 ## @end example
46 ##
47 ## @end deftypefn
48 ## @seealso{egolaygen,egolayenc}
49
50 function [C,dec_error]=egolaydec(R)
51
52   if ( nargin < 1 )
53     error('usage: C=egolaydec(R)');
54   elseif ( columns(R) ~= 24 )
55     error('extended golay code is (24,12), use rx codeword of 24 bit column size');
56   end
57
58   I=eye(12);
59                                 %P is 12x12 matrix
60   P=[1 0 0 0 1 1 1 0 1 1 0 1;
61      0 0 0 1 1 1 0 1 1 0 1 1;
62      0 0 1 1 1 0 1 1 0 1 0 1;
63      0 1 1 1 0 1 1 0 1 0 0 1;
64      1 1 1 0 1 1 0 1 0 0 0 1;
65      1 1 0 1 1 0 1 0 0 0 1 1;
66      1 0 1 1 0 1 0 0 0 1 1 1;
67      0 1 1 0 1 0 0 0 1 1 1 1;
68      1 1 0 1 0 0 0 1 1 1 0 1;
69      1 0 1 0 0 0 1 1 1 0 1 1;
70      0 1 0 0 0 1 1 1 0 1 1 1;
71      1 1 1 1 1 1 1 1 1 1 1 0;];
72
73   H=[I; P]; %partiy check matrix transpose.
74
75   dec_error=[];
76   C=zeros(size(R));
77
78   for rspn=1:rows(R)
79     RR=R(rspn,:);
80     S=mod(RR*H,2);
81     wt=sum(S);
82     done=0;
83     if (wt <= 3)
84       E=[S, zeros(1,12)];
85       done=1;
86     else
87       SP = mod(repmat(S,[12, 1])+P,2);
88       idx = find( sum(SP,2) <= 2 );    
89       if ( idx )
90         idx=idx(1); %pick first of matches.
91         Ui=zeros(1,12); Ui(idx)=1;
92         E=[SP(idx,:),Ui];
93         done=1;
94       end
95     end
96
97     if ( ~done )
98       X=mod(S*P,2);
99       wt=sum(X);
100       if (wt==2 || wt==3)
101         E=[zeros(1,12), X];
102         done=1;
103       else
104         SP = mod(repmat(X,[12, 1])+P,2);
105         idx = find( sum(SP,2) == 2 );
106         if ( idx )
107           idx=idx(1);
108           Ui=zeros(1,12); Ui(idx)=1;
109           E=[Ui,SP(idx,:)];
110           done=1;
111         end
112       end
113     end
114
115     dec_error=[dec_error; 1-done];
116     C(rspn,:)=mod(E+RR,2);
117   end
118
119   return;
120 end
121                                 %!
122                                 %!assert(egolaydec([1 1 1 zeros(1,21)]),zeros(1,24))
123                                 %!assert(egolaydec([1 0 1 zeros(1,20) 1]),zeros(1,24))
124                                 %!
125
126