uses armmath has two swithcable filters
Dependencies: FastAnalogIn mbed-dsp mbed
Revision 1:d1b05f475265, committed 2014-04-26
- Comitter:
- Clocktop
- Date:
- Sat Apr 26 21:48:04 2014 +0000
- Parent:
- 0:888888c9f065
- Commit message:
- Final working version
Changed in this revision
diff -r 888888c9f065 -r d1b05f475265 LowPass.c --- a/LowPass.c Wed Apr 23 18:23:58 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -/* LowPass.c */ -/* FIR filter coefficients */ -/* exported by MATLAB using FIR_dump2c */ -/* Michael G. Morrow - 2000, 2003 */ - - -#include "LowPass.h" - -float B[N+1] = { -1, -0, -0, -0, -.3 -}; \ No newline at end of file
diff -r 888888c9f065 -r d1b05f475265 LowPass.h --- a/LowPass.h Wed Apr 23 18:23:58 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -/* LowPass.h */ -/* FIR filter coefficients */ -/* exported by MATLAB using FIR_dump2c */ -/* Michael G. Morrow - 2000, 2003, 2004 */ - - -#define N 5 - -extern float B[];
diff -r 888888c9f065 -r d1b05f475265 main.cpp --- a/main.cpp Wed Apr 23 18:23:58 2014 +0000 +++ b/main.cpp Sat Apr 26 21:48:04 2014 +0000 @@ -2,7 +2,9 @@ #include <ctype.h> #include "FastAnalogIn.h" #include "arm_math.h" -#define NUM_TAPS 51 +#define NUM_TAPS 29 +#define block_size 32 + FastAnalogIn audioin(PTC2); //Analog in AnalogOut audioout(PTE30); //Analog out Ticker sampleing; //ticker is a class that automatically interrupts the processor on an interval @@ -10,103 +12,90 @@ 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; +int numblocks; +int buffer_ready = 0; +int samplecounter = 0; +int i,k; + +const int num_samples = 512; //512 max! kmc +const int num_buffers = 3; + 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) -** ------------------------------------------------------------------- */ +static float32_t firStateF32_L[block_size + NUM_TAPS - 1]; +static float32_t firStateF32_H[block_size + NUM_TAPS - 1]; +float32_t *inputF32, *outputF32, *processingF32; - 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_H[NUM_TAPS] = { + .0000574316875202937f, 0.000259433064931947f, 0.000437449344311845f, -0.000166641902553674f, + -0.00238511586135069f, -0.00525856044788883f, -0.00452383563647740f, 0.00502173506378964f, + 0.0222334773814194f, 0.0330963994141118f, 0.0145265901181610f, -0.0486381603203359f, + -0.144905311828843f, -0.234315167629590f, 0.729118629590472f,-0.234315167629590f,-0.144905311828843f, + -0.0486381603203359f, 0.0145265901181610f, 0.0330963994141118f, 0.0222334773814194f, + 0.00502173506378964f, -0.00452383563647740f, -0.00525856044788883f, -0.00238511586135069f, + -0.000166641902553674f, 0.000437449344311845f, 0.000259433064931947f, .0000574316875202937 }; 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 - + 0.000794455095852286f, -0.00145678301449100f, -0.00401267242044256f, -0.00774677450389924f, + -0.0115271352127628f, -0.0134875234349854f, -0.0112906732230415f, -0.00273103203148382f, + 0.0134991003647579f, 0.0371289350679086f, 0.0659791021599723f, 0.0961528037475044f, + 0.122766312185753f, 0.141063665542080f, 0.147583022099822f, 0.141063665542080f, 0.122766312185753f, + 0.0961528037475044f, 0.0659791021599723f, 0.0371289350679086f, 0.0134991003647579f, + -0.00273103203148382f, -0.0112906732230415f, -0.0134875234349854f, -0.0115271352127628f, + -0.00774677450389924f, -0.00401267242044256f, -0.00145678301449100f, 0.000794455095852286 }; - - -int i,k; - -void samplingCallback() -{ - //e = not(e); +void samplingCallback(){ buffer[fill_index][samplecounter] = audioin; audioout = buffer[dump_index][samplecounter]; - samplecounter++; - if(samplecounter >= 511) { + if(++samplecounter>=num_samples){ samplecounter = 0; - //l= not(l); ready_index = fill_index; - if(++fill_index >= num_buffers) { + if(++fill_index >= num_buffers){ fill_index = 0; } - if(++dump_index >= num_buffers) { + if(++dump_index >= num_buffers){ dump_index = 0; } + buffer_ready = 1; } - } -void getfilter(){ - if(state == 1){ - arm_fir_f32(&H, inputF32 , outputF32, 1); - set1 = 1; - set2 = 0; + +void getfilter() +{ + if(state == 1) { + for(i=0; i<numblocks; i++) { + arm_fir_f32(&H, inputF32+(i*block_size) , outputF32+(i*block_size), block_size); } - else{ - arm_fir_f32(&L, inputF32 , outputF32, 1); - set1 = 0; - set2 = 1; + set1 = 1; + set2 = 0; + } else { + for(i=0; i<numblocks; i++) { + arm_fir_f32(&L, inputF32+(i*block_size) , outputF32+(i*block_size), block_size); } -} - + set1 = 0; + set2 = 1; + } + buffer_ready = 0; +} -void samplingBegin() -{ - //ticker_timing = 100000/sample_rate; +void samplingBegin(){ + numblocks = num_samples/block_size; samplecounter = 0; sampleing.attach_us(&samplingCallback, 125); //sample rate 40k 25, 45k 22, 50k,20 } -bool samplingisdone() -{ - //c = not(c); - return samplecounter >= 510; +int samplingisdone(){ + return buffer_ready; } -//////////////////////////////////////////////////////////////////// -// Buffer Zeroing -//////////////////////////////////////////////////////////////////// -void zerobuffers() -{ +void zerobuffers(){ for(i=0; i<=num_buffers; i++) { for(k=0; k<num_samples; k++) { buffer[i][k] = 0.0; @@ -114,26 +103,18 @@ } } - -int main() -{ - - - - //getfilter(); +int main(){ + arm_fir_init_f32(&H, NUM_TAPS, (float32_t *)&firCoeffs32_H[0], &firStateF32_H[0], block_size); + arm_fir_init_f32(&L, NUM_TAPS, (float32_t *)&firCoeffs32_L[0], &firStateF32_L[0], block_size); + ///zerobuffers(); 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()) { + while(1){ + if(samplingisdone()==1){ l = 1; inputF32 = &buffer[ready_index][0]; outputF32 = &buffer[dump_index][0]; getfilter(); - - l=0 ; + l = 0; } } -} +} \ No newline at end of file