Added FEC algos

pull/1/head
Wijnand Modderman-Lenstra 9 years ago
parent a7136447fc
commit 7f90da765e

@ -0,0 +1,2 @@
// Package fec implementes Forward Error Correction algorithms
package fec

@ -0,0 +1,37 @@
package fec
import (
"fmt"
"github.com/pd0mz/go-dmr/bit"
)
func Golay_20_8_Parity(bits bit.Bits) bit.Bits {
var p = make(bit.Bits, 12)
p[0] = bits[1] ^ bits[4] ^ bits[5] ^ bits[6] ^ bits[7]
p[1] = bits[1] ^ bits[2] ^ bits[4]
p[2] = bits[0] ^ bits[2] ^ bits[3] ^ bits[5]
p[3] = bits[0] ^ bits[1] ^ bits[3] ^ bits[4] ^ bits[6]
p[4] = bits[0] ^ bits[1] ^ bits[2] ^ bits[4] ^ bits[5] ^ bits[7]
p[5] = bits[0] ^ bits[2] ^ bits[3] ^ bits[4] ^ bits[7]
p[6] = bits[3] ^ bits[6] ^ bits[7]
p[7] = bits[0] ^ bits[1] ^ bits[5] ^ bits[6]
p[8] = bits[0] ^ bits[1] ^ bits[2] ^ bits[6] ^ bits[7]
p[9] = bits[2] ^ bits[3] ^ bits[4] ^ bits[5] ^ bits[6]
p[10] = bits[0] ^ bits[3] ^ bits[4] ^ bits[5] ^ bits[6] ^ bits[7]
p[11] = bits[1] ^ bits[2] ^ bits[3] ^ bits[5] ^ bits[7]
return p
}
func Golay_20_8_Check(bits bit.Bits) error {
if len(bits) != 20 {
return fmt.Errorf("fec/golay_20_8: expected 20 bits, got %d", len(bits))
}
parity := Golay_20_8_Parity(bits[:8])
for i := 0; i < 20; i++ {
if parity[i] != bits[8+i] {
return fmt.Errorf("fec/golay_20_8: parity error at bit %d: %q != %q", i, parity.String(), bits[8:].String())
}
}
return nil
}

