After deciding how to transmit the preamble and the bit, in this post I will explain how I want to transmit a byte.
Since I expect to be in front of a very noisy channel, I will adopt a error-detection-and-correction code like the Hamming 7,4
The notation (7,4) means that 7 bits are required to send 4 bits of information. The extra bits allow to detect and correct a flipped bit among the 7 bits transmitted
The following general algorithm generates a single-error correcting (SEC) code for any number of bits.
- Number the bits starting from 1: bit 1, 2, 3, 4, 5, etc.
- Write the bit numbers in binary: 1, 10, 11, 100, 101, etc.
- All bit positions that are powers of two (have only one 1 bit in the binary form of their position) are parity bits: 1, 2, 4, 8, etc. (1, 10, 100, 1000)
- All other bit positions, with two or more 1 bits in the binary form of their position, are data bits.
- Each data bit is included in a unique set of 2 or more parity bits, as determined by the binary form of its bit position.
- Parity bit 1 covers all bit positions which have the least significant bit set: bit 1 (the parity bit itself), 3, 5, 7, 9, etc.
- Parity bit 2 covers all bit positions which have the second least significant bit set: bit 2 (the parity bit itself), 3, 6, 7, 10, 11, etc.
- Parity bit 4 covers all bit positions which have the third least significant bit set: bits 4–7, 12–15, 20–23, etc.
- Parity bit 8 covers all bit positions which have the fourth least significant bit set: bits 8–15, 24–31, 40–47, etc.
- In general each parity bit covers all bits where the bitwise AND of the parity position and the bit position is non-zero.
The form of the parity is irrelevant. Even parity is simpler from the perspective of theoretical mathematics, but there is no difference in practice.
This general rule can be shown visually:
Shown are only 20 encoded bits (5 parity, 15 data) but the pattern continues indefinitely. The key thing about Hamming Codes that can be seen from visual inspection is that any given bit is included in a unique set of parity bits. To check for errors, check all of the parity bits. The pattern of errors, called the error syndrome, identifies the bit in error. If all parity bits are correct, there is no error. Otherwise, the sum of the positions of the erroneous parity bits identifies the erroneous bit. For example, if the parity bits in positions 1, 2 and 8 indicate an error, then bit 1+2+8=11 is in error. If only one parity bit indicates an error, the parity bit itself is in error.
Hamming codes have a minimum distance of 3, which means that the decoder can detect and correct a single error, but it cannot distinguish a double bit error of some codeword from a single bit error of a different codeword. Thus, they can detect double-bit errors only if correction is not attempted.
To remedy this shortcoming, Hamming codes can be extended by an extra parity bit. This way, it is possible to increase the minimum distance of the Hamming code to 4, which allows the decoder to distinguish between single bit errors and two-bit errors. Thus the decoder can detect and correct a single error and at the same time detect (but not correct) a double error. If the decoder does not attempt to correct errors, it can detect up to three errors.
All this said, here is the structure of the basic data packet transmitted
Bit position | ||||||||||||||||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|
Preamble | P1 | P2 | D1 | P4 | D2 | D3 | D4 | P8 | D5 | D6 | D7 |
|
The bit time is dependent on the capabilities of the smartphone’s camera. In particular the number of frames the camera can capture in preview mode
My smartphone can take 30 snapshots per seconds with a resolution of 800 x 480. This means that it captures 30 x 800 = 24000 rows per second.
Assuming to transmit 2 byte per second, it means we need to transmit 15 bits in a second, which leds to a bit time of 33 ms. The number of rows captured per bit time is equal to 24 rows / msec * 33 msec = 792 rows
The width of each band and the number of bands in a bit time depends on the PWM frequency
Bit value | Frequency | Band width (rows) | Rows per bit time | Number of bands |
Preamble | 500 Hz | 48 | 792 | 16 |
Logic 0 | 750 Hz | 32 | 792 | 24 |
Logic 1 | 1000 Hz | 24 | 792 | 33 |