m b / Mbed 2 deprecated AoA_estimator

Dependencies:   mbed dsp

Revision:
14:8865ec38bdc8
Parent:
10:cd3f7010da48
Child:
15:29805fab7655
diff -r d221d9ef6338 -r 8865ec38bdc8 Phase_Finder.h
--- a/Phase_Finder.h	Thu Apr 28 06:25:32 2016 +0000
+++ b/Phase_Finder.h	Thu Apr 28 17:20:41 2016 +0000
@@ -3,19 +3,16 @@
 const int SAMPLE_LENGTH = 251;
 const int PEAKS = 4;
 
-/** AoA_Est.h
-*   Computes the angle of arrival of an audio signal
+/** Phase_Finder.h
+*   Computes the phase of a signal using correlation
 *   EXAMPLE:
     @code
-    //Find the angle of arrival of a 900Hz audio signal using 3 MBEDS equipped with microphones. 
-    //Requires an array of phases, found using Phase_Finder and communication between MBEDs. 
-    //This code begins after phases have been found and communicated to master MBED
-    //Sensors are located at (0,0), (150,0), and (150,90). Maximum sensors is 10 including master. Can be changed with parameter MAX_SENSORS
-    float angle = 0;
-    int x[2] = {150, 150); //Distances from master MBED microphone in milimeters. All distances must be for microphones, not MBED units
-    int y[2] = {0, 90);  //Do not include master MBED, its microphone is assumed to be 0,0.
-    AoA_Est AoA(3, x, y, 900);
-    angle = AoA.estimate(phases, phases);
+    //Estimate the phase of a 900Hz input signal.
+    //Requires an input cosine reference. Generated in MATLAB and placed into an array not shown. 
+    float reference[307] = {...};
+    float phase = 0;
+    Phase_Finder phase(50000, 900, reference);
+    phase - phase.estimate(signal, 251);
 * }
 * @endcode
 */
@@ -23,13 +20,18 @@
 class Phase_Finder {
 
 public:
-    /** Create a
+    /** Create a Phase_Finder object
     *
-    * @param _pin mbed AnalogIn pin where the analog output of sensor is connected
-    *
-    * @note Supported types of sensors:
+    * @param sampleRate ADC Sample Rate
+    * @param frequency Frequency of Interest in Hz
+    * @param reference[] Cosine Reference signal used for correlation. Must be input sample length + ceil(sampleRate/Frequency). Must be same frequency as input frequency
     */
     Phase_Finder(int sampleRate, float frequency, float reference[]);
+    /** Estimate the phase
+    *
+    * @param samples[] Input samples
+    * @param leng Length of input sample array
+    */
     float estimate(float samples[], int leng);
 
 
@@ -55,18 +57,20 @@
 
 void Phase_Finder::est_Max(float samples1[]) {
     float change = 0;
-    
+    //Remove random high amplitude distortion
     for (int i = 2; i < length - 1; i++) {
         change = abs(samples1[i - 2] - samples1[i - 1]);
         if (abs(samples1[i] - samples1[i-1]) > change*4.5)
             samples1[i] = (samples1[i - 1] + samples1[i + 1]) / 2;
     }
+    //Cross correlation
     float corr[56] = {};
     for (int j = 0; j < 56; j++) {
         for (int i = 0; i <= length; i++) {
             corr[j] += reference[i-j+55] * samples1[i];
         }
     }
+    //Find maximum correlation
     float max = 0;
     for (int i = 0; i < 56; i++) {
         if (max < corr[i]) {
@@ -74,7 +78,7 @@
             indices1[0] = i;
         }
     }
-
+    //Peak finder method. Inferior to correlation
     /*for (int j = 0; j<peaks; j++) {
         float max = 0;
         
@@ -102,6 +106,7 @@
 float Phase_Finder::est_Phase() {
     //float avgDist = 0;
     float ph;
+    //Used for peak finding averaging. Not necessary.
     //for (int i = 0; i<peaks - 1; i++){
     //  avgDist += indices1[i] - round(sampleRate/frequency*i);
     //}