@ -0,0 +1,298 @@
package fec
var (
golayGenerator = [12]uint32{
0x063a, 0x031d, 0x07b4, 0x03da,
0x01ed, 0x06cc, 0x0366, 0x01b3,
0x06e3, 0x054b, 0x049f, 0x0475,
}
golayMatrix = [2048]uint32{
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0048,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0824,
0x0000, 0x0000, 0x0000, 0x0301, 0x0000, 0x0400, 0x0090, 0x0002,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0048,
0x0000, 0x0000, 0x0000, 0x0048, 0x0000, 0x0048, 0x0048, 0x0048,
0x0000, 0x0000, 0x0000, 0x0010, 0x0000, 0x0001, 0x0602, 0x0180,
0x0000, 0x0086, 0x0800, 0x0420, 0x0120, 0x0a10, 0x0005, 0x0048,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0500,
0x0000, 0x0000, 0x0000, 0x0004, 0x0000, 0x0222, 0x0090, 0x0801,
0x0000, 0x0000, 0x0000, 0x0042, 0x0000, 0x0001, 0x0090, 0x0208,
0x0000, 0x0808, 0x0090, 0x0420, 0x0090, 0x0144, 0x0090, 0x0090,
0x0000, 0x0000, 0x0000, 0x0a80, 0x0000, 0x0001, 0x0020, 0x0016,
0x0000, 0x0110, 0x0003, 0x0420, 0x0c04, 0x0080, 0x0300, 0x0048,
0x0000, 0x0001, 0x010c, 0x0420, 0x0001, 0x0001, 0x0840, 0x0001,
0x0240, 0x0420, 0x0420, 0x0420, 0x000a, 0x0001, 0x0090, 0x0420,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0500,
0x0000, 0x0000, 0x0000, 0x00a0, 0x0000, 0x0015, 0x0a00, 0x0002,
0x0000, 0x0000, 0x0000, 0x0010, 0x0000, 0x02c0, 0x0009, 0x0002,
0x0000, 0x0808, 0x0444, 0x0002, 0x0120, 0x0002, 0x0002, 0x0002,
0x0000, 0x0000, 0x0000, 0x0010, 0x0000, 0x0802, 0x0084, 0x0221,
0x0000, 0x0600, 0x0003, 0x0904, 0x0120, 0x0080, 0x0410, 0x0048,
0x0000, 0x0010, 0x0010, 0x0010, 0x0120, 0x040c, 0x0840, 0x0010,
0x0120, 0x0041, 0x0288, 0x0010, 0x0120, 0x0120, 0x0120, 0x0002,
0x0000, 0x0000, 0x0000, 0x0500, 0x0000, 0x0500, 0x0500, 0x0500,
0x0000, 0x0808, 0x0003, 0x0250, 0x0040, 0x0080, 0x002c, 0x0500,
0x0000, 0x0808, 0x0220, 0x0085, 0x0006, 0x0030, 0x0840, 0x0500,
0x0808, 0x0808, 0x0100, 0x0808, 0x0601, 0x0808, 0x0090, 0x0002,
0x0000, 0x0064, 0x0003, 0x0008, 0x0218, 0x0080, 0x0840, 0x0500,
0x0003, 0x0080, 0x0003, 0x0003, 0x0080, 0x0080, 0x0003, 0x0080,
0x0480, 0x0302, 0x0840, 0x0010, 0x0840, 0x0001, 0x0840, 0x0840,
0x0014, 0x0808, 0x0003, 0x0420, 0x0120, 0x0080, 0x0840, 0x0204,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0083,
0x0000, 0x0000, 0x0000, 0x0004, 0x0000, 0x0400, 0x0a00, 0x0130,
0x0000, 0x0000, 0x0000, 0x0010, 0x0000, 0x0400, 0x0140, 0x0208,
0x0000, 0x0400, 0x002a, 0x08c0, 0x0400, 0x0400, 0x0005, 0x0400,
0x0000, 0x0000, 0x0000, 0x0010, 0x0000, 0x0304, 0x0020, 0x0c00,
0x0000, 0x0821, 0x0580, 0x0202, 0x0012, 0x0080, 0x0005, 0x0048,
0x0000, 0x0010, 0x0010, 0x0010, 0x0888, 0x0062, 0x0005, 0x0010,
0x0240, 0x0108, 0x0005, 0x0010, 0x0005, 0x0400, 0x0005, 0x0005,
0x0000, 0x0000, 0x0000, 0x0004, 0x0000, 0x0850, 0x0020, 0x0208,
0x0000, 0x0004, 0x0004, 0x0004, 0x0109, 0x0080, 0x0442, 0x0004,
0x0000, 0x01a0, 0x0c01, 0x0208, 0x0006, 0x0208, 0x0208, 0x0208,
0x0240, 0x0013, 0x0100, 0x0004, 0x0820, 0x0400, 0x0090, 0x0208,
0x0000, 0x040a, 0x0020, 0x0141, 0x0020, 0x0080, 0x0020, 0x0020,
0x0240, 0x0080, 0x0818, 0x0004, 0x0080, 0x0080, 0x0020, 0x0080,
0x0240, 0x0804, 0x0082, 0x0010, 0x0510, 0x0001, 0x0020, 0x0208,
0x0240, 0x0240, 0x0240, 0x0420, 0x0240, 0x0080, 0x0005, 0x0902,
0x0000, 0x0000, 0x0000, 0x0010, 0x0000, 0x0028, 0x0a00, 0x0044,
0x0000, 0x0142, 0x0a00, 0x0409, 0x0a00, 0x0080, 0x0a00, 0x0a00,
0x0000, 0x0010, 0x0010, 0x0010, 0x0006, 0x0901, 0x04a0, 0x0010,
0x0081, 0x0224, 0x0100, 0x0010, 0x0058, 0x0400, 0x0a00, 0x0002,
0x0000, 0x0010, 0x0010, 0x0010, 0x0441, 0x0080, 0x010a, 0x0010,
0x000c, 0x0080, 0x0060, 0x0010, 0x0080, 0x0080, 0x0a00, 0x0080,
0x0010, 0x0010, 0x0010, 0x0010, 0x0200, 0x0010, 0x0010, 0x0010,
0x0c02, 0x0010, 0x0010, 0x0010, 0x0120, 0x0080, 0x0005, 0x0010,
0x0000, 0x0201, 0x00c8, 0x0822, 0x0006, 0x0080, 0x0011, 0x0500,
0x0430, 0x0080, 0x0100, 0x0004, 0x0080, 0x0080, 0x0a00, 0x0080,
0x0006, 0x0440, 0x0100, 0x0010, 0x0006, 0x0006, 0x0006, 0x0208,
0x0100, 0x0808, 0x0100, 0x0100, 0x0006, 0x0080, 0x0100, 0x0061,
0x0900, 0x0080, 0x0604, 0x0010, 0x0080, 0x0080, 0x0020, 0x0080,
0x0080, 0x0080, 0x0003, 0x0080, 0x0080, 0x0080, 0x0080, 0x0080,
0x0029, 0x0010, 0x0010, 0x0010, 0x0006, 0x0080, 0x0840, 0x0010,
0x0240, 0x0080, 0x0100, 0x0010, 0x0080, 0x0080, 0x0408, 0x0080,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0210,
0x0000, 0x0000, 0x0000, 0x00a0, 0x0000, 0x0400, 0x0106, 0x0801,
0x0000, 0x0000, 0x0000, 0x0042, 0x0000, 0x0400, 0x0009, 0x0180,
0x0000, 0x0400, 0x0800, 0x001c, 0x0400, 0x0400, 0x0260, 0x0400,
0x0000, 0x0000, 0x0000, 0x0405, 0x0000, 0x0802, 0x0020, 0x0180,
0x0000, 0x0110, 0x0800, 0x0202, 0x0281, 0x0024, 0x0410, 0x0048,
0x0000, 0x0228, 0x0800, 0x0180, 0x0054, 0x0180, 0x0180, 0x0180,
0x0800, 0x0041, 0x0800, 0x0800, 0x000a, 0x0400, 0x0800, 0x0180,
0x0000, 0x0000, 0x0000, 0x0042, 0x0000, 0x008c, 0x0020, 0x0801,
0x0000, 0x0110, 0x0608, 0x0801, 0x0040, 0x0801, 0x0801, 0x0801,
0x0000, 0x0042, 0x0042, 0x0042, 0x0b00, 0x0030, 0x0404, 0x0042,
0x0025, 0x0280, 0x0100, 0x0042, 0x000a, 0x0400, 0x0090, 0x0801,
0x0000, 0x0110, 0x0020, 0x0008, 0x0020, 0x0640, 0x0020, 0x0020,
0x0110, 0x0110, 0x00c4, 0x0110, 0x000a, 0x0110, 0x0020, 0x0801,
0x0480, 0x0804, 0x0211, 0x0042, 0x000a, 0x0001, 0x0020, 0x0180,
0x000a, 0x0110, 0x0800, 0x0420, 0x000a, 0x000a, 0x000a, 0x0204,
0x0000, 0x0000, 0x0000, 0x00a0, 0x0000, 0x0802, 0x0009, 0x0044,
0x0000, 0x00a0, 0x00a0, 0x00a0, 0x0040, 0x0308, 0x0410, 0x00a0,
0x0000, 0x0104, 0x0009, 0x0e00, 0x0009, 0x0030, 0x0009, 0x0009,
0x0212, 0x0041, 0x0100, 0x00a0, 0x0884, 0x0400, 0x0009, 0x0002,
0x0000, 0x0802, 0x0340, 0x0008, 0x0802, 0x0802, 0x0410, 0x0802,
0x000c, 0x0041, 0x0410, 0x00a0, 0x0410, 0x0802, 0x0410, 0x0410,
0x0480, 0x0041, 0x0026, 0x0010, 0x0200, 0x0802, 0x0009, 0x0180,
0x0041, 0x0041, 0x0800, 0x0041, 0x0120, 0x0041, 0x0410, 0x0204,
0x0000, 0x0201, 0x0814, 0x0008, 0x0040, 0x0030, 0x0282, 0x0500,
0x0040, 0x0406, 0x0100, 0x00a0, 0x0040, 0x0040, 0x0040, 0x0801,
0x0480, 0x0030, 0x0100, 0x0042, 0x0030, 0x0030, 0x0009, 0x0030,
0x0100, 0x0808, 0x0100, 0x0100, 0x0040, 0x0030, 0x0100, 0x0204,
0x0480, 0x0008, 0x0008, 0x0008, 0x0105, 0x0802, 0x0020, 0x0008,
0x0a20, 0x0110, 0x0003, 0x0008, 0x0040, 0x0080, 0x0410, 0x0204,
0x0480, 0x0480, 0x0480, 0x0008, 0x0480, 0x0030, 0x0840, 0x0204,
0x0480, 0x0041, 0x0100, 0x0204, 0x000a, 0x0204, 0x0204, 0x0204,
0x0000, 0x0000, 0x0000, 0x0908, 0x0000, 0x0400, 0x0020, 0x0044,
0x0000, 0x0400, 0x0051, 0x0202, 0x0400, 0x0400, 0x0088, 0x0400,
0x0000, 0x0400, 0x0284, 0x0021, 0x0400, 0x0400, 0x0812, 0x0400,
0x0400, 0x0400, 0x0100, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
0x0000, 0x00c0, 0x0020, 0x0202, 0x0020, 0x0019, 0x0020, 0x0020,
0x000c, 0x0202, 0x0202, 0x0202, 0x0940, 0x0400, 0x0020, 0x0202,
0x0103, 0x0804, 0x0448, 0x0010, 0x0200, 0x0400, 0x0020, 0x0180,
0x00b0, 0x0400, 0x0800, 0x0202, 0x0400, 0x0400, 0x0005, 0x0400,
0x0000, 0x0201, 0x0020, 0x0490, 0x0020, 0x0102, 0x0020, 0x0020,
0x0882, 0x0068, 0x0100, 0x0004, 0x0214, 0x0400, 0x0020, 0x0801,
0x0018, 0x0804, 0x0100, 0x0042, 0x00c1, 0x0400, 0x0020, 0x0208,
0x0100, 0x0400, 0x0100, 0x0100, 0x0400, 0x0400, 0x0100, 0x0400,
0x0020, 0x0804, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
0x0401, 0x0110, 0x0020, 0x0202, 0x0020, 0x0080, 0x0020, 0x0020,
0x0804, 0x0804, 0x0020, 0x0804, 0x0020, 0x0804, 0x0020, 0x0020,
0x0240, 0x0804, 0x0100, 0x0089, 0x000a, 0x0400, 0x0020, 0x0050,
0x0000, 0x0201, 0x0402, 0x0044, 0x0190, 0x0044, 0x0044, 0x0044,
0x000c, 0x0810, 0x0100, 0x00a0, 0x0023, 0x0400, 0x0a00, 0x0044,
0x0860, 0x008a, 0x0100, 0x0010, 0x0200, 0x0400, 0x0009, 0x0044,
0x0100, 0x0400, 0x0100, 0x0100, 0x0400, 0x0400, 0x0100, 0x0400,
0x000c, 0x0520, 0x0881, 0x0010, 0x0200, 0x0802, 0x0020, 0x0044,
0x000c, 0x000c, 0x000c, 0x0202, 0x000c, 0x0080, 0x0410, 0x0101,
0x0200, 0x0010, 0x0010, 0x0010, 0x0200, 0x0200, 0x0200, 0x0010,
0x000c, 0x0041, 0x0100, 0x0010, 0x0200, 0x0400, 0x00c2, 0x0828,
0x0201, 0x0201, 0x0100, 0x0201, 0x0c08, 0x0201, 0x0020, 0x0044,
0x0100, 0x0201, 0x0100, 0x0100, 0x0040, 0x0080, 0x0100, 0x001a,
0x0100, 0x0201, 0x0100, 0x0100, 0x0006, 0x0030, 0x0100, 0x0880,
0x0100, 0x0100, 0x0100, 0x0100, 0x0100, 0x0400, 0x0100, 0x0100,
0x0052, 0x0201, 0x0020, 0x0008, 0x0020, 0x0080, 0x0020, 0x0020,
0x000c, 0x0080, 0x0100, 0x0c40, 0x0080, 0x0080, 0x0020, 0x0080,
0x0480, 0x0804, 0x0100, 0x0010, 0x0200, 0x0148, 0x0020, 0x0403,
0x0100, 0x0022, 0x0100, 0x0100, 0x0811, 0x0080, 0x0100, 0x0204,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0210,
0x0000, 0x0000, 0x0000, 0x0004, 0x0000, 0x0980, 0x0421, 0x0002,
0x0000, 0x0000, 0x0000, 0x0488, 0x0000, 0x0001, 0x0140, 0x0002,
0x0000, 0x0070, 0x0800, 0x0002, 0x020c, 0x0002, 0x0002, 0x0002,
0x0000, 0x0000, 0x0000, 0x0122, 0x0000, 0x0001, 0x0084, 0x0c00,
0x0000, 0x0600, 0x0800, 0x0091, 0x0012, 0x0024, 0x0300, 0x0048,
0x0000, 0x0001, 0x0800, 0x0244, 0x0001, 0x0001, 0x0038, 0x0001,
0x0800, 0x0108, 0x0800, 0x0800, 0x04c0, 0x0001, 0x0800, 0x0002,
0x0000, 0x0000, 0x0000, 0x0004, 0x0000, 0x0001, 0x080a, 0x00e0,
0x0000, 0x0004, 0x0004, 0x0004, 0x0040, 0x0418, 0x0300, 0x0004,
0x0000, 0x0001, 0x0220, 0x0910, 0x0001, 0x0001, 0x0404, 0x0001,
0x0502, 0x0280, 0x0049, 0x0004, 0x0820, 0x0001, 0x0090, 0x0002,
0x0000, 0x0001, 0x0450, 0x0008, 0x0001, 0x0001, 0x0300, 0x0001,
0x00a8, 0x0842, 0x0300, 0x0004, 0x0300, 0x0001, 0x0300, 0x0300,
0x0001, 0x0001, 0x0082, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
0x0014, 0x0001, 0x0800, 0x0420, 0x0001, 0x0001, 0x0300, 0x0001,
0x0000, 0x0000, 0x0000, 0x0841, 0x0000, 0x0028, 0x0084, 0x0002,
0x0000, 0x0600, 0x0118, 0x0002, 0x0040, 0x0002, 0x0002, 0x0002,
0x0000, 0x0104, 0x0220, 0x0002, 0x0c10, 0x0002, 0x0002, 0x0002,
0x0081, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
0x0000, 0x0600, 0x0084, 0x0008, 0x0084, 0x0150, 0x0084, 0x0084,
0x0600, 0x0600, 0x0060, 0x0600, 0x0809, 0x0600, 0x0084, 0x0002,
0x004a, 0x08a0, 0x0501, 0x0010, 0x0200, 0x0001, 0x0084, 0x0002,
0x0014, 0x0600, 0x0800, 0x0002, 0x0120, 0x0002, 0x0002, 0x0002,
0x0000, 0x0092, 0x0220, 0x0008, 0x0040, 0x0a04, 0x0011, 0x0500,
0x0040, 0x0121, 0x0c80, 0x0004, 0x0040, 0x0040, 0x0040, 0x0002,
0x0220, 0x0440, 0x0220, 0x0220, 0x0188, 0x0001, 0x0220, 0x0002,
0x0014, 0x0808, 0x0220, 0x0002, 0x0040, 0x0002, 0x0002, 0x0002,
0x0900, 0x0008, 0x0008, 0x0008, 0x0422, 0x0001, 0x0084, 0x0008,
0x0014, 0x0600, 0x0003, 0x0008, 0x0040, 0x0080, 0x0300, 0x0830,
0x0014, 0x0001, 0x0220, 0x0008, 0x0001, 0x0001, 0x0840, 0x0001,
0x0014, 0x0014, 0x0014, 0x01c0, 0x0014, 0x0001, 0x0408, 0x0002,
0x0000, 0x0000, 0x0000, 0x0004, 0x0000, 0x0028, 0x0140, 0x0c00,
0x0000, 0x0004, 0x0004, 0x0004, 0x0012, 0x0241, 0x0088, 0x0004,
0x0000, 0x0a02, 0x0140, 0x0021, 0x0140, 0x0094, 0x0140, 0x0140,
0x0081, 0x0108, 0x0610, 0x0004, 0x0820, 0x0400, 0x0140, 0x0002,
0x0000, 0x00c0, 0x0209, 0x0c00, 0x0012, 0x0c00, 0x0c00, 0x0c00,
0x0012, 0x0108, 0x0060, 0x0004, 0x0012, 0x0012, 0x0012, 0x0c00,
0x0424, 0x0108, 0x0082, 0x0010, 0x0200, 0x0001, 0x0140, 0x0c00,
0x0108, 0x0108, 0x0800, 0x0108, 0x0012, 0x0108, 0x0005, 0x02a0,
0x0000, 0x0004, 0x0004, 0x0004, 0x0680, 0x0102, 0x0011, 0x0004,
0x0004, 0x0004, 0x0004, 0x0004, 0x0820, 0x0004, 0x0004, 0x0004,
0x0018, 0x0440, 0x0082, 0x0004, 0x0820, 0x0001, 0x0140, 0x0208,
0x0820, 0x0004, 0x0004, 0x0004, 0x0820, 0x0820, 0x0820, 0x0004,
0x0900, 0x0230, 0x0082, 0x0004, 0x004c, 0x0001, 0x0020, 0x0c00,
0x0401, 0x0004, 0x0004, 0x0004, 0x0012, 0x0080, 0x0300, 0x0004,
0x0082, 0x0001, 0x0082, 0x0082, 0x0001, 0x0001, 0x0082, 0x0001,
0x0240, 0x0108, 0x0082, 0x0004, 0x0820, 0x0001, 0x0408, 0x0050,
0x0000, 0x0028, 0x0402, 0x0380, 0x0028, 0x0028, 0x0011, 0x0028,
0x0081, 0x0810, 0x0060, 0x0004, 0x0504, 0x0028, 0x0a00, 0x0002,
0x0081, 0x0440, 0x080c, 0x0010, 0x0200, 0x0028, 0x0140, 0x0002,
0x0081, 0x0081, 0x0081, 0x0002, 0x0081, 0x0002, 0x0002, 0x0002,
0x0900, 0x0007, 0x0060, 0x0010, 0x0200, 0x0028, 0x0084, 0x0c00,
0x0060, 0x0600, 0x0060, 0x0060, 0x0012, 0x0080, 0x0060, 0x0101,
0x0200, 0x0010, 0x0010, 0x0010, 0x0200, 0x0200, 0x0200, 0x0010,
0x0081, 0x0108, 0x0060, 0x0010, 0x0200, 0x0844, 0x0408, 0x0002,
0x0900, 0x0440, 0x0011, 0x0004, 0x0011, 0x0028, 0x0011, 0x0011,
0x020a, 0x0004, 0x0004, 0x0004, 0x0040, 0x0080, 0x0011, 0x0004,
0x0440, 0x0440, 0x0220, 0x0440, 0x0006, 0x0440, 0x0011, 0x0880,
0x0081, 0x0440, 0x0100, 0x0004, 0x0820, 0x0310, 0x0408, 0x0002,
0x0900, 0x0900, 0x0900, 0x0008, 0x0900, 0x0080, 0x0011, 0x0242,
0x0900, 0x0080, 0x0060, 0x0004, 0x0080, 0x0080, 0x0408, 0x0080,
0x0900, 0x0440, 0x0082, 0x0010, 0x0200, 0x0001, 0x0408, 0x0124,
0x0014, 0x0022, 0x0408, 0x0a01, 0x0408, 0x0080, 0x0408, 0x0408,
0x0000, 0x0000, 0x0000, 0x0210, 0x0000, 0x0210, 0x0210, 0x0210,
0x0000, 0x000b, 0x0800, 0x0540, 0x0040, 0x0024, 0x0088, 0x0210,
0x0000, 0x0104, 0x0800, 0x0021, 0x00a2, 0x0848, 0x0404, 0x0210,
0x0800, 0x0280, 0x0800, 0x0800, 0x0111, 0x0400, 0x0800, 0x0002,
0x0000, 0x00c0, 0x0800, 0x0008, 0x0508, 0x0024, 0x0043, 0x0210,
0x0800, 0x0024, 0x0800, 0x0800, 0x0024, 0x0024, 0x0800, 0x0024,
0x0800, 0x0412, 0x0800, 0x0800, 0x0200, 0x0001, 0x0800, 0x0180,
0x0800, 0x0800, 0x0800, 0x0800, 0x0800, 0x0024, 0x0800, 0x0800,
0x0000, 0x0c20, 0x0181, 0x0008, 0x0040, 0x0102, 0x0404, 0x0210,
0x0040, 0x0280, 0x0032, 0x0004, 0x0040, 0x0040, 0x0040, 0x0801,
0x0018, 0x0280, 0x0404, 0x0042, 0x0404, 0x0001, 0x0404, 0x0404,
0x0280, 0x0280, 0x0800, 0x0280, 0x0040, 0x0280, 0x0404, 0x0128,
0x0206, 0x0008, 0x0008, 0x0008, 0x0890, 0x0001, 0x0020, 0x0008,
0x0401, 0x0110, 0x0800, 0x0008, 0x0040, 0x0024, 0x0300, 0x0482,
0x0160, 0x0001, 0x0800, 0x0008, 0x0001, 0x0001, 0x0404, 0x0001,
0x0800, 0x0280, 0x0800, 0x0800, 0x000a, 0x0001, 0x0800, 0x0050,
0x0000, 0x0104, 0x0402, 0x0008, 0x0040, 0x0481, 0x0920, 0x0210,
0x0040, 0x0810, 0x0205, 0x00a0, 0x0040, 0x0040, 0x0040, 0x0002,
0x0104, 0x0104, 0x00d0, 0x0104, 0x0200, 0x0104, 0x0009, 0x0002,
0x0428, 0x0104, 0x0800, 0x0002, 0x0040, 0x0002, 0x0002, 0x0002,
0x0031, 0x0008, 0x0008, 0x0008, 0x0200, 0x0802, 0x0084, 0x0008,
0x0182, 0x0600, 0x0800, 0x0008, 0x0040, 0x0024, 0x0410, 0x0101,
0x0200, 0x0104, 0x0800, 0x0008, 0x0200, 0x0200, 0x0200, 0x0460,
0x0800, 0x0041, 0x0800, 0x0800, 0x0200, 0x0098, 0x0800, 0x0002,
0x0040, 0x0008, 0x0008, 0x0008, 0x0040, 0x0040, 0x0040, 0x0008,
0x0040, 0x0040, 0x0040, 0x0008, 0x0040, 0x0040, 0x0040, 0x0040,
0x0803, 0x0104, 0x0220, 0x0008, 0x0040, 0x0030, 0x0404, 0x0880,
0x0040, 0x0280, 0x0100, 0x0411, 0x0040, 0x0040, 0x0040, 0x0002,
0x0008, 0x0008, 0x0008, 0x0008, 0x0040, 0x0008, 0x0008, 0x0008,
0x0040, 0x0008, 0x0008, 0x0008, 0x0040, 0x0040, 0x0040, 0x0008,
0x0480, 0x0008, 0x0008, 0x0008, 0x0200, 0x0001, 0x0112, 0x0008,
0x0014, 0x0022, 0x0800, 0x0008, 0x0040, 0x0d00, 0x00a1, 0x0204,
0x0000, 0x00c0, 0x0402, 0x0021, 0x0805, 0x0102, 0x0088, 0x0210,
0x0320, 0x0810, 0x0088, 0x0004, 0x0088, 0x0400, 0x0088, 0x0088,
0x0018, 0x0021, 0x0021, 0x0021, 0x0200, 0x0400, 0x0140, 0x0021,
0x0046, 0x0400, 0x0800, 0x0021, 0x0400, 0x0400, 0x0088, 0x0400,
0x00c0, 0x00c0, 0x0114, 0x00c0, 0x0200, 0x00c0, 0x0020, 0x0c00,
0x0401, 0x00c0, 0x0800, 0x0202, 0x0012, 0x0024, 0x0088, 0x0101,
0x0200, 0x00c0, 0x0800, 0x0021, 0x0200, 0x0200, 0x0200, 0x000e,
0x0800, 0x0108, 0x0800, 0x0800, 0x0200, 0x0400, 0x0800, 0x0050,
0x0018, 0x0102, 0x0a40, 0x0004, 0x0102, 0x0102, 0x0020, 0x0102,
0x0401, 0x0004, 0x0004, 0x0004, 0x0040, 0x0102, 0x0088, 0x0004,
0x0018, 0x0018, 0x0018, 0x0021, 0x0018, 0x0102, 0x0404, 0x0880,
0x0018, 0x0280, 0x0100, 0x0004, 0x0820, 0x0400, 0x0203, 0x0050,
0x0401, 0x00c0, 0x0020, 0x0008, 0x0020, 0x0102, 0x0020, 0x0020,
0x0401, 0x0401, 0x0401, 0x0004, 0x0401, 0x0a08, 0x0020, 0x0050,
0x0018, 0x0804, 0x0082, 0x0700, 0x0200, 0x0001, 0x0020, 0x0050,
0x0401, 0x0022, 0x0800, 0x0050, 0x0184, 0x0050, 0x0050, 0x0050,
0x0402, 0x0810, 0x0402, 0x0402, 0x0200, 0x0028, 0x0402, 0x0044,
0x0810, 0x0810, 0x0402, 0x0810, 0x0040, 0x0810, 0x0088, 0x0101,
0x0200, 0x0104, 0x0402, 0x0021, 0x0200, 0x0200, 0x0200, 0x0880,
0x0081, 0x0810, 0x0100, 0x0248, 0x0200, 0x0400, 0x0034, 0x0002,
0x0200, 0x00c0, 0x0402, 0x0008, 0x0200, 0x0200, 0x0200, 0x0101,
0x000c, 0x0810, 0x0060, 0x0101, 0x0200, 0x0101, 0x0101, 0x0101,
0x0200, 0x0200, 0x0200, 0x0010, 0x0200, 0x0200, 0x0200, 0x0200,
0x0200, 0x0022, 0x0800, 0x0484, 0x0200, 0x0200, 0x0200, 0x0101,
0x00a4, 0x0201, 0x0402, 0x0008, 0x0040, 0x0102, 0x0011, 0x0880,
0x0040, 0x0810, 0x0100, 0x0004, 0x0040, 0x0040, 0x0040, 0x0620,
0x0018, 0x0440, 0x0100, 0x0880, 0x0200, 0x0880, 0x0880, 0x0880,
0x0100, 0x0022, 0x0100, 0x0100, 0x0040, 0x000d, 0x0100, 0x0880,
0x0900, 0x0008, 0x0008, 0x0008, 0x0200, 0x0414, 0x0020, 0x0008,
0x0401, 0x0022, 0x0290, 0x0008, 0x0040, 0x0080, 0x0806, 0x0101,
0x0200, 0x0022, 0x0045, 0x0008, 0x0200, 0x0200, 0x0200, 0x0880,
0x0022, 0x0022, 0x0100, 0x0022, 0x0200, 0x0022, 0x0408, 0x0050,
}
)
// Golay(23, 12) forward error correction.
func Golay_23_12_Correct(block *uint32) {
var (
mask = uint32(0x400000)
curr = *block
syndrome uint32
)
for i := 0; i < 12; i++ {
if (curr & mask) != 0 {
syndrome ^= golayGenerator[i]
}
mask >>= 1
}
syndrome ^= (curr & 0x07ff)
*block = ((curr >> 11) ^ golayMatrix[syndrome])
}
// Golay(23, 12) codeword.
func Golay_23_12_Encode(codeword uint32) uint32 {
var c = codeword
for i := 0; i < 12; i++ {
if codeword&1 == 1 { // Test data bit
codeword ^= 0xc75 // XOR polynomial
codeword >>= 1 // Shift intermediate result
}
}
return ((codeword << 12) | c) // Assemble encoded codeword
}

