uses armmath has two swithcable filters
Dependencies: FastAnalogIn mbed-dsp mbed
Diff: main.cpp
- Revision:
- 0:888888c9f065
- Child:
- 1:d1b05f475265
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Apr 23 18:23:58 2014 +0000 @@ -0,0 +1,139 @@ +#include "mbed.h" +#include <ctype.h> +#include "FastAnalogIn.h" +#include "arm_math.h" +#define NUM_TAPS 51 +FastAnalogIn audioin(PTC2); //Analog in +AnalogOut audioout(PTE30); //Analog out +Ticker sampleing; //ticker is a class that automatically interrupts the processor on an interval +DigitalOut l(PTA1); +DigitalOut set1(LED1); +DigitalOut set2(LED2); +DigitalIn state(PTA2); +const int num_samples = 512; //512 max! kmc +const int num_buffers = 3; +int samplecounter = 0; +int fill_index = 0; +int dump_index = 1; +int ready_index =2; +float32_t buffer[num_buffers][num_samples]; +arm_fir_instance_f32 H,L; + +static float32_t firStateF32[1 + NUM_TAPS - 1]; +float32_t *inputF32, *outputF32; +/* ---------------------------------------------------------------------- +** FIR Coefficients buffer generated using fir1() MATLAB function. +** fir1(28, 6/24) +** ------------------------------------------------------------------- */ + + const float32_t firCoeffs32_H[NUM_TAPS] = { +-0.0000080878311f, 0.00003923656432f, -0.000061039638221f, -0.00003695668356985f, 0.000275668560418687f, -0.000327280297262495f, +-0.000260129744371785f, 0.00116157181055702f, -0.000923953609936865f, -0.00133518222306195f, 0.00349543690781629f, +-0.00150896657945131f, -0.00479968490025620f, 0.00809627752995577f, -0.000496933275084586f, -0.0132944668070009f, 0.0150641815132129f, +0.00598396310662075f, -0.0310875990306542f, 0.0231130218762105f, 0.0273325707514090f, -0.0702585024688767f, 0.0297114875214292f, +0.115565099312860f, -0.288244540649423f, 0.365609759681161f, -0.288244540649423f, 0.115565099312860f, 0.0297114875214292f, +-0.0702585024688767f, 0.0273325707514090f, 0.0231130218762105f, -0.0310875990306542f, 0.00598396310662075f, 0.0150641815132129f, +-0.0132944668070009f, -0.000496933275084586f, 0.00809627752995577f, -0.00479968490025620f, -0.00150896657945131f, 0.00349543690781629f, + -0.00133518222306195f, -0.000923953609936865f, 0.00116157181055702f, -0.000260129744371785f, -0.000327280297262495f, + 0.000275668560418687f, -0.0000369566835698504f, -0.00006103963822183f, 0.0000392365643258656f, -0.00000808783111344f +}; + +const float32_t firCoeffs32_L[NUM_TAPS] = { +-0.000471676005294967f, -0.000869941110733902f, -0.00136615751059969f, -0.00192713332921617f, -0.00249683488334566f, +-0.00299588766189560f, -0.00332345379822847f, -0.00336168772922327f, -0.00298271993668161f, -0.00205784383868562f, +-0.000468311685565155f, 0.00188308797184820f, 0.00506066745872936f, 0.00908576885361775f, 0.0139290391946988f, 0.0195059759543063f, +0.0256764342311968f, 0.0322484706002718f, 0.0389865257052826f, 0.0456235534497802f, 0.0518763277757713f, 0.0574628369693583f, +0.0621204442047146f, 0.0656233774164507f, 0.0677981261749183f, 0.0685354696461944f, 0.0677981261749183f, 0.0656233774164507f, +0.0621204442047146f, 0.0574628369693583f, 0.0518763277757713f, 0.0456235534497802f, 0.0389865257052826f, 0.0322484706002718f, +0.0256764342311968f, 0.0195059759543063f, 0.0139290391946988f, 0.00908576885361775f, 0.00506066745872936f, 0.00188308797184820f, +-0.000468311685565155f, -0.00205784383868562f, -0.00298271993668161f, -0.00336168772922327f, -0.00332345379822847f, +-0.00299588766189560f, -0.00249683488334566f, -0.00192713332921617f, -0.00136615751059969f, -0.00086994111073390f, +-0.000471676005294967f + +}; + + + +int i,k; + +void samplingCallback() +{ + //e = not(e); + buffer[fill_index][samplecounter] = audioin; + audioout = buffer[dump_index][samplecounter]; + samplecounter++; + if(samplecounter >= 511) { + samplecounter = 0; + //l= not(l); + ready_index = fill_index; + if(++fill_index >= num_buffers) { + fill_index = 0; + } + if(++dump_index >= num_buffers) { + dump_index = 0; + } + } + +} +void getfilter(){ + if(state == 1){ + arm_fir_f32(&H, inputF32 , outputF32, 1); + set1 = 1; + set2 = 0; + } + else{ + arm_fir_f32(&L, inputF32 , outputF32, 1); + set1 = 0; + set2 = 1; + } +} + + +void samplingBegin() +{ + //ticker_timing = 100000/sample_rate; + samplecounter = 0; + sampleing.attach_us(&samplingCallback, 125); //sample rate 40k 25, 45k 22, 50k,20 +} + +bool samplingisdone() +{ + //c = not(c); + return samplecounter >= 510; +} + +//////////////////////////////////////////////////////////////////// +// Buffer Zeroing +//////////////////////////////////////////////////////////////////// +void zerobuffers() +{ + for(i=0; i<=num_buffers; i++) { + for(k=0; k<num_samples; k++) { + buffer[i][k] = 0.0; + } + } +} + + +int main() +{ + + + + //getfilter(); + samplingBegin(); + //button.rise(&getfilter); + //getfilter(); + arm_fir_init_f32(&H, NUM_TAPS, (float32_t *)&firCoeffs32_H[0], &firStateF32[0], 1); + arm_fir_init_f32(&L, NUM_TAPS, (float32_t *)&firCoeffs32_L[0], &firStateF32[0], 1); + while(1) { + if(samplingisdone()) { + l = 1; + inputF32 = &buffer[ready_index][0]; + outputF32 = &buffer[dump_index][0]; + getfilter(); + + l=0 ; + } + } +}