-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReedSolomonDecoder.cs
250 lines (206 loc) · 9.14 KB
/
ReedSolomonDecoder.cs
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
using System;
using System.Linq;
namespace TEST_lire_ecrire_image
{
/// <summary> <p>Implements Reed-Solomon decoding, as the name implies.</p>
///
/// <p>The algorithm will not be explained here, but the following references were helpful
/// in creating this implementation:</p>
///
/// <ul>
/// <li>Bruce Maggs.
/// <a href="http://www.cs.cmu.edu/afs/cs.cmu.edu/project/pscico-guyb/realworld/www/rs_decode.ps">
/// "Decoding Reed-Solomon Codes"</a> (see discussion of Forney's Formula)</li>
/// <li>J.I. Hall. <a href="www.mth.msu.edu/~jhall/classes/codenotes/GRS.pdf">
/// "Chapter 5. Generalized Reed-Solomon Codes"</a>
/// (see discussion of Euclidean algorithm)</li>
/// </ul>
///
/// <p>Much credit is due to William Rucklidge since portions of this code are an indirect
/// port of his C++ Reed-Solomon implementation.</p>
///
/// </summary>
/// <author>Sean Owen</author>
/// <author>William Rucklidge</author>
/// <author>sanfordsquires</author>
internal sealed class ReedSolomonDecoder
{
private readonly GenericGF field;
public ReedSolomonDecoder(GenericGF field)
{
this.field = field;
}
/// <summary>
/// <p>Decodes given set of received codewords, which include both data and error-correction
/// codewords. Really, this means it uses Reed-Solomon to detect and correct errors, in-place,
/// in the input.</p>
/// </summary>
/// <param name="received">data and error-correction codewords</param>
/// <param name="twoS">number of error-correction codewords available</param>
/// <returns>false: decoding fails</returns>
public bool Decode(int[] received, int twoS)
{
var poly = new GenericGFPoly(field, received);
var syndromeCoefficients = new int[twoS];
var noError = true;
for (var i = 0; i < twoS; i++)
{
int eval = poly.EvaluateAt(field.Exp(i + field.GeneratorBase));
syndromeCoefficients[syndromeCoefficients.Length - 1 - i] = eval;
if (eval != 0)
noError = false;
}
if (noError)
return true;
var syndrome = new GenericGFPoly(field, syndromeCoefficients);
GenericGFPoly[] sigmaOmega = RunEuclideanAlgorithm(field.BuildMonomial(twoS, 1), syndrome, twoS);
if (sigmaOmega == null)
return false;
GenericGFPoly sigma = sigmaOmega[0];
int[] errorLocations = FindErrorLocations(sigma);
if (errorLocations == null)
return false;
GenericGFPoly omega = sigmaOmega[1];
int[] errorMagnitudes = FindErrorMagnitudes(omega, errorLocations);
for (int i = 0; i < errorLocations.Length; i++)
{
int position = received.Length - 1 - field.Log(errorLocations[i]);
if (position < 0)
{
// throw new ReedSolomonException("Bad error location");
return false;
}
received[position] = GenericGF.AddOrSubtract(received[position], errorMagnitudes[i]);
}
return true;
}
// this method has been added by Sebastien ROBERT ([email protected])
// this implementation makes the mathematician-friendly approach programmer-friendly
public byte[] DecodeEx(byte[] message, byte[] ecc)
{
int[] received = message
.Select(x => (int)x)
.Concat(ecc.Select(x => (int)x))
.ToArray();
if (Decode(received, ecc.Length) == false)
return null;
return received
.Take(message.Length)
.Select(x => (byte)x)
.ToArray();
}
internal GenericGFPoly[] RunEuclideanAlgorithm(GenericGFPoly a, GenericGFPoly b, int R)
{
// Assume a's degree is >= b's
if (a.Degree < b.Degree)
{
GenericGFPoly temp = a;
a = b;
b = temp;
}
GenericGFPoly rLast = a;
GenericGFPoly r = b;
GenericGFPoly tLast = field.Zero;
GenericGFPoly t = field.One;
int halfR = R / 2;
// Run Euclidean algorithm until r's degree is less than R/2
while (r.Degree >= halfR)
{
GenericGFPoly rLastLast = rLast;
GenericGFPoly tLastLast = tLast;
rLast = r;
tLast = t;
// Divide rLastLast by rLast, with quotient in q and remainder in r
if (rLast.IsZero)
{
// Oops, Euclidean algorithm already terminated?
// throw new ReedSolomonException("r_{i-1} was zero");
return null;
}
r = rLastLast;
GenericGFPoly q = field.Zero;
int denominatorLeadingTerm = rLast.GetCoefficient(rLast.Degree);
int dltInverse = field.Inverse(denominatorLeadingTerm);
while (r.Degree >= rLast.Degree && !r.IsZero)
{
int degreeDiff = r.Degree - rLast.Degree;
int scale = field.Multiply(r.GetCoefficient(r.Degree), dltInverse);
q = q.AddOrSubtract(field.BuildMonomial(degreeDiff, scale));
r = r.AddOrSubtract(rLast.MultiplyByMonomial(degreeDiff, scale));
}
t = q.Multiply(tLast).AddOrSubtract(tLastLast);
if (r.Degree >= rLast.Degree)
{
// throw new IllegalStateException("Division algorithm failed to reduce polynomial?");
return null;
}
}
int sigmaTildeAtZero = t.GetCoefficient(0);
if (sigmaTildeAtZero == 0)
{
// throw new ReedSolomonException("sigmaTilde(0) was zero");
return null;
}
int inverse = field.Inverse(sigmaTildeAtZero);
GenericGFPoly sigma = t.Multiply(inverse);
GenericGFPoly omega = r.Multiply(inverse);
return new GenericGFPoly[] { sigma, omega };
}
private int[] FindErrorLocations(GenericGFPoly errorLocator)
{
// This is a direct application of Chien's search
int numErrors = errorLocator.Degree;
if (numErrors == 1)
{
// shortcut
return new int[] { errorLocator.GetCoefficient(1) };
}
int[] result = new int[numErrors];
int e = 0;
for (int i = 1; i < field.Size && e < numErrors; i++)
{
if (errorLocator.EvaluateAt(i) == 0)
{
result[e] = field.Inverse(i);
e++;
}
}
if (e != numErrors)
{
// throw new ReedSolomonException("Error locator degree does not match number of roots");
return null;
}
return result;
}
private int[] FindErrorMagnitudes(GenericGFPoly errorEvaluator, int[] errorLocations)
{
// This is directly applying Forney's Formula
int s = errorLocations.Length;
int[] result = new int[s];
for (int i = 0; i < s; i++)
{
int xiInverse = field.Inverse(errorLocations[i]);
int denominator = 1;
for (int j = 0; j < s; j++)
{
if (i != j)
{
//denominator = field.multiply(denominator,
// GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse)));
// Above should work but fails on some Apple and Linux JDKs due to a Hotspot bug.
// Below is a funny-looking workaround from Steven Parkes
int term = field.Multiply(errorLocations[j], xiInverse);
int termPlus1 = (term & 0x1) == 0 ? term | 1 : term & ~1;
denominator = field.Multiply(denominator, termPlus1);
// removed in java version, not sure if this is right
// denominator = field.multiply(denominator, GenericGF.addOrSubtract(1, field.multiply(errorLocations[j], xiInverse)));
}
}
result[i] = field.Multiply(errorEvaluator.EvaluateAt(xiInverse), field.Inverse(denominator));
if (field.GeneratorBase != 0)
result[i] = field.Multiply(result[i], xiInverse);
}
return result;
}
}
}