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:
12:75acace69521
Parent:
11:e80e38508fe6
Child:
13:2a66d067310b
--- a/main.cpp	Wed May 27 13:00:37 2015 +0000
+++ b/main.cpp	Wed Jun 17 08:23:26 2015 +0000
@@ -1,8 +1,18 @@
 #include "mbed.h"
 #include "FastIO.h"
 
+/* version 0.1.4, P.C.S. Scholtens, Datang NXP, June 17th 2015, Nijmegen, Netherlands
+   - Flattened core loop of the fill_histogram() function. As both symbols 0 and 1 have
+     their own core, sampling can be done faster: there's no comparison with the
+     previous value (which was the variable previous_bit) but only a comparison with a
+     constant 0 or 1. The sampling of longer sequences is now 1.5 times faster.
+   - Added compiler pre-processor directive ALLOW_OUT_OF_RANGE to allow omitting
+     out-of-range check of variable run_length. This improves sampling speed, but may
+     cause segfaults.
+*/
+
 /* version 0.1.3, P.C.S. Scholtens, Datang NXP, May 27th 2015, Nijmegen, Netherlands
-   - Added debug mode via compiler preprocessor directives, just for developers.
+   - Added debug mode via compiler pre-processor directives, just for developers.
    - Repaired bug in calculation of offset.
    - Cleared assined values also in clear_histogram() function.
    - Corrected return value of new synchronization method, dutycycles was calculated
@@ -80,6 +90,7 @@
 #define  DEPTH           1024
 #define  WATCH_DOG_TIME  10
 #undef   DEBUG_MODE
+#undef   ALLOW_OUT_OF_RANGE
 
 /* Reserve memory space for the histogram */
 unsigned int zeros[DEPTH];
@@ -179,35 +190,49 @@
     unsigned int run_length = 0;
     /* 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) {
+    /* Implements run-in: skip the first sequence of ZEROs as it is only a partial one. */
+    while(!(bool) bitstream) {
+        /* Do nothing, intentionally */;
+    };
+    /* Implements run-in: skip the first sequence of ONEs as we always want to start with zeros. */
+    while( (bool) bitstream) {
         /* Do nothing, intentionally */;
     };
     run_length   =  0;
-    previous_bit =  !previous_bit;
-    /* Start actual counting here, store in variable run_length (will be clipped to DEPTH) */
+    /* Start actual counting ZEROs here, store in variable run_length (will be clipped to DEPTH) */
     while(count < num_unsync_samples) {
-        /* Core of the loop */
-        while(previous_bit == (bool) bitstream) {
+        /* Core of the ZERO loop */
+        while(! (bool) bitstream) {
             run_length++;
         };
         /* Increment counter before clipping to preserve accuracy. */
         count += run_length;
         /* Test if run length exceeds depth of histogram, if so assign clip value. */
+#ifndef ALLOW_OUT_OF_RANGE
         if (run_length > DEPTH-1) {
             run_length = DEPTH-1;
         }
-        /* Now write in histogram array of interest */
-        if (previous_bit) {
-            ones[run_length]++;
-        }
-        else {
-            zeros[run_length]++;
-        }
+#endif
+        /* Now write in histogram array of ZERO's */
+        zeros[run_length]++;
         /* Reset for next run length counting loop */
         run_length   =  0;
-        previous_bit =  !previous_bit;
+        /* Core of the ONES loop */
+        while(  (bool) bitstream) {
+            run_length++;
+        };
+        /* Increment counter before clipping to preserve accuracy. */
+        count += run_length;
+        /* Test if run length exceeds depth of histogram, if so assign clip value. */
+#ifndef ALLOW_OUT_OF_RANGE
+        if (run_length > DEPTH-1) {
+            run_length = DEPTH-1;
+        }
+#endif
+        /* Now write in histogram array of ONES's */
+        ones[run_length]++;
+        /* Reset for next run length counting loop */
+        run_length   =  0;
     }
     /* Switch off watch dog timer */
     timeout.detach();
@@ -373,7 +398,7 @@
 
     float synced_dutycycle_new, synced_voltage_new;
     pc.baud(115200);
-    pc.printf("Bitstream counter, version 0.1.3, P.C.S. Scholtens, May 27th 2015, Nijmegen, Netherlands.\n");
+    pc.printf("Bitstream counter, version 0.1.4, P.C.S. Scholtens, June 17th 2015, Nijmegen, Netherlands.\n");
     pc.printf("Build " __DATE__ " " __TIME__ "\n");
     /*LPC_TIM2->PR = 0x0000002F;  / * decimal 47 */ 
     /*LPC_TIM3->PR = 24;*/