A simple example.

## How does it work?

### Oversampling

The core loop of the sampling does only one thing: it continuously looks at the input pin and increments a counter. Only when the input toggles, the counter value is used as an index and the histogram is updated and the counter is reset. By doing so the histogram will contain the run length of observed zeroes or ones, expressed in the time grid of the sampler. For a 1MHz bit stream the LPC 1768 should be capable to over sample approximately four times.

### Grouping of run length

A filled histogram of run lengths, of both the zero and one symbols, will contain groups of adjacent run lengths values separated by empty spaces. If the sigma delta is connected to an analog voltage at exactly 25% of the range, the output pattern of the bit stream, expressed in the time grid of the ADC, will be close to 000100001000100001000100001... With approximately four times oversampling the LPC board may capture a data stream like: 0000, or expressed in run lengths: 10, 4, 16, 3, 12, 3, 15, 3, 11, 3, 16, 4. The histogram of zeroes will be filled with 1 at positions 10, 11, 12, 15 and 16, while the histogram of ones will be filled with 4 and 2 respectively at position 3 and 4.

### Assign values to groups

After captured the data, the histogram will be scanned for groups of adjacent run lengths. A begin and end pointer/index of each will be stored in object type "Recovered". Once the whole histogram is scanned, a list of run length groups is determined. For each groups the average value of the run length will be determined.

### Calculate Over Sample Ratio and Offset

The minimum distance between two average values will be a reasonable accurate value of the over sample factor. In our example the group of symbols consists of ADC run lengths of:

• one: occurs 4 times with length 3 and 2 times 4, thus the average is 3.333.
• three: consists of 11, 12 and 13 and thus an average of 12.0.
• four: consists of one time 15 and two times 16: average equals 15.666.

The average distance between one and three is now 8.666. Therefore the average distance between three and four, only 3.666, a reasonable approximation of the over sample ratio. When acquiring more data, the average values will approximate the oversampling ratio better. An alternative method would be two take the shortest symbol as a value of the oversample factor, as this is the unit. However, as the loop requires some pre-processing before actively it can start counting, the average run length of the symbol with run length one will always be to lower than the actual over sample ratio. This creates an offset in the correlation of bit stream symbol to over sample data..

## Known limitations

• The amount of samples is only approximated, or more accurate, taken as a minimum value. As only the counter is compared once a complete run length of the same symbols is seen, it will be always slightly above the require value.
• The amount of samples taken is hard coded. No means to vary this while running the application.
• When the ADC input is very close or the maximum input voltage (or very close tot the minimum input voltage) the resulting bit stream will contain mostly very long run length of one's and hardly any zero (or vice versa). As no clock is connected, the stream may become out of synchronization for these cases.
• Only the DC level is calculated, as a sum of all ones divided by the amount of symbols. Technically one could add Fourier transform in the post-processing and calculate SNR, THD, SINAD, ENOB etc, This requires another data structure of the histogram: store run length in the sequence they appear.
• The algorithm works only correct given two assumptions. There should be exactly one group of empty spaces between two groups of captured run lengths (each representing a different bit stream run length). And each group of run lengths may not contain any empty position. Another decoder http://en.wikipedia.org/wiki/Viterbi_algorithm would possibly do better and even could estimate a qualification number.

## Changes

RevisionDateWhoCommit message
13:2a66d067310b 2015-06-18 pscholtens Re-written core loop with use of macro's. Fixed number of un-synchronized number of samples. default tip
12:75acace69521 2015-06-17 pscholtens Flattened core of loop to speed-up sampling. Adding non-default unsafe option for faster loop start.
11:e80e38508fe6 2015-05-27 pscholtens Debug mode added, offset and dutycycle corrected, assigned values cleared too.
10:42e390f304fc 2015-05-27 pscholtens FastIO integrated, 4 seconds sampling, repair swapped compare operator.
9:8136aea421e3 2015-04-30 pscholtens Corrected output of new DC filter, still not working on bench.
8:38175daee62b 2015-04-24 pscholtens Version 0.1.1 Average function repaired, offset corrected, type cast typo.
7:5141bd76b08d 2015-04-22 pscholtens Version 0.1.0 More sophisticated recovery of symbols. Should extend duty cycle range from [1/3,2/3] to almost full scale [1/128,127/128].
6:a5fc4e2ff34b 2015-04-21 pscholtens version 0.0.9: Speedier performance dus to simpler core loop. Overflow will not toggle symbol anymore. Update of mbed library.
5:1c0bfd69719f 2015-04-17 pscholtens Version 0.0.8 Finally it works correctly, let's start measuring accuractely!
4:27a2eaee71ac 2015-04-17 pscholtens Version 0.0.7 Assignment of values implemented, underflow warning, corrected handling of skip run-in cycles.
3:8d13bf073e92 2015-04-15 pscholtens Corrected value, added skip of starting sequence and overflow remark.
2:5e37831540c7 2015-04-15 pscholtens It works, but not perfect :-)
1:2551859fbc25 2015-04-15 pscholtens Provisional version of synchronous detection is working.
0:dc1b041f713e 2015-04-14 pscholtens Version 0.0.3: unsynchronized counter of bitstream.