![](/media/cache/profiles/1c34ff889cc1ed4136b6b0e2310e9162.jpg.50x50_q85.jpg)
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.
Diff: main.cpp
- Revision:
- 11:e80e38508fe6
- Parent:
- 10:42e390f304fc
- Child:
- 12:75acace69521
diff -r 42e390f304fc -r e80e38508fe6 main.cpp --- a/main.cpp Wed May 27 09:53:34 2015 +0000 +++ b/main.cpp Wed May 27 13:00:37 2015 +0000 @@ -1,6 +1,14 @@ #include "mbed.h" #include "FastIO.h" +/* 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. + - Repaired bug in calculation of offset. + - Cleared assined values also in clear_histogram() function. + - Corrected return value of new synchronization method, dutycycles was calculated + in wrong way. +*/ + /* 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. @@ -69,8 +77,9 @@ */ -#define DEPTH 1024 +#define DEPTH 1024 #define WATCH_DOG_TIME 10 +#undef DEBUG_MODE /* Reserve memory space for the histogram */ unsigned int zeros[DEPTH]; @@ -135,8 +144,9 @@ /* 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; + zeros[i] = 0; + ones[i] = 0; + assign[i] = 0; } } @@ -286,6 +296,15 @@ index->calc_average(); index = index->next; } +#ifdef DEBUG_MODE + int j = 0; + index = list; + while (index != NULL) { + pc.printf("Group %i from %i to %i, average = %f\n", j, index->index_start,index->index_stop, index->average); + index = index->next; + j++; + } +#endif /* Step three (第三步): Find smallest distance between two adjacent symbols, e.g. with run length of 0.91, 6.99, 8.01, the last two define the grid/oversample ratio. */ float oversample = DEPTH; Recovered* cmp1 = list; @@ -300,6 +319,9 @@ cmp2=cmp1->next; } } +#ifdef DEBUG_MODE + pc.printf("Oversample ratio %f\n", oversample); +#endif /* Step four (第四步): Divide the average run length of all found recovered symbol by the found oversample ratio. */ index = list; while (index != NULL) { @@ -307,13 +329,16 @@ index = index->next; } - /* Step five (第五步): find offset and remove it (Assumption that there are always symbol with run length 1 ) */ + /* Step five (第五步): find offset and remove it (Assumption that there are always symbols with run length 1 ) */ index = list; - float offset = oversample-index->average; + float offset = 1-index->average; while (index != NULL) { index->average += offset; index = index->next; } +#ifdef DEBUG_MODE + pc.printf("Offset at initial run-in lengths %f\n", offset); +#endif /* Step six (第六步): round to nearest integer and assign value to both arrays */ index = list; @@ -334,21 +359,24 @@ /* Step eight (第八步): Delete the recovered symbol object to clear memory. As a destructor is defined this will be automatically handled recursively. And of course return the duty cycle */ delete list; - return ((float) sum0)/sum1; + return ((float) sum1)/(sum0+sum1); } /* The main (主程序) routine of the program */ int main() { +#ifdef DEBUG_MODE unsigned int num_of_zeros, num_of_ones, value_of_unsync_zeros, value_of_unsync_ones, value_of_synced_zeros, value_of_synced_ones, sum_of_unsync_symbols, sum_of_synced_symbols; - float unsync_dutycycle, synced_dutycycle, unsync_voltage, synced_voltage, synced_dutycycle_new, synced_voltage_new; + float unsync_voltage, synced_voltage, unsync_dutycycle, synced_dutycycle; +#endif + + float synced_dutycycle_new, synced_voltage_new; pc.baud(115200); - pc.printf("Bitstream counter, version 0.1.2, P.C.S. Scholtens, May 27th 2015, Nijmegen, Netherlands.\n"); + pc.printf("Bitstream counter, version 0.1.3, 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;*/ - clear_histogram(); while(1) { timer.reset(); myled = 1; @@ -356,8 +384,7 @@ timer.start(); fill_histogram(4e7); timer.stop(); - pc.printf("\n------ Captured Histogram ------\n"); - print_histogram(); +#ifdef DEBUG_MODE 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); @@ -370,17 +397,22 @@ sum_of_synced_symbols = value_of_synced_zeros+value_of_synced_ones; synced_dutycycle = ((float) value_of_synced_ones)/sum_of_synced_symbols; /* 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 */ +#endif synced_dutycycle_new = get_dutycycle_synced_symbols_new_method(); synced_voltage_new = (0.5*13*synced_dutycycle_new+1)*0.9; /* This is the ADC formula, see analysisSigmaDeltaADC.pdf */ + pc.printf("\n------ Captured Histogram ------\n"); + print_histogram(); +#ifdef DEBUG_MODE 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("Duty Cycle %f, = %f Volt\n", unsync_dutycycle , unsync_voltage); + pc.printf("----- Synchronized Results OLD -----\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("----- Synchronized Results NEW -----\n"); - pc.printf("Duty Cyle %f, equals %f Volt\n", synced_dutycycle_new , synced_voltage_new); + pc.printf("Duty Cyle %f, = %f Volt\n", synced_dutycycle , synced_voltage); +#endif + pc.printf("------- Synchronized Results -------\n"); + pc.printf("Duty Cyle %f, = %f Volt\n", synced_dutycycle_new , synced_voltage_new); pc.printf("------------------------------------\n"); pc.printf("Measured in %f sec.\n", timer.read()); pc.printf("====================================\n");