For checking sesnsor of sound

Dependencies:   mbed-dsp

Files at this revision

API Documentation at this revision

Comitter:
josephpulin
Date:
Tue Feb 04 15:06:00 2020 +0000
Parent:
0:05e2c9ca68e2
Commit message:
Don't worry bee happy

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 05e2c9ca68e2 -r 8f4e141c6819 main.cpp
--- a/main.cpp	Fri Apr 13 08:59:47 2018 +0000
+++ b/main.cpp	Tue Feb 04 15:06:00 2020 +0000
@@ -9,65 +9,90 @@
 /* FFT settings */
 #define SAMPLES                 512             /* 256 real party and 256 imaginary parts */
 #define FFT_SIZE                SAMPLES / 2     /* FFT size is always the same size as we have samples, so 256 in our case */
+#define SEUIL                   0.4
+
 
 /* Global variables */
 float32_t Input[SAMPLES];
 float32_t Output[FFT_SIZE];
 bool      trig=0;
-/* MBED class APIs */
+
+Serial pc(USBTX, USBRX);
 DigitalOut myled(LED1);
-AnalogIn   myADC(A1);
-AnalogOut  myDAC(D13);
-Serial     pc(USBTX, USBRX);
+AnalogIn loudness(A6);
 Ticker     timer;
 
+float32_t tab_temp[8][4];
+float32_t moyenne[4];
+
 void sample(){
     trig=1;
-    }
+}
 
 int main() {
-
-    //arm_cfft_instance_f32 S;   // ARM CFFT module
     float maxValue;            // Max FFT value is stored here
     uint32_t maxIndex;         // Index in Output array where max value is
-    bool once=0;
+    float maxv;
     pc.baud(115200);
     pc.printf("Starting FFT\r\n");
-    while(1) {
-            timer.attach_us(&sample,20); //20us 50KHz sampling rate
-            for (int i = 0; i < SAMPLES; i += 2) {
-                while (trig==0){}
-                trig=0;
-                Input[i] = myADC.read() - 0.5f; //Real part NB removing DC offset
-                Input[i + 1] = 0;               //Imaginary Part set to zero
-                }
-            timer.detach();
+    
+    for(int k = 0; k < 10; k++) {
+        timer.attach_us(&sample,100); //100us 10KHz sampling rate
+        for (int i = 0; i < SAMPLES; i += 2) {
+            while (trig==0){}
+            trig=0;
+            Input[i] = loudness.read() ;
+            Input[i + 1] = 0;               //Imaginary Part set to zero
+        }
+        timer.detach();
+    
         // Init the Complex FFT module, intFlag = 0, doBitReverse = 1
         //NB using predefined arm_cfft_sR_f32_lenXXX, in this case XXX is 256
         arm_cfft_f32(&arm_cfft_sR_f32_len256, Input, 0, 1);
-
+ 
         // Complex Magniture Module put results into Output(Half size of the Input)
         arm_cmplx_mag_f32(Input, Output, FFT_SIZE);
         
         //Calculates maxValue and returns corresponding value
         arm_max_f32(Output, FFT_SIZE, &maxValue, &maxIndex);
-
-        if (once==0){
-            pc.printf("Maximum is %f\r\n",maxValue);
-            once = 1;
+ 
+        maxv = Output[1];
+        int index = 0;
+        for (int i= 1; i<FFT_SIZE ; i++){
+            if (maxv < Output[i]){
+                maxv = Output[i];
+                index = i;
             }
-       
-        //maxValue /= 100.0f;
+        }
+        pc.printf("Maximum is %f\r\n, index %d\r\n",maxv, index);
+        
+        /*for (int i = 1 ; i<FFT_SIZE ; i++){  
+            pc.printf("output[%d] = %f\r\n", i, (Output[i]/maxValue));
+        }*/
         
-        myDAC=1.0f;     //SYNC Pulse to DAC Output
-        wait_us(20);    //Used on Oscilliscope set trigger level to the highest
-        myDAC=0.0f;     //point on this pulse (all FFT data will be scaled
-                        //90% of Max Value
+        for(int i = 0; i < 4; i++) {
+            tab_temp[k][i] = Output[i+11]/maxv;
+            pc.printf("tab_temp[%d][%i] = %f\r\n", k, i, tab_temp[k][i]);
+        }
+    }
+    
+    float32_t tmp_sum = 0;
+    for(int i = 0; i < 4; i++) {
         
-        for (int i=0; i<FFT_SIZE/2 ; i++){
-            myDAC=(Output[i]/maxValue)*0.9f; // Scale to Max Value and scale to 90%
-            wait_us(10); //Each pulse of 10us is 50KHz/256 = 195Hz resolution
-            }
-        myDAC=0.0f;
+        tmp_sum += tab_temp[0][i] + tab_temp[1][i] + tab_temp[2][i] + tab_temp[3][i] + 
+                   tab_temp[4][i] + tab_temp[5][i] + tab_temp[6][i] + tab_temp[7][i];
+        moyenne[i] = tmp_sum/8;
+        tmp_sum = 0;
+    }
+    
+    for(int i = 0; i < 4; i++) {
+        pc.printf("moyenne[%d] = %f\r\n", i, moyenne[i]);
+    }
+    
+    bool queenIsPresent = false;
+    for(int i = 0; i < 4; i++) {
+        if(moyenne[i] > SEUIL) {
+            queenIsPresent = true;
         }
-}
\ No newline at end of file
+    }
+}