@ -0,0 +1,43 @@
package fec
var (
hamming15_11_3_gen = [11]uint32{
0x4009, 0x200d, 0x100f, 0x080e, 0x0407, 0x020a, 0x0105, 0x008b, 0x004c, 0x0026, 0x0013,
}
hamming15_11_3_table = [16]uint32{
0x0000, 0x0001, 0x0002, 0x0013, 0x0004, 0x0105, 0x0026, 0x0407,
0x0008, 0x4009, 0x020a, 0x008b, 0x004c, 0x200d, 0x080e, 0x100f,
}
)
// Correct a block of data using Hamming(15, 11, 3).
func Hamming15_11_3_Correct(block *uint32) {
var (
ecc, syndrome uint32
codeword = *block
)
for i := 0; i < 11; i++ {
if (codeword & hamming15_11_3_gen[i]) > 0x0f {
ecc ^= hamming15_11_3_gen[i]
}
}
syndrome = ecc ^ codeword
if syndrome != 0 {
codeword ^= hamming15_11_3_table[syndrome&0x0f]
}
*block = (codeword >> 4)
}
// Hamming(15, 11, 3) encode a block of data.
func Hamming15_11_3_Encode(input uint32) uint32 {
var codeword uint32
for i := uint8(0); i < 11; i++ {
if input&(1<<(10-i)) > 0 {
codeword ^= hamming15_11_3_gen[i]
}
}
return codeword
}

