Tau ReSpeaker Setup V01
Dependencies: MbedJSONValue mbed
Fork of TAU_ReSpeaker_DSP_Test by
Diff: filters.h
- Revision:
- 4:59319802012b
- Parent:
- 3:48258b86e182
- Child:
- 5:ec6f2323a263
--- a/filters.h Mon Feb 19 11:50:39 2018 +0000 +++ b/filters.h Mon Feb 19 15:14:44 2018 +0000 @@ -9,35 +9,54 @@ // filter variables: // /////////////////////////////// -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; +// filter coeficients, best performance to declare in function (constant values). +//float SOSMatHP[2][6] = { // 25khz cutoff at 920 kHz sample rate // 50kHz cutoff at 1.4 Mhz sample rate +// 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; // second-order sections filter variables - upto 8 sections #define MAX_SECTION_NUMBER 8 -int NumSectionsHP = sizeof(SOSMatHP)/sizeof(float)/6; // convertions -float ADC2Float=(2.0f/4095.0f); //ADCvalue*(2/0xFFF)-1.0f // 12 bits range -float Float2ADC=(4095.0f/2.0f); //(ADCvalue+1.0f)*(0xFFF/2) // 12 bits range +#define ADC2Float (2.0f/4095.0f) // 0.0004884f// +#define Float2ADC (4095.0f/2.0f) // 2047.5f // +//float ADC2Float=(2.0f/4095.0f); //ADCvalue*(2/0xFFF)-1.0f // 12 bits range +//float Float2ADC=(4095.0f/2.0f); //(ADCvalue+1.0f)*(0xFFF/2) // 12 bits range /////////////////////////////// // available filters: // /////////////////////////////// -// no filter function (output equals input) -inline void no_filter(void); -// high pass filter -inline void highpass_filter(void); +// off mode, output vdd/2 +inline void offMode(void); +// passthrough function (output equals input) +inline void passthrough(void); +// highpass filter +inline void highpass(void); +// highpass filter + trigger mode +inline void highpassTrig(void); /////////////////////////////// // Filter Functions: // /////////////////////////////// -// no filter function (output equals input) -inline void no_filter(void) + +// off mode, output vdd/2 +inline void offMode(void){ + uint16_t ADCValueOut; + // set to vdd/2 + ADCValueOut=(uint16_t)((0.0f +1.0f) * Float2ADC); + // Output value using DAC + *(__IO uint32_t *) Dac_Reg = ADCValueOut; +}// end off mode + +// passthrough function (output equals input) +inline void passthrough(void) { uint32_t ADCValue; @@ -46,12 +65,25 @@ // Output value using DAC *(__IO uint32_t *) Dac_Reg = ADCValue; -} // end no filter +} // end passthrough // high pass filter -inline void highpass_filter(void) +inline void highpass(void) { + /////////////////////////////// + // 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; @@ -60,7 +92,10 @@ static float CurrInput [MAX_SECTION_NUMBER+1]; static float LastInput [MAX_SECTION_NUMBER+1]; static float LLastInput [MAX_SECTION_NUMBER+1]; - // Read ADC input + + //////////////////// + // Read ADC input // + //////////////////// ADCFloat=((uint16_t)(hadc1.Instance->DR) * ADC2Float)-1.0f; ////////////////////////////////////////////////////// @@ -96,6 +131,70 @@ // 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); + +} // end high pass filter + +// highpass filter + trigger mode +inline void highpassTrig(void){ + /////////////////////////////// + // 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]; + + //////////////////// + // Read ADC input // + //////////////////// + ADCFloat=((uint16_t)(hadc1.Instance->DR) * ADC2Float)-1.0f; -} \ No newline at end of file + ////////////////////////////////////////////////////// + // 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; + + //////////////////////////// + // Apply Filter 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/2; + //wait_us(1); +} // end high pass filter + trigger mode \ No newline at end of file