Search in sources :

Example 1 with DogArray_I16

use of org.ddogleg.struct.DogArray_I16 in project BoofCV by lessthanoptimal.

the class TestReedSolomonCodes_U16 method correct_random.

void correct_random(int numBits, int primitive, int generatorBase) {
    var alg = new ReedSolomonCodes_U16(numBits, primitive, generatorBase);
    // make sure the message size does not exceed the limits of its Field
    int maxErrors = 4;
    // words dedicated to error correction
    int nsyn = maxErrors * 2 + 2;
    int messageSize = Math.min(100, alg.math.num_values - nsyn - 2);
    var ecc = new DogArray_I16();
    int mask = alg.math.max_value;
    alg.generator(nsyn);
    for (int trial = 0; trial < 20_000; trial++) {
        DogArray_I16 message = randomMessage(mask, messageSize);
        DogArray_I16 corrupted = message.copy();
        alg.computeECC(message, ecc);
        // apply noise to the message
        int numErrors = rand.nextInt(maxErrors);
        for (int j = 0; j < numErrors; j++) {
            int selected = rand.nextInt(message.size);
            // make sure it changes even if the same number is selected twice
            corrupted.data[selected] ^= (short) ((0x112 + j) & mask);
        }
        // corrupt the ecc code
        if (numErrors < maxErrors - 1 && rand.nextInt(5) < 1) {
            ecc.data[rand.nextInt(ecc.size)] ^= (short) (0x113 & mask);
        }
        alg.correct(corrupted, ecc);
        assertEquals(message.size, corrupted.size);
        for (int j = 0; j < corrupted.size; j++) {
            assertEquals(message.get(j), corrupted.get(j));
        }
    }
}
Also used : DogArray_I16(org.ddogleg.struct.DogArray_I16)

Example 2 with DogArray_I16

use of org.ddogleg.struct.DogArray_I16 in project BoofCV by lessthanoptimal.

the class TestReedSolomonCodes_U16 method findErrors_BruteForce_TooMany.

/**
 * Test a case where there are too many errors.
 */
@Test
void findErrors_BruteForce_TooMany() {
    DogArray_I16 message = randomMessage(0xFF, 50);
    for (int generatorBase = 0; generatorBase < 2; generatorBase++) {
        var alg = new ReedSolomonCodes_U16(12, primitive12, generatorBase);
        findErrors_BruteForce(alg, message, 6, true);
        findErrors_BruteForce(alg, message, 8, true);
    }
}
Also used : DogArray_I16(org.ddogleg.struct.DogArray_I16) Test(org.junit.jupiter.api.Test)

Example 3 with DogArray_I16

use of org.ddogleg.struct.DogArray_I16 in project BoofCV by lessthanoptimal.

the class TestReedSolomonCodes_U16 method correctErrors_hand.

/**
 * Compare against a hand computed scenario
 */
@Test
void correctErrors_hand() {
    DogArray_I16 message = DogArray_I16.parseHex("[ 0x40, 0xd2, 0x75, 0x47, 0x76, 0x17, 0x32, 0x06, 0x27, 0x26, 0x96, 0xc6, 0xc6, 0x96, 0x70, 0xec ]");
    var ecc = new DogArray_I16();
    DogArray_I16 syndromes = new DogArray_I16();
    DogArray_I16 errorLocator = new DogArray_I16();
    int nsyn = 10;
    var alg = new ReedSolomonCodes_U16(8, primitive8, 0);
    alg.generator(nsyn);
    alg.computeECC(message, ecc);
    DogArray_I16 corrupted = message.copy();
    corrupted.data[0] = 0;
    corrupted.data[4] = 8;
    corrupted.data[5] = 9;
    alg.computeSyndromes(corrupted, ecc, syndromes);
    alg.findErrorLocatorPolynomialBM(syndromes, errorLocator);
    DogArray_I32 errorLocations = new DogArray_I32(3);
    errorLocations.data[0] = 0;
    errorLocations.data[1] = 4;
    errorLocations.data[2] = 5;
    errorLocations.size = 3;
    alg.correctErrors(corrupted, message.size + ecc.size, syndromes, errorLocator, errorLocations);
    assertEquals(corrupted.size, message.size);
    for (int j = 0; j < corrupted.size; j++) {
        assertEquals(corrupted.get(j), message.get(j));
    }
}
Also used : DogArray_I16(org.ddogleg.struct.DogArray_I16) DogArray_I32(org.ddogleg.struct.DogArray_I32) Test(org.junit.jupiter.api.Test)