@ -0,0 +1,302 @@
package fec
import (
"errors"
"fmt"
)
const (
RS_12_9_DATASIZE = 9
RS_12_9_CHECKSUMSIZE = 3
// Maximum degree of various polynomials
RS_12_9_POLY_MAXDEG = RS_12_9_CHECKSUMSIZE * 2
)
type RS_12_9_Poly [RS_12_9_POLY_MAXDEG]uint8
var (
// DMR AI. spec. page 138.
rs_12_9_galois_exp_table = [256]uint8{
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1d, 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26,
0x4c, 0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9, 0x8f, 0x03, 0x06, 0x0c, 0x18, 0x30, 0x60, 0xc0,
0x9d, 0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35, 0x6a, 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23,
0x46, 0x8c, 0x05, 0x0a, 0x14, 0x28, 0x50, 0xa0, 0x5d, 0xba, 0x69, 0xd2, 0xb9, 0x6f, 0xde, 0xa1,
0x5f, 0xbe, 0x61, 0xc2, 0x99, 0x2f, 0x5e, 0xbc, 0x65, 0xca, 0x89, 0x0f, 0x1e, 0x3c, 0x78, 0xf0,
0xfd, 0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f, 0xfe, 0xe1, 0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2,
0xd9, 0xaf, 0x43, 0x86, 0x11, 0x22, 0x44, 0x88, 0x0d, 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce,
0x81, 0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93, 0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc,
0x85, 0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54,
0xa8, 0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73,
0xe6, 0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff,
0xe3, 0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41,
0x82, 0x19, 0x32, 0x64, 0xc8, 0x8d, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6,
0x51, 0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x09,
0x12, 0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0x0b, 0x16,
0x2c, 0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e, 0x01,
}
// DMR AI. spec. page 138.
rs_12_9_galois_log_table = [256]uint8{
0, 0, 1, 25, 2, 50, 26, 198, 3, 223, 51, 238, 27, 104, 199, 75,
4, 100, 224, 14, 52, 141, 239, 129, 28, 193, 105, 248, 200, 8, 76, 113,
5, 138, 101, 47, 225, 36, 15, 33, 53, 147, 142, 218, 240, 18, 130, 69,
29, 181, 194, 125, 106, 39, 249, 185, 201, 154, 9, 120, 77, 228, 114, 166,
6, 191, 139, 98, 102, 221, 48, 253, 226, 152, 37, 179, 16, 145, 34, 136,
54, 208, 148, 206, 143, 150, 219, 189, 241, 210, 19, 92, 131, 56, 70, 64,
30, 66, 182, 163, 195, 72, 126, 110, 107, 58, 40, 84, 250, 133, 186, 61,
202, 94, 155, 159, 10, 21, 121, 43, 78, 212, 229, 172, 115, 243, 167, 87,
7, 112, 192, 247, 140, 128, 99, 13, 103, 74, 222, 237, 49, 197, 254, 24,
227, 165, 153, 119, 38, 184, 180, 124, 17, 68, 146, 217, 35, 32, 137, 46,
55, 63, 209, 91, 149, 188, 207, 205, 144, 135, 151, 178, 220, 252, 190, 97,
242, 86, 211, 171, 20, 42, 93, 158, 132, 60, 57, 83, 71, 109, 65, 162,
31, 45, 67, 216, 183, 123, 164, 118, 196, 23, 73, 236, 127, 12, 111, 246,
108, 161, 59, 82, 41, 157, 85, 170, 251, 96, 134, 177, 187, 204, 62, 90,
203, 89, 95, 176, 156, 169, 160, 81, 11, 245, 22, 235, 122, 117, 44, 215,
79, 174, 213, 233, 230, 231, 173, 232, 116, 214, 244, 234, 168, 80,
}
)
func RS_12_9_Galois_Inv(elt uint8) uint8 {
return rs_12_9_galois_exp_table[255-rs_12_9_galois_log_table[elt]]
}
func RS_12_9_Galois_Mul(a, b uint8) uint8 {
if a == 0 || b == 0 {
return 0
}
return rs_12_9_galois_exp_table[(rs_12_9_galois_log_table[a]+rs_12_9_galois_log_table[b])%255]
}
// Multiply by z (shift right by 1).
func RS_12_9_MulPolyZ(poly *RS_12_9_Poly) {
for i := RS_12_9_POLY_MAXDEG - 1; i > 0; i-- {
poly[i] = poly[i-1]
}
poly[0] = 0
}
func RS_12_9_MulPolys(p1, p2 *RS_12_9_Poly, dst []uint8) {
var (
i, j uint8
tmp = make([]uint8, RS_12_9_POLY_MAXDEG*2)
)
for i = 0; i < RS_12_9_POLY_MAXDEG*2; i++ {
dst[i] = 0
}
for i = 0; i < RS_12_9_POLY_MAXDEG; i++ {
for j := RS_12_9_POLY_MAXDEG; j < (RS_12_9_POLY_MAXDEG * 2); j++ {
tmp[j] = 0
}
// Scale tmp by p1[i]
for j = 0; j < RS_12_9_POLY_MAXDEG; j++ {
tmp[j] = RS_12_9_Galois_Mul(p2[j], p1[i])
}
// Shift (multiply) tmp right by i
for j = (RS_12_9_POLY_MAXDEG * 2) - 1; j >= i && j < (RS_12_9_POLY_MAXDEG*2)-1; j-- {
tmp[j] = tmp[j-i]
}
for j = 0; j < i; j++ {
tmp[j] = 0
}
// Add into partial product
for j = 0; j < (RS_12_9_POLY_MAXDEG * 2); j++ {
dst[j] ^= tmp[j]
}
}
}
// Computes the combined erasure/error evaluator polynomial (error_locator_poly*syndrome mod z^4)
func RS_12_9_CalcErrorEvaluatorPoly(locator, syndrome, evaluator *RS_12_9_Poly) {
var (
i uint8
product = make([]uint8, RS_12_9_POLY_MAXDEG*2)
)
RS_12_9_MulPolys(locator, syndrome, product)
for i = 0; i < RS_12_9_CHECKSUMSIZE; i++ {
evaluator[i] = product[i]
}
for ; i < RS_12_9_POLY_MAXDEG; i++ {
evaluator[i] = 0
}
}
func RS_12_9_CalcDiscrepancy(locator, syndrome *RS_12_9_Poly, L, n uint8) uint8 {
var i, sum uint8
for i = 0; i < L; i++ {
sum ^= RS_12_9_Galois_Mul(locator[i], syndrome[n-i])
}
return sum
}
// This finds the coefficients of the error locator polynomial, and then calculates
// the error evaluator polynomial using the Berlekamp-Massey algorithm.
// From Cain, Clark, "Error-Correction Coding For Digital Communications", pp. 216.
func RS_12_9_Calc(syndrome, locator, evaluator *RS_12_9_Poly) {
var (
n, L, L2 uint8
k int8
d, i uint8
psi2 = make([]uint8, RS_12_9_POLY_MAXDEG)
D = RS_12_9_Poly{0, 1, 0}
)
k = -1
for i = 0; i < RS_12_9_POLY_MAXDEG; i++ {
locator[i] = 0
}
locator[0] = 1
for n = 0; n < RS_12_9_CHECKSUMSIZE; n++ {
d = RS_12_9_CalcDiscrepancy(locator, syndrome, L, n)
if d != 0 {
// psi2 = locator - d*D
for i = 0; i < RS_12_9_POLY_MAXDEG; i++ {
psi2[i] = locator[i] ^ RS_12_9_Galois_Mul(d, D[i])
}
if int8(L) < int8(n)-k {
L2 = uint8(int8(n) - k)
k = int8(int8(n) - int8(L))
for i = 0; i < RS_12_9_POLY_MAXDEG; i++ {
D[i] = RS_12_9_Galois_Mul(locator[i], RS_12_9_Galois_Inv(d))
}
L = L2
}
// locator = psi2
for i = 0; i < RS_12_9_POLY_MAXDEG; i++ {
locator[i] = psi2[i]
}
}
RS_12_9_MulPolyZ(&D)
}
RS_12_9_CalcErrorEvaluatorPoly(locator, syndrome, evaluator)
}
// The error-locator polynomial's roots are found by looking for the values of a^n where
// evaluating the polynomial yields zero (evaluating rs_12_9_error_locator_poly at
// successive values of alpha (Chien's search)).
func RS_12_9_FindRoots(locator *RS_12_9_Poly) []uint8 {
var k, r uint16
roots := make([]uint8, 0)
for r = 1; r < 256; r++ {
var sum uint8
// Evaluate locator at r
for k = 0; k < RS_12_9_CHECKSUMSIZE+1; k++ {
sum ^= RS_12_9_Galois_Mul(rs_12_9_galois_exp_table[(k*r)%255], locator[k])
}
if sum == 0 {
roots = append(roots, uint8(255-r))
}
}
return roots
}
func RS_12_9_CalcSyndrome(data []byte, syndrome *RS_12_9_Poly) error {
if len(data) != RS_12_9_DATASIZE+RS_12_9_CHECKSUMSIZE {
return fmt.Errorf("fec/rs_12_9: unexpected size %d, expected %d bytes",
len(data), RS_12_9_DATASIZE+RS_12_9_CHECKSUMSIZE)
}
var i, j uint8
for i = 0; i < 3; i++ {
syndrome[i] = 0
}
for j = 0; j < 3; j++ {
for i = 0; i < uint8(len(data)); i++ {
syndrome[j] = data[i] ^ RS_12_9_Galois_Mul(rs_12_9_galois_exp_table[j+1], syndrome[j])
}
}
return nil
}
func RS_12_9_CheckSyndrome(syndrome *RS_12_9_Poly) bool {
for _, v := range syndrome {
if v != 0 {
return true
}
}
return false
}
func RS_12_9_Correct(data []byte, syndrome *RS_12_9_Poly) (int, error) {
if len(data) != RS_12_9_DATASIZE+RS_12_9_CHECKSUMSIZE {
return -1, fmt.Errorf("fec/rs_12_9: unexpected size %d, expected %d bytes",
len(data), RS_12_9_DATASIZE+RS_12_9_CHECKSUMSIZE)
}
var (
i, j uint8
errorsFound int
locator = RS_12_9_Poly{}
evaluator = RS_12_9_Poly{}
)
RS_12_9_Calc(syndrome, &locator, &evaluator)
roots := RS_12_9_FindRoots(&locator)
errorsFound = len(roots)
if errorsFound == 0 {
return 0, nil
}
// Error correction is done using the error-evaluator equation on pp 207.
if errorsFound > 0 && errorsFound < RS_12_9_CHECKSUMSIZE {
// First check for illegal error locations.
for r := 0; r < errorsFound; r++ {
if roots[r] >= RS_12_9_DATASIZE+RS_12_9_CHECKSUMSIZE {
return errorsFound, errors.New("fec/rs_12_9: errors can't be corrected")
}
}
// Evaluates rs_12_9_error_evaluator_poly/rs_12_9_error_locator_poly' at the roots
// alpha^(-i) for error locs i.
for r := 0; r < errorsFound; r++ {
i = roots[r]
var num, denom uint8
// Evaluate rs_12_9_error_evaluator_poly at alpha^(-i)
for j = 0; j < RS_12_9_POLY_MAXDEG; j++ {
num ^= RS_12_9_Galois_Mul(evaluator[j], rs_12_9_galois_exp_table[((255-i)*j)%255])
}
// Evaluate rs_12_9_error_evaluator_poly' (derivative) at alpha^(-i). All odd powers disappear.
for j = 1; j < RS_12_9_POLY_MAXDEG; j += 2 {
denom ^= RS_12_9_Galois_Mul(locator[j], rs_12_9_galois_exp_table[((255-i)*(j-1))%255])
}
data[len(data)-int(i)-1] ^= RS_12_9_Galois_Mul(num, RS_12_9_Galois_Inv(denom))
}
return errorsFound, nil
}
return 0, nil
}
// Simulates an LFSR with the generator polynomial and calculates checksum bytes for the given data.
func RS_12_9_CalcChecksum(data []byte) []uint8 {
var (
feedback uint8
genpoly = []uint8{0x40, 0x38, 0x0e, 0x01} // See DMR AI. spec. page 136 for these coefficients.
checksum = make([]uint8, 3)
)
for i := 0; i < 9; i++ {
feedback = data[i] ^ checksum[0]
checksum[0] = checksum[1] ^ RS_12_9_Galois_Mul(genpoly[2], feedback)
checksum[1] = checksum[2] ^ RS_12_9_Galois_Mul(genpoly[1], feedback)
checksum[2] = RS_12_9_Galois_Mul(genpoly[0], feedback)
}
return checksum
}
Loading…
Cancel
Save