-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.html
77 lines (69 loc) · 2.84 KB
/
test.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LDPC Demo</title>
<script src="LDPC.js"></script>
</head>
<body>
<div>Open the console.</div>
<script>
// Note that the results between multiple test runs can differ as you can't set the seed for javascripts random
// generator.
const TEST_RUNS = 100;
const MESSAGE_LENGTH = 250;
const PARITY_LENGTH = 250;
const ENCODED_LENGTH = MESSAGE_LENGTH + PARITY_LENGTH;
const CHANNEL_ERROR_RATE = 0.12;
const BITS_TO_DESTROY = Math.round(CHANNEL_ERROR_RATE * ENCODED_LENGTH);
// we can give the decoder a hint about how confident we are about a value
// (e.g. 1 means pretty sure it's 1, 0.9 rather sure it's a 1.etc.)
// The higher the CONFIDENCE_HINT is chosen in this test, the easier it gets.
const CONFIDENCE_HINT = 0.1;
const testMessage = new Array(MESSAGE_LENGTH);
async function runTest() {
console.log('start tests');
let fails = 0, undetectedFails = 0;
for (let run=0; run<TEST_RUNS; ++run) {
for (let i=0; i<MESSAGE_LENGTH; ++i) {
testMessage[i] = Math.random()>0.5? 1 : 0;
}
let encoded = await LDPC.encode(testMessage, PARITY_LENGTH);
let received = Array.from(encoded);
// flip some bits
for (let i=0; i<BITS_TO_DESTROY; ++i) {
let index = Math.floor(Math.random() * (ENCODED_LENGTH+1));
// we can give the decoder a hint about how confident we are about a value
// (e.g. 0.9 means rather sure it's 1)
if (received[index]) {
received[index] = 0 + CONFIDENCE_HINT;
} else {
received[index] = 1 - CONFIDENCE_HINT;
}
}
let decoded;
try {
decoded = await LDPC.decode(received, MESSAGE_LENGTH, PARITY_LENGTH);
} catch(e) {
// some exception happened or we couldn't decode to a codeword
++fails;
console.log('test run', run, 'failed');
continue;
}
for (let i=0; i<MESSAGE_LENGTH; ++i) {
if (decoded[i] !== testMessage[i]) {
++fails;
++undetectedFails;
console.log('test run', run, 'failed but was not detected.');
break;
}
}
console.log('test run', run, 'successful');
}
console.log('Results at message length', MESSAGE_LENGTH, 'parity length', PARITY_LENGTH, 'channel error rate', CHANNEL_ERROR_RATE);
console.log('runs', TEST_RUNS, 'fails', fails, 'of which', undetectedFails, 'where undetected in decoding.');
}
runTest();
</script>
</body>
</html>