Example 4 with DogArray_I16

use of org.ddogleg.struct.DogArray_I16 in project BoofCV by lessthanoptimal.

the class TestReedSolomonCodes_U16 method computeECC.

@Test
void computeECC() {
    DogArray_I16 message = randomMessage(0xFFF, 50);
    var ecc = new DogArray_I16();
    for (int base = 0; base < 2; base++) {
        var alg = new ReedSolomonCodes_U16(12, primitive12, base);
        alg.generator(6);
        alg.computeECC(message, ecc);
        assertEquals(6, ecc.size);
        int numNotZero = 0;
        for (int dataIdx = 0; dataIdx < ecc.size; dataIdx++) {
            if (0 != ecc.data[dataIdx])
                numNotZero++;
        }
        assertTrue(numNotZero >= 5);
    // numerical properties are tested by computeSyndromes
    }
}
Also used : DogArray_I16(org.ddogleg.struct.DogArray_I16) Test(org.junit.jupiter.api.Test)

Example 5 with DogArray_I16

use of org.ddogleg.struct.DogArray_I16 in project BoofCV by lessthanoptimal.

the class TestReedSolomonCodes_U16 method findErrors_BruteForce.

public void findErrors_BruteForce(ReedSolomonCodes_U16 alg, DogArray_I16 message, int numErrors, boolean expectedFail) {
    var ecc = new DogArray_I16();
    int nsyn = 10;
    DogArray_I16 syndromes = DogArray_I16.zeros(nsyn);
    DogArray_I16 errorLocator = new DogArray_I16();
    DogArray_I32 locations = new DogArray_I32();
    alg.generator(nsyn);
    alg.computeECC(message, ecc);
    DogArray_I16 cmessage = message.copy();
    // corrupt the message and ecc
    int N = message.size + ecc.size;
    int[] corrupted = selectN(numErrors, N);
    for (int i = 0; i < corrupted.length; i++) {
        int w = corrupted[i];
        if (w < message.size)
            cmessage.data[w] ^= (short) 0x45;
        else {
            ecc.data[w - message.size] ^= (short) 0x45;
        }
    }
    // compute needed info
    alg.computeSyndromes(cmessage, ecc, syndromes);
    alg.findErrorLocatorPolynomialBM(syndromes, errorLocator);
    if (expectedFail) {
        assertFalse(alg.findErrorLocations_BruteForce(errorLocator, N, locations));
    } else {
        // find the error locations
        assertTrue(alg.findErrorLocations_BruteForce(errorLocator, N, locations));
        // see if it found the expected number of errors and that the locations match
        assertEquals(numErrors, locations.size);
        for (int i = 0; i < locations.size; i++) {
            int num = 0;
            for (int j = 0; j < corrupted.length; j++) {
                if (corrupted[j] == locations.data[i]) {
                    num++;
                }
            }
            assertEquals(1, num);
        }
        DogArray_I16 hack = new DogArray_I16();
        alg.findErrorLocatorPolynomial(N, locations, hack);
    }
}
Also used : DogArray_I16(org.ddogleg.struct.DogArray_I16) DogArray_I32(org.ddogleg.struct.DogArray_I32)

Aggregations

DogArray_I16 (org.ddogleg.struct.DogArray_I16)17 Test (org.junit.jupiter.api.Test)13 DogArray_I32 (org.ddogleg.struct.DogArray_I32)3