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.
Committer:
pscholtens
Date:
Wed Apr 15 06:54:12 2015 +0000
Revision:
1:2551859fbc25
Parent:
0:dc1b041f713e
Child:
2:5e37831540c7
Provisional version of synchronous detection is working.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pscholtens 0:dc1b041f713e 1 #include "mbed.h"
pscholtens 0:dc1b041f713e 2
pscholtens 1:2551859fbc25 3 /* version 0.0.4, P.C.S. Scholtens, Datang NXP, April 14th, 2015, Shanghai, PR China
pscholtens 1:2551859fbc25 4 Implement histogram to find run lengths of zroes and ones. */
pscholtens 1:2551859fbc25 5
pscholtens 1:2551859fbc25 6 /* version 0.0.3, P.C.S. Scholtens, Datang NXP, April 14th, 2015, Shanghai, PR China
pscholtens 1:2551859fbc25 7 Initial version. No synchronixzation of the symbols is done. */
pscholtens 0:dc1b041f713e 8
pscholtens 0:dc1b041f713e 9 /* See also:
pscholtens 0:dc1b041f713e 10 https://developer.mbed.org/forum/bugs-suggestions/topic/3464/
pscholtens 0:dc1b041f713e 11 */
pscholtens 0:dc1b041f713e 12
pscholtens 1:2551859fbc25 13 #define DEPTH 128
pscholtens 1:2551859fbc25 14
pscholtens 1:2551859fbc25 15 /* Reserve memory space for the histogram */
pscholtens 1:2551859fbc25 16 unsigned int zeros[DEPTH];
pscholtens 1:2551859fbc25 17 unsigned int ones[DEPTH];
pscholtens 1:2551859fbc25 18
pscholtens 1:2551859fbc25 19 DigitalIn bitstream(p11);
pscholtens 0:dc1b041f713e 20 DigitalOut myled(LED1);
pscholtens 0:dc1b041f713e 21 Serial pc(USBTX, USBRX); // tx, rx
pscholtens 0:dc1b041f713e 22
pscholtens 0:dc1b041f713e 23 Timer t;
pscholtens 0:dc1b041f713e 24
pscholtens 1:2551859fbc25 25 /* A function to clear the contents of both histograms */
pscholtens 1:2551859fbc25 26
pscholtens 1:2551859fbc25 27 void clear_histogram() {
pscholtens 1:2551859fbc25 28 for(unsigned int i = 0; i < DEPTH; i++) {
pscholtens 1:2551859fbc25 29 zeros[i] = 0;
pscholtens 1:2551859fbc25 30 ones[i] = 0;
pscholtens 1:2551859fbc25 31 }
pscholtens 1:2551859fbc25 32 }
pscholtens 1:2551859fbc25 33
pscholtens 1:2551859fbc25 34 /* Print the contents of the histogram, excluding the empty values */
pscholtens 1:2551859fbc25 35
pscholtens 1:2551859fbc25 36 void print_histogram() {
pscholtens 1:2551859fbc25 37 pc.printf("sequence zeros ones\n");
pscholtens 1:2551859fbc25 38 for (unsigned int i = 0; i < DEPTH; i++) {
pscholtens 1:2551859fbc25 39 wait(0.1);
pscholtens 1:2551859fbc25 40 if ( zeros[i]+ones[i] != 0 ) {
pscholtens 1:2551859fbc25 41 pc.printf("%08i %08i %08i\n",i,zeros[i],ones[i]);
pscholtens 1:2551859fbc25 42 }
pscholtens 1:2551859fbc25 43 }
pscholtens 1:2551859fbc25 44 }
pscholtens 1:2551859fbc25 45
pscholtens 1:2551859fbc25 46 /* Function which fill the histogram */
pscholtens 1:2551859fbc25 47 void fill_histogram(unsigned int num_unsync_samples) {
pscholtens 1:2551859fbc25 48 unsigned int count = 0;
pscholtens 1:2551859fbc25 49 unsigned int run_length = 0;
pscholtens 1:2551859fbc25 50 bool previous_bit = bitstream;
pscholtens 1:2551859fbc25 51 while(count < num_unsync_samples) {
pscholtens 1:2551859fbc25 52 while((previous_bit == (bool) bitstream) && (run_length < DEPTH)) {
pscholtens 1:2551859fbc25 53 run_length++;
pscholtens 1:2551859fbc25 54 };
pscholtens 1:2551859fbc25 55 if (previous_bit) {
pscholtens 1:2551859fbc25 56 ones[run_length]++;
pscholtens 1:2551859fbc25 57 }
pscholtens 1:2551859fbc25 58 else {
pscholtens 1:2551859fbc25 59 zeros[run_length]++;
pscholtens 1:2551859fbc25 60 }
pscholtens 1:2551859fbc25 61 run_length = 0;
pscholtens 1:2551859fbc25 62 previous_bit = !previous_bit;
pscholtens 1:2551859fbc25 63 count++;
pscholtens 1:2551859fbc25 64 }
pscholtens 1:2551859fbc25 65 }
pscholtens 1:2551859fbc25 66
pscholtens 1:2551859fbc25 67 /* Here we count the number of unsynchronized symbols, mimicing previous implementation */
pscholtens 1:2551859fbc25 68 unsigned int get_num_unsync_symbols(int symbol) {
pscholtens 1:2551859fbc25 69 unsigned int sum = 0;
pscholtens 1:2551859fbc25 70 for (unsigned int i = 0; i < DEPTH; i++) {
pscholtens 1:2551859fbc25 71 if (symbol == 0) {
pscholtens 1:2551859fbc25 72 sum += zeros[i];
pscholtens 1:2551859fbc25 73 } else {
pscholtens 1:2551859fbc25 74 sum += ones[i];
pscholtens 1:2551859fbc25 75 }
pscholtens 1:2551859fbc25 76 }
pscholtens 1:2551859fbc25 77 return sum;
pscholtens 1:2551859fbc25 78 }
pscholtens 1:2551859fbc25 79
pscholtens 1:2551859fbc25 80 /* The main routine of the program */
pscholtens 1:2551859fbc25 81
pscholtens 0:dc1b041f713e 82 int main() {
pscholtens 1:2551859fbc25 83 unsigned int num_of_zeros, num_of_ones;
pscholtens 1:2551859fbc25 84 float dutycycle, voltage;
pscholtens 1:2551859fbc25 85 pc.baud(115200);
pscholtens 1:2551859fbc25 86 pc.printf("Bitstream counter, version 0.0.4\n");
pscholtens 0:dc1b041f713e 87 /*LPC_TIM2->PR = 0x0000002F; / * decimal 47 */
pscholtens 0:dc1b041f713e 88 /*LPC_TIM3->PR = 24;*/
pscholtens 1:2551859fbc25 89 clear_histogram();
pscholtens 0:dc1b041f713e 90 t.start();
pscholtens 0:dc1b041f713e 91 while(1) {
pscholtens 0:dc1b041f713e 92 t.reset();
pscholtens 0:dc1b041f713e 93 myled = 1;
pscholtens 1:2551859fbc25 94 clear_histogram();
pscholtens 1:2551859fbc25 95 pc.printf("Cleared\n");
pscholtens 1:2551859fbc25 96 fill_histogram(1e5);
pscholtens 1:2551859fbc25 97 pc.printf("Filled\n");
pscholtens 1:2551859fbc25 98 print_histogram();
pscholtens 1:2551859fbc25 99 pc.printf("Printed\n");
pscholtens 1:2551859fbc25 100 num_of_zeros = get_num_unsync_symbols(0);
pscholtens 1:2551859fbc25 101 num_of_ones = get_num_unsync_symbols(1);
pscholtens 1:2551859fbc25 102 dutycycle = ((float) num_of_ones)/(num_of_zeros+num_of_ones); /* We need to typecast one of the integers to float, otherwise the result is rounded till zero. */
pscholtens 0:dc1b041f713e 103 voltage = (0.5*13*dutycycle+1)*0.9; /* This is the ADC formula, see analysisSigmaDeltaADC.pdf */
pscholtens 1:2551859fbc25 104 pc.printf("Counted %i/%i during %f seconds, dutycycle = %f, voltage = %f\n", num_of_zeros, num_of_ones, t.read(), dutycycle, voltage);
pscholtens 0:dc1b041f713e 105 myled = 0;
pscholtens 0:dc1b041f713e 106 wait(1.0);
pscholtens 0:dc1b041f713e 107 }
pscholtens 0:dc1b041f713e 108 }