[b04b82]: inst / reedmullerdec.m Maximize Restore History

Download this file

reedmullerdec.m    263 lines (239 with data), 7.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
## Copyright (C) 2007 Muthiah Annamalai <muthiah.annamalai@uta.edu>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
##
## -*- texinfo -*-
## @deftypefn {Function File} {} reedmullerdec (@var{VV},@var{G},@var{R},@var{M})
##
## Decode the received code word @var{VV} using the RM-generator matrix @var{G},
## of order @var{R}, @var{M}, returning the code-word C. We use the standard
## majority logic vote method due to Irving S. Reed. The received word has to be
## a matrix of column size equal to to code-word size (i.e @math{2^m}). Each row
## is treated as a separate received word.
##
## The second return value is the message @var{M} got from @var{C}.
##
## G is obtained from definition type construction of Reed Muller code,
## of order @var{R}, length @math{2^M}. Use the function reedmullergen,
## for the generator matrix for the (@var{R},@var{M}) order RM code.
##
## Faster code constructions (also easier) exist, but since
## finding permutation order of the basis vectors, is important, we
## stick with the standard definitions. To use decoder
## function reedmullerdec, you need to use this specific
## generator function.
##
## see: Lin & Costello, Ch.4, "Error Control Coding", 2nd Ed, Pearson.
##
## @example
## @group
## G=reedmullergen(2,4);
## M=[rand(1,11)>0.5];
## C=mod(M*G,2);
## [dec_C,dec_M]=reedmullerdec(C,G,2,4)
##
## @end group
## @end example
##
## @end deftypefn
## @seealso{reedmullergen,reedmullerenc}
## FIXME: make possible to use different generators, if permutation
## matrix (i.e polynomial vector elements of rows of G are given
function [C,CMM]=reedmullerdec(VV,G,R,M)
if ( nargin < 4 )
print_usage();
end
%
% we do a R+1 level majority logic decoding.
% at each order of polynomial modifying the code-word.
%
U=0:M-1; %allowed basis vectors in V2^M.
C=-1*ones(size(VV)); %preset the output word.
[Rows,Cols]=size(G);%rows shadows 'rows()'
%
%first get the row index of G & its corresponding permutation
%elements.
%
P{1}=[0];
for idx=1:M
P{idx+1}=idx;
end
idx=idx+1;
Ufull=1:M;
r=2;
while r <= R
TMP=nchoosek(Ufull,r);
for idy=1:nchoosek(M,r)
P{idx+idy}=TMP(idy,:);
end
idx=idx+idy;
r=r+1;
end
%
%enter majority logic decoding loop, R+1 order polynomial,
%but we do it here for n-k times, both are equivalent.
%
NCODES=size(VV);
NCODES=NCODES(1);
v_adjust=[];
for row_v=1:1:NCODES
V=VV(row_v,:);
CM=-1*ones(1,Rows);
%
%Now start at bottom row, and get the index set,
%for each until the 2nd most row.
%
%special case, r=0, parity check, so just sum-up.
if ( R == 0 )
wt=__majority_logic_vote(V);
CMM(row_v,:)=wt;
C(row_v,:)=mod(wt*G,2);
continue;
end
order=R;
Gadj=G;
prev_len=length(P{Rows});
for idx=Rows:-1:1
%
%adjust the 'V' received vector, at change of each order.
%
if ( prev_len ~= length(P{idx}) || idx == 1 ) %force for_ idx=1
v_adjust=mod(CM(idx+1:end)*Gadj(idx+1:end,:),2);%effect of iterative decoding.
Gadj(idx+1:end,:)=0;
V=mod(V+ v_adjust,2); % + = - in GF(2).
order = order - 1;
if ( order == 0 ) %special handling of the all-1's basis vector.
CM(idx)=__majority_logic_vote(V);
break
end
end
prev_len=length(P{idx});
Si=P{idx};% index identifier
Si=sort(Si,'descend');
%generate index elements
B=__binvec([0:(2.^length(Si)-1)]');
WTS=2.^[Si-1];
%actual index set elements.
S=sum(B.*repmat(WTS,[2^length(Si),1]),2);
%doing the operation set difference U \ S to get SCi
SCi=U;
Si_diff=Si-1;
%a matrix approach removing vector elements
SS_mat=repmat(S,1,M);
MM_mat=repmat(0:M-1,length(S),1);
[idx_r,idx_c]=find(SS_mat-MM_mat);
SCi(idx_c)=[];
SCi=sort(SCi,'descend');
%corner case RM(r=m,m) case
if (length(SCi) > 0 )
%generate the set SC,
B=__binvec([0:(2.^length(SCi)-1)]');
WTS=2.^[SCi];
%actual index set elements.
SC=sum(B.*repmat(WTS,[2^length(SCi),1]),2);
else
SC=[0]; %default, has to be empty set mathematically;
end
%
%next compute the checksums & form the weights.
%
SC=reshape(SC,length(SC),1);
Srow=reshape(S,1,length(S));
SC_mat=repmat(SC,1,length(S));
S_mat=repmat(Srow,length(SC),1);
idx_mat=SC_mat + S_mat+1;
Vr=reshape(V,2^M,1);
Vr_mat=repmat(V,1,length(S));
wts=mod(sum(reshape(Vr_mat(idx_mat(:)),size(idx_mat)),2),2);
%
%do the majority logic vote.
%
CM(idx)=__majority_logic_vote(wts);
end
CMM(row_v,:)=CM;
C(row_v,:)=mod(CM*G,2);
end
return;
end
%
% utility functions
%
function bvec=__binvec(dec_vec)
%dec_vec=reshape(dec_vec,length(dec_vec),1); %expect col-vec
maxlen=ceil(log2(max(dec_vec)+1));
x=[]; bvec=zeros(length(dec_vec),maxlen);
for idx=maxlen:-1:1
tmp=mod(dec_vec,2);
bvec(:,idx)=tmp;
dec_vec=(dec_vec-tmp)./2;
end
return
end
%
% crude majority logic decoding; force the = case to 0 by default.
%
function wt=__majority_logic_vote(wts)
wt=[sum(wts) > sum(1-wts)];%count no of 1's - no of 0's.
end
%
% majority logic decoding, tie-break using random.
%
function wt=__majority_logic_vote_random(wts)
wt=(1+sign( sum(wts)-sum(1-wts) ))/2;
if ( wt == 0.5 )
wt = (rand()>0.5);
end
end
% test cases
%G=[1 1 1 1,1 1 1 1;
% 0 1 0 1,0 1 0 1;
% 0 0 1 1,0 0 1 1;
% 0 0 0 0 1 1 1 1];
%m=[1 0 0 1];
%c=mod(m*G,2);
%c(1)=1-c(1); %corrects errors!
%[dc,dm]=reedmullerdec(c,G,1,3)
%pause
%
%G=reedmullergen(1,4);
%m=[1 0 0 0 1];
%c=mod(m*G,2);
%[dc,dm]=reedmullerdec(c,G,1,4)
%pause
%
%G=reedmullergen(3,4);
%m=[ones(1,15)];
%c=mod(m*G,2);
%[dc,dm]=reedmullerdec(c,G,3,4)
%pause
%
%G=reedmullergen(2,3);
%m=[0 0 0 1 1 1 1]
%c=mod(m*G,2)
%[dc,dm]=reedmullerdec(c,G,2,3)
%pause
%
%G=reedmullergen(3,3);
%c1=mod([ones(1,8)]*G,2);
%c2=mod([ones(1,4),zeros(1,4)]*G,2);
%[dC,dM]=reedmullerdec([c2;c2;c1;c2],G,3,3)
%
% %special case of repetition code.
% G=reedmullergen(0,3);
% G
% c1=1*G;
% c2=0*G; C=[c1; c2]
% [dC,dM]=reedmullerdec(C,G,0,3)
%