Tau ReSpeaker Setup V01

Dependencies:   MbedJSONValue mbed

Fork of TAU_ReSpeaker_DSP_Test by Yossi_Students

Revision:
8:8a3e6241c104
Parent:
7:25c81cb23e42
Child:
10:273127efdc6e
--- a/filters.h	Mon Aug 06 12:30:23 2018 +0000
+++ b/filters.h	Wed Aug 22 12:57:13 2018 +0000
@@ -119,7 +119,7 @@
         ADCFloatFiltered = CurrInput[ii];
     }
 
-    ADCFloatFiltered = ADCFloatFiltered * GscaleHP;
+    ADCFloatFiltered = ADCFloatFiltered * GscaleHP * signalGain;
 
     ////////////////////////////////
     // Apply Saturation to Output //
@@ -188,7 +188,7 @@
         ADCFloatFiltered = CurrInput[ii];
     }
 
-    ADCFloatFiltered = ADCFloatFiltered * GscaleHP;
+    ADCFloatFiltered = ADCFloatFiltered * GscaleHP * signalGain;
 
     ///////////////////////////////////////////////
     // Event detection                           //
@@ -210,9 +210,9 @@
             *(__IO uint32_t *) Dac_Reg = ADCValueOut;
             //*(__IO uint32_t *) Dac_Reg = 0; // test triggered mode
             // delay for set time
-            wait_us(trigPause);
+            wait_ms(trigPause);
         }
-    } else if (ADCFloatFiltered >= trigTresh) {
+    } else if (ADCFloatFiltered >= trigTresh) { 
         trigFlag=1;
     }
     ////////////////////////////////
@@ -311,7 +311,7 @@
             *(__IO uint32_t *) Dac_Reg = ADCValueOut;
             //*(__IO uint32_t *) Dac_Reg = 0; // test triggered mode
             // delay for set time
-            wait_us(trigPause);
+            wait_ms(trigPause);
             
             // change gain settings
             GainVectorIndex++;
@@ -337,4 +337,129 @@
     // HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValueOut);
     *(__IO uint32_t *) Dac_Reg = ADCValueOut;
     //wait_us(1);
+} // end GainsTrig
+
+// highpass filter + trigger mode + Delays vector
+inline void DelaysTrig(void)  // - mid work feature. (not working yet)
+{
+    ///////////////////////////////
+    //    Gains variables:       //
+    ///////////////////////////////
+    #define DELAY_VECTOR_SIZE 5
+    uint32_t DelayVector[] = {100 , 200 , 300 , 400 , 500}; // millis
+    static uint16_t DelayVectorIndex = 0;
+    static uint32_t BufferIndex = 0;
+    
+//uint32_t bufferSizeSet = 5000;
+//uint32_t preBufferSizeSet = 1000;
+//uint32_t bufferCountDown = bufferSizeSet - preBufferSizeSet;
+//float bufferADC[bufferSizeSet] = {0};
+    ///////////////////////////////
+    //    filter variables:      //
+    ///////////////////////////////
+    // filter coeficients best performance if defined in loop
+    float SOSMatHP[2][6] = { // 25khz cutoff at 920 kHz sample rate // closer to 30kHz when io toggle switched off.
+        1.000000,  -2.000000,  1.000000,  1.000000,  -1.706510,  0.731145,
+        1.000000,  -2.000000,  1.000000,  1.000000,  -1.852377,  0.879117
+    };
+    float GscaleHP = 0.801724;
+
+    // num sections
+    int NumSectionsHP = sizeof(SOSMatHP)/sizeof(float)/6;
+
+    float ADCFloat;
+    float ADCFloatFiltered;
+    uint16_t ADCValueOut;
+
+    // filter stages variables
+    static float CurrInput [MAX_SECTION_NUMBER+1];
+    static float LastInput [MAX_SECTION_NUMBER+1];
+    static float LLastInput [MAX_SECTION_NUMBER+1];
+
+    // trigger variables
+    static bool trigFlag=0; // flag to indicate trigger event
+
+    ////////////////////
+    // Read ADC input //
+    ////////////////////
+    ADCFloat=((uint16_t)(hadc1.Instance->DR) * ADC2Float)-1.0f;
+    
+    //////////////////////////////////////////////////////
+    // Apply Filter to input                            //
+    //////////////////////////////////////////////////////
+
+    LLastInput[0] = LastInput[0];
+    LastInput[0] = CurrInput[0];
+    CurrInput[0] = ADCFloat;
+    for (int ii=1; ii <= NumSectionsHP; ii++) {
+        LLastInput[ii] = LastInput[ii];
+        LastInput[ii] = CurrInput[ii];
+        CurrInput[ii] = SOSMatHP[ii-1][0]*CurrInput[ii-1] + SOSMatHP[ii-1][1]*LastInput[ii-1] +
+                        SOSMatHP[ii-1][2]*LLastInput[ii-1] -
+                        SOSMatHP[ii-1][4]*LastInput[ii] - SOSMatHP[ii-1][5]*LLastInput[ii];
+        ADCFloatFiltered = CurrInput[ii];
+    }
+
+    ADCFloatFiltered = ADCFloatFiltered * GscaleHP;
+
+    ////////////////////////////////
+    // Fill in buffer             //
+    ////////////////////////////////
+    bufferADC[BufferIndex] = ADCFloatFiltered;
+    BufferIndex++;
+    BufferIndex = BufferIndex % bufferSize;
+    ///////////////////////////////////////////////
+    // Event detection                           //
+    ///////////////////////////////////////////////
+    if (trigFlag) { // event already detected
+        bufferCountDown--; // count down
+        if (bufferCountDown == 0) { // pulse pass run out, perform delay and reset variables
+            bufferCountDown = bufferSizeSet - preBufferSizeSet; //reset counter for next iteration
+            trigFlag=0;
+            // reset filter
+            for (int ii=1; ii <= NumSectionsHP; ii++) {
+                LLastInput[ii] = 0;
+                LastInput[ii] = 0;
+                CurrInput[ii] = 0;
+                ADCFloatFiltered = 0;
+            }
+            
+            // generate delay
+            // delay for set time
+            wait_ms(DelayVector[DelayVectorIndex]);
+            // change delay settings
+            DelayVectorIndex++;
+            DelayVectorIndex = DelayVectorIndex % DELAY_VECTOR_SIZE;
+            
+            
+            // play out buffer
+            for (int ii=0 ; ii<bufferSize ; ii++) {
+                
+                ADCFloatFiltered = bufferADC[BufferIndex];
+                BufferIndex++;
+                BufferIndex = BufferIndex % bufferSize;
+                ////////////////////////////////
+                // Apply Saturation to Output //
+                ////////////////////////////////
+
+                if (ADCFloatFiltered < -1.0f) {
+                    ADCValueOut=0; // Min value
+                } else if (ADCFloatFiltered > 1.0f) {
+                    ADCValueOut=0xFFF; // Max value
+                } else {
+                    ADCValueOut=(uint16_t)((ADCFloatFiltered +1.0f) * Float2ADC);
+                }
+
+                // Output value using DAC
+                // HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, ADCValueOut);
+                *(__IO uint32_t *) Dac_Reg = ADCValueOut;
+                //wait_us(1);
+            }
+
+            
+        }
+    } else if (ADCFloatFiltered >= trigTresh) {
+        trigFlag=1;
+    }
+    
 } // end GainsTrig
\ No newline at end of file