Peter Scholtens / Mbed 2 deprecated SimpleDecimationFilter

Dependencies:   mbed FastIO

Files at this revision

API Documentation at this revision

Comitter:
pscholtens
Date:
Thu Jun 18 12:31:06 2015 +0000
Parent:
12:75acace69521
Commit message:
Re-written core loop with use of macro's. Fixed number of un-synchronized number of samples.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Jun 17 08:23:26 2015 +0000
+++ b/main.cpp	Thu Jun 18 12:31:06 2015 +0000
@@ -1,6 +1,15 @@
 #include "mbed.h"
 #include "FastIO.h"
 
+/* version 0.1.5, P.C.S. Scholtens, Datang NXP, June 18th 2015, Nijmegen, Netherlands
+   - Converter function parameter num_unsync_samples in fill_histogram() to constant
+     NUM_UNSYNC_SAMPLES to speed up comparison of the core while loop.
+   - Re-written core loop with nested macro's. This ensures that the code will stay
+     exactly the same for both symbol 0 and 1 (Don't Repeat Yourself...).
+   - At start-up mention is this executable has the debug and/or allow option activated.
+   - Print also the assigned value of the detected underflow sequences.
+*/
+
 /* 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
@@ -87,10 +96,11 @@
 
 */
 
-#define  DEPTH           1024
-#define  WATCH_DOG_TIME  10
+#define  DEPTH               1024
+#define  WATCH_DOG_TIME      10
+#define  NUM_UNSYNC_SAMPLES  4e7
 #undef   DEBUG_MODE
-#undef   ALLOW_OUT_OF_RANGE
+//#define  ALLOW_OUT_OF_RANGE
 
 /* Reserve memory space for the histogram */
 unsigned int zeros[DEPTH];
@@ -165,7 +175,7 @@
 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]);
+        pc.printf("Underflow %8i %8i %8i\n",zeros[0],ones[0],assign[0]);
     }
     for (unsigned int i = 1; i < DEPTH-1; i++) {
         if ( zeros[i]+ones[i] != 0 ) {
@@ -185,57 +195,37 @@
 }
 
 /* Function which fill the histogram */
-void fill_histogram(unsigned int num_unsync_samples) {
+void fill_histogram() {
+
+/* Make the macro definition of clip_run_length() macro dependent of the existence of ALLOW_OUT_OF_RANGE.
+ * This optional macro tests if run length exceeds the defined depth of histogram, and if so assigns the clip value. */
+#ifdef ALLOW_OUT_OF_RANGE
+#define clip_run_length() ;
+#else
+#define clip_run_length() if (run_length > DEPTH-1) {run_length = DEPTH-1; }
+#endif
+/* Now define the center loop macro as it will be used in two symbol flavours: either 0 or 1. */
+#define fast_loop(symbol) run_length = 0; while( (bool) bitstream == symbol) {run_length++;}; count += run_length; clip_run_length();
     unsigned int count = 0;
     unsigned int run_length = 0;
     /* Switch on watch dog timer */
     timeout.attach(&at_time_out, WATCH_DOG_TIME);
     /* Implements run-in: skip the first sequence of ZEROs as it is only a partial one. */
-    while(!(bool) bitstream) {
-        /* Do nothing, intentionally */;
-    };
+    fast_loop(0);
     /* 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;
-    /* Start actual counting ZEROs here, store in variable run_length (will be clipped to DEPTH) */
-    while(count < num_unsync_samples) {
-        /* 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;
-        }
-#endif
-        /* Now write in histogram array of ZERO's */
+    fast_loop(1);
+    while(count < NUM_UNSYNC_SAMPLES ) {
+        /* Core of the loop! */
+        fast_loop(0);
         zeros[run_length]++;
-        /* Reset for next run length counting loop */
-        run_length   =  0;
-        /* 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 */
+        fast_loop(1);
         ones[run_length]++;
-        /* Reset for next run length counting loop */
-        run_length   =  0;
     }
     /* Switch off watch dog timer */
     timeout.detach();
+/* Do not use outside this scope */
+#undef fast_loop
+#undef clip_run_length
 }
 
 /* Here we count the number of unsynchronized symbols, mimicing previous implementation */
@@ -398,8 +388,16 @@
 
     float synced_dutycycle_new, synced_voltage_new;
     pc.baud(115200);
-    pc.printf("Bitstream counter, version 0.1.4, P.C.S. Scholtens, June 17th 2015, Nijmegen, Netherlands.\n");
-    pc.printf("Build " __DATE__ " " __TIME__ "\n");
+    pc.printf("Bitstream counter, version 0.1.5, P.C.S. Scholtens, June 18th 2015, Nijmegen, Netherlands.\n");
+    pc.printf("Build: " __DATE__ ", " __TIME__ );
+#ifdef DEBUG_MODE
+    pc.printf(", debug mode");
+#endif
+#ifdef ALLOW_OUT_OF_RANGE
+    pc.printf(", allows out-of-range");
+#endif
+    pc.printf(".\n");
+
     /*LPC_TIM2->PR = 0x0000002F;  / * decimal 47 */ 
     /*LPC_TIM3->PR = 24;*/
     while(1) {
@@ -407,7 +405,7 @@
         myled = 1;
         clear_histogram();
         timer.start();
-        fill_histogram(4e7);
+        fill_histogram();
         timer.stop();
 #ifdef DEBUG_MODE
         num_of_zeros = get_num_unsync_symbols(0);
--- a/mbed.bld	Wed Jun 17 08:23:26 2015 +0000
+++ b/mbed.bld	Thu Jun 18 12:31:06 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/433970e64889
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7
\ No newline at end of file