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.
History
Re-written core loop with use of macro's. Fixed number of un-synchronized number of samples.
2015-06-18, by pscholtens [Thu, 18 Jun 2015 12:31:06 +0000] rev 13
Re-written core loop with use of macro's. Fixed number of un-synchronized number of samples.
Flattened core of loop to speed-up sampling. Adding non-default unsafe option for faster loop start.
2015-06-17, by pscholtens [Wed, 17 Jun 2015 08:23:26 +0000] rev 12
Flattened core of loop to speed-up sampling. Adding non-default unsafe option for faster loop start.
Debug mode added, offset and dutycycle corrected, assigned values cleared too.
2015-05-27, by pscholtens [Wed, 27 May 2015 13:00:37 +0000] rev 11
Debug mode added, offset and dutycycle corrected, assigned values cleared too.
FastIO integrated, 4 seconds sampling, repair swapped compare operator.
2015-05-27, by pscholtens [Wed, 27 May 2015 09:53:34 +0000] rev 10
FastIO integrated, 4 seconds sampling, repair swapped compare operator.
Corrected output of new DC filter, still not working on bench.
2015-04-30, by pscholtens [Thu, 30 Apr 2015 06:34:56 +0000] rev 9
Corrected output of new DC filter, still not working on bench.
Version 0.1.1 Average function repaired, offset corrected, type cast typo.
2015-04-24, by pscholtens [Fri, 24 Apr 2015 12:00:38 +0000] rev 8
Version 0.1.1 Average function repaired, offset corrected, type cast typo.
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].
2015-04-22, by pscholtens [Wed, 22 Apr 2015 18:15:20 +0000] rev 7
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].
version 0.0.9: Speedier performance dus to simpler core loop. Overflow will not toggle symbol anymore. Update of mbed library.
2015-04-21, by pscholtens [Tue, 21 Apr 2015 12:19:41 +0000] rev 6
version 0.0.9: Speedier performance dus to simpler core loop. Overflow will not toggle symbol anymore. Update of mbed library.
Version 0.0.8 Finally it works correctly, let's start measuring accuractely!
2015-04-17, by pscholtens [Fri, 17 Apr 2015 02:19:41 +0000] rev 5
Version 0.0.8 Finally it works correctly, let's start measuring accuractely!
Version 0.0.7 Assignment of values implemented, underflow warning, corrected handling of skip run-in cycles.
2015-04-17, by pscholtens [Fri, 17 Apr 2015 01:37:20 +0000] rev 4
Version 0.0.7 Assignment of values implemented, underflow warning, corrected handling of skip run-in cycles.