A simple example.

Dependencies:   mbed FastIO

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.
Revision:
10:42e390f304fc
Parent:
9:8136aea421e3
Child:
11:e80e38508fe6
--- a/main.cpp	Thu Apr 30 06:34:56 2015 +0000
+++ b/main.cpp	Wed May 27 09:53:34 2015 +0000
@@ -1,4 +1,13 @@
 #include "mbed.h"
+#include "FastIO.h"
+
+/* version 0.1.2, P.C.S. Scholtens, Datang NXP, May 27th 2015, Nijmegen, Netherlands
+   - Switched from the default I/O to FastIO library to enables faster reading of the
+     input. The distinction between various run length should be improved.
+   - Incremented number of samples to 4x10e7, with a duration of approx. 4 seconds.
+   - Repaired bug of always clipped values in run length (accidentally swapped compare
+     operator).
+*/
 
 /* version 0.1.1, P.C.S. Scholtens, Datang NXP, April 24th 2015, Nijmegen, Netherlands
    - Average function did not return the calculated average, now repaired.
@@ -46,7 +55,7 @@
    Implement histogram to find run lengths of zeroes and ones. */
 
 /* version 0.0.4, P.C.S. Scholtens, Datang NXP, April 14th, 2015, Shanghai, PR China
-   Implement histogram to find run lengths of zroes and ones. */
+   Implement histogram to find run lengths of zeroes and ones. */
 
 /* version 0.0.3, P.C.S. Scholtens, Datang NXP, April 14th, 2015, Shanghai, PR China
    Initial version. No synchronization of the symbols is done. */
@@ -60,15 +69,15 @@
 
 */
 
-#define  DEPTH  128
-#define  WATCH_DOG_TIME  4
+#define  DEPTH  1024
+#define  WATCH_DOG_TIME  10
 
 /* Reserve memory space for the histogram */
 unsigned int zeros[DEPTH];
 unsigned int ones[DEPTH];
 unsigned int assign[DEPTH];
 
-DigitalIn  bitstream(p11);
+FastIn<p11> bitstream;
 DigitalOut myled(LED1);
 Serial     pc(USBTX, USBRX); // tx, rx
 Timer      timer;
@@ -158,15 +167,15 @@
 void fill_histogram(unsigned int num_unsync_samples) {
     unsigned int count = 0;
     unsigned int run_length = 0;
-    bool previous_bit = (bool) bitstream;
     /* Switch on watch dog timer */
     timeout.attach(&at_time_out, WATCH_DOG_TIME);
+    bool previous_bit = (bool) bitstream;
     /* Implements run-in: skip the first sequence as it is only a partial one. */
     while(previous_bit == (bool) bitstream) {
         /* Do nothing, intentionally */;
     };
+    run_length   =  0;
     previous_bit =  !previous_bit;
-    run_length   =  0;
     /* Start actual counting here, store in variable run_length (will be clipped to DEPTH) */
     while(count < num_unsync_samples) {
         /* Core of the loop */
@@ -176,7 +185,7 @@
         /* Increment counter before clipping to preserve accuracy. */
         count += run_length;
         /* Test if run length exceeds depth of histogram, if so assign clip value. */
-        if (run_length < DEPTH-1) {
+        if (run_length > DEPTH-1) {
             run_length = DEPTH-1;
         }
         /* Now write in histogram array of interest */
@@ -335,7 +344,7 @@
                 sum_of_unsync_symbols, sum_of_synced_symbols;
     float unsync_dutycycle, synced_dutycycle, unsync_voltage, synced_voltage, synced_dutycycle_new, synced_voltage_new;
     pc.baud(115200);
-    pc.printf("Bitstream counter, version 0.1.1, P.C.S. Scholtens, April 24th 2015, Nijmegen, Netherlands.\n");
+    pc.printf("Bitstream counter, version 0.1.2, P.C.S. Scholtens, May 27th 2015, Nijmegen, Netherlands.\n");
     pc.printf("Build " __DATE__ " " __TIME__ "\n");
     /*LPC_TIM2->PR = 0x0000002F;  / * decimal 47 */ 
     /*LPC_TIM3->PR = 24;*/
@@ -345,7 +354,7 @@
         myled = 1;
         clear_histogram();
         timer.start();
-        fill_histogram(1e7);
+        fill_histogram(4e7);
         timer.stop();
         pc.printf("\n------   Captured Histogram   ------\n");
         print_histogram();