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.

main.cpp

Committer:
pscholtens
Date:
2015-04-17
Revision:
4:27a2eaee71ac
Parent:
3:8d13bf073e92
Child:
5:1c0bfd69719f

File content as of revision 4:27a2eaee71ac:

#include "mbed.h"

/* version 0.0.7, P.C.S. Scholtens, Datang NXP, April 16/17th 2015, Shanghai, PR China
   - Method written to assign synchronized values to run-length.
   - Added warnings for underflow.
   - After skipped run-in cycles, copy the current bit, to prevent false single hit.
*/

/* version 0.0.6, P.C.S. Scholtens, Datang NXP, April 15th, 2015, Shanghai, PR China
   - Corrected duty-cycle output for actual value of symbols (Thanks to Richard Zhu!).
   - Skipped run-in cycles to avoid pollution of the histogram with the first, most
     likely partial, sequence captured.
   - Added warnings for overflow.
*/

/* version 0.0.5, P.C.S. Scholtens, Datang NXP, April 14th, 2015, Shanghai, PR China
   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. */

/* version 0.0.3, P.C.S. Scholtens, Datang NXP, April 14th, 2015, Shanghai, PR China
   Initial version. No synchronixzation of the symbols is done. */

/* See also:
https://developer.mbed.org/forum/bugs-suggestions/topic/3464/
*/

#define  DEPTH  128

/* Reserve memory space for the histogram */
unsigned int zeros[DEPTH];
unsigned int ones[DEPTH];
unsigned int assign[DEPTH];

DigitalIn bitstream(p11);
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX); // tx, rx

Timer t;

/* A function to clear the contents of both histograms */
void clear_histogram() {
    for(unsigned int i = 0; i < DEPTH; i++) {
        zeros[i] = 0;
        ones[i]  = 0;
    }
}

/* Print the contents of the histogram, excluding the empty values */
void print_histogram() {
    pc.printf(" Sequence    Zeros     Ones   Assign\n");
    if ( zeros[0]+ones[0] != 0 ) {
        pc.printf("Underflow %8i %8i\n",zeros[0],ones[0]);
    }
    for (unsigned int i = 1; i < DEPTH-1; i++) {
        if ( zeros[i]+ones[i] != 0 ) {
            pc.printf(" %8i %8i %8i %8i\n",i,zeros[i],ones[i],assign[i]);
        }
    }
    if ( zeros[DEPTH-1]+ones[DEPTH-1] != 0 ) {
        pc.printf("Overflow  %8i %8i\n",zeros[DEPTH-1],ones[DEPTH-1]);
    }

}

/* Function which fill the histogram */
void fill_histogram(unsigned int num_unsync_samples) {
    unsigned int count = 0;
    unsigned int run_length = 0;
    bool previous_bit = (bool) bitstream;
    /* Implements run-in: skip the first sequence as it is only a partial one. */
    while((previous_bit == (bool) bitstream)  && (run_length < DEPTH-1)) {
        run_length++;
    };
    previous_bit =  !previous_bit;
    /* Start actual counting here */
    run_length   =  0;
    while(count < num_unsync_samples) {
        while((previous_bit == (bool) bitstream)  && (run_length < DEPTH-1)) {
            run_length++;
        };
        if (previous_bit) {
            ones[run_length]++;
        }
        else {
            zeros[run_length]++;
        }
        count        += run_length;
        run_length   =  0;
        previous_bit =  !previous_bit;
    }
}

/* Here we count the number of unsynchronized symbols, mimicing previous implementation */
unsigned int get_num_unsync_symbols(int symbol) {
    unsigned int sum = 0;
    for (unsigned int i = 0; i < DEPTH; i++) {
        if (symbol == 0) {
            sum += zeros[i];
        } else {
            sum += ones[i];
        }
    }
    return sum;
}

/* Calculate the value, using the unsynchronized method */
unsigned int get_value_unsync_symbols(int symbol) {
    unsigned int sum = 0;
    for (unsigned int i = 0; i < DEPTH; i++) {
        if (symbol == 0) {
            sum += i*zeros[i];
        } else {
            sum += i*ones[i];
        }
    }
    return sum;
}

/* Calculate the value, using the synchronization algorithm */
unsigned int get_value_synced_symbols(int symbol) {
    bool presence = false;
    int value = 0;
    for (unsigned int i = 0; i < DEPTH; i++) {
        if ( zeros[i]+ones[i] != 0 ) {
            if (presence) {
                assign[i]=value;
            } else {
                value++;
                presence = true;
            }
        } else {
            presence = false;
        }
    }
    /* Now do the actual summation of symbol values */
    unsigned int sum = 0;
    for (unsigned int i = 0; i < DEPTH; i++) {
        if (symbol == 0) {
            sum += assign[i]*zeros[i];
        } else {
            sum += assign[i]*ones[i];
        }
    }
    return sum;
}


/* The main routine of the program */

int main() {
    unsigned int num_of_zeros, num_of_ones, value_of_unsync_zeros, value_of_unsync_ones, value_of_synced_zeros, value_of_synced_ones;
    float unsync_dutycycle, synced_dutycycle, unsync_voltage, synced_voltage;
    pc.baud(115200);
    pc.printf("Bitstream counter, version 0.0.7\n");
    /*LPC_TIM2->PR = 0x0000002F;  / * decimal 47 */ 
    /*LPC_TIM3->PR = 24;*/
    clear_histogram();
    while(1) {
        t.reset();
        myled = 1;
        clear_histogram();
        t.start();
        fill_histogram(1e6);
        t.stop();
        pc.printf("\n------   Captured Histogram   ------\n");
        print_histogram();
        num_of_zeros = get_num_unsync_symbols(0);
        num_of_ones  = get_num_unsync_symbols(1);
        value_of_unsync_zeros = get_value_unsync_symbols(0);
        value_of_unsync_ones  = get_value_unsync_symbols(1);
        unsync_dutycycle = ((float) value_of_unsync_ones)/(value_of_unsync_zeros+value_of_unsync_ones); /* We need to typecast one of the integers to float, otherwise the result is rounded till zero. */
        unsync_voltage   = (0.5*13*unsync_dutycycle+1)*0.9; /* This is the ADC formula, see analysisSigmaDeltaADC.pdf */
        value_of_synced_zeros = get_value_synced_symbols(0);
        value_of_synced_ones  = get_value_synced_symbols(1);
        synced_dutycycle = ((float) value_of_synced_ones)/(value_of_synced_zeros+value_of_synced_ones); /* We need to typecast one of the integers to float, otherwise the result is rounded till zero. */
        synced_voltage   = (0.5*13*synced_dutycycle+1)*0.9; /* This is the ADC formula, see analysisSigmaDeltaADC.pdf */
        pc.printf("------ Unsynchronized Results ------\n");
        pc.printf("Counted Sequences  %8i %8i\n",           num_of_zeros         , num_of_ones);
        pc.printf("Summed Values      %8i %8i\n",           value_of_unsync_zeros, value_of_unsync_ones);
        pc.printf("Duty Cycle %f, equals %f Volt\n",        unsync_dutycycle     , unsync_voltage);
        pc.printf("------  Synchronized Results  ------\n");
        pc.printf("Summed Values      %8i %8i\n",           value_of_synced_zeros, value_of_synced_ones);
        pc.printf("Duty Cyle %f, equals %f Volt\n",        synced_dutycycle     , synced_voltage);
        pc.printf("------------------------------------\n");
        pc.printf("Measured in %f sec.\n",                  t.read());
        pc.printf("====================================\n");
        myled = 0;
        wait(1.0);
    }
}