cancel
Showing results for 
Search instead for 
Did you mean: 

Filtering ADC output with FIR ?

antonius
Senior

Dear Members,

Dear Members,

I got a FIR filter coefficient from matlab :

/* General type conversion for MATLAB generated C-code  */
#include "tmwtypes.h"
/* 
 * Expected path to tmwtypes.h 
 * C:\Program Files\MATLAB\R2018b\extern\include\tmwtypes.h 
 */
const int BL = 252;
const real64_T B[252] = {
  6.509489485376e-06,8.270468373576e-06,7.064827126383e-06,2.035036566381e-06,
  -6.199144943968e-06,-1.530430577156e-05,-2.170369436731e-05,-2.174427733836e-05,
  -1.324304616624e-05,3.149084058685e-06,2.339035331051e-05,4.073809420116e-05,
  4.771598033019e-05, 3.88749823869e-05,1.343292994109e-05,-2.328007613973e-05,
  -6.040768693408e-05,-8.440733005252e-05,-8.353908676759e-05,-5.263877917656e-05,
  3.573508475597e-06,7.018851252303e-05,0.0001255118884919, 0.000147627918814,
  0.0001222953867957,4.952226525078e-05,-5.382154786297e-05,-0.0001570052393388,
  -0.0002239403811517,-0.0002250586264019,-0.000148863525125,-8.971673748049e-06,
  0.0001565921278752,0.0002948202746476,0.0003538868289885,0.0003013760703547,
  0.0001384716510019,-9.546804025625e-05,-0.0003309454428265,-0.0004879684402625,
  -0.0005020986841372,-0.000348744552471,-5.704216636268e-05,0.0002928736706498,
  0.0005904665632932,0.0007285044131239,0.0006399792716264, 0.000325987210871,
  -0.0001361070302262,-0.0006094448304161, -0.00093670421378,-0.0009907704066022,
  -0.0007211998800203,-0.0001802110032807,0.0004829797264755, 0.001061180750259,
   0.001352190579092, 0.001227129264082,0.0006816976957453,-0.0001487550665314,
    -0.0010193333099,-0.001645588235867,-0.001794153121802,-0.001365824774668,
  -0.0004425130028242,0.0007213421897964, 0.001765671644857, 0.002333570437864,
   0.002189935617387, 0.001311451978109,-8.479801395671e-05,-0.001589583874628,
  -0.002718070358089,-0.003062598285136,-0.002434585940507,-0.0009475929608268,
  0.0009914380896389, 0.002788470577278,  0.00384022894245,  0.00373166167708,
   0.002389832564894,0.0001396368407159,-0.002365947536661,-0.004329634042112,
  -0.005057755292929,-0.004196967869377,-0.001881265667525, 0.001266880368926,
   0.004294843744221, 0.006200435613341, 0.006254439807294,  0.00426393327023,
  0.0006854015379761,-0.003462805399825, -0.00687871151427,-0.008373150891941,
  -0.007267200985151,-0.003662419169445, 0.001515171799773, 0.006732980928894,
    0.01028679430541,  0.01083449837216, 0.007867746323399, 0.001965125316513,
  -0.005275587183862, -0.01163742062178, -0.01492483266663, -0.01366929215605,
  -0.007686544451677, 0.001703476697994,  0.01189919209156,  0.01966422424027,
    0.02205153049026,  0.01736115274647, 0.005854450632269, -0.01000807742441,
    -0.0259235455839, -0.03665900395193, -0.03732268145457, -0.02468377261547,
   0.001805107035604,  0.03957199855514,  0.08322774168024,   0.1255731326557,
      0.159094765642,   0.1776005452962,   0.1776005452962,    0.159094765642,
     0.1255731326557,  0.08322774168024,  0.03957199855514, 0.001805107035604,
   -0.02468377261547, -0.03732268145457, -0.03665900395193,  -0.0259235455839,
   -0.01000807742441, 0.005854450632269,  0.01736115274647,  0.02205153049026,
    0.01966422424027,  0.01189919209156, 0.001703476697994,-0.007686544451677,
   -0.01366929215605, -0.01492483266663, -0.01163742062178,-0.005275587183862,
   0.001965125316513, 0.007867746323399,  0.01083449837216,  0.01028679430541,
   0.006732980928894, 0.001515171799773,-0.003662419169445,-0.007267200985151,
  -0.008373150891941, -0.00687871151427,-0.003462805399825,0.0006854015379761,
    0.00426393327023, 0.006254439807294, 0.006200435613341, 0.004294843744221,
   0.001266880368926,-0.001881265667525,-0.004196967869377,-0.005057755292929,
  -0.004329634042112,-0.002365947536661,0.0001396368407159, 0.002389832564894,
    0.00373166167708,  0.00384022894245, 0.002788470577278,0.0009914380896389,
  -0.0009475929608268,-0.002434585940507,-0.003062598285136,-0.002718070358089,
  -0.001589583874628,-8.479801395671e-05, 0.001311451978109, 0.002189935617387,
   0.002333570437864, 0.001765671644857,0.0007213421897964,-0.0004425130028242,
  -0.001365824774668,-0.001794153121802,-0.001645588235867,  -0.0010193333099,
  -0.0001487550665314,0.0006816976957453, 0.001227129264082, 0.001352190579092,
   0.001061180750259,0.0004829797264755,-0.0001802110032807,-0.0007211998800203,
  -0.0009907704066022, -0.00093670421378,-0.0006094448304161,-0.0001361070302262,
   0.000325987210871,0.0006399792716264,0.0007285044131239,0.0005904665632932,
  0.0002928736706498,-5.704216636268e-05,-0.000348744552471,-0.0005020986841372,
  -0.0004879684402625,-0.0003309454428265,-9.546804025625e-05,0.0001384716510019,
  0.0003013760703547,0.0003538868289885,0.0002948202746476,0.0001565921278752,
  -8.971673748049e-06,-0.000148863525125,-0.0002250586264019,-0.0002239403811517,
  -0.0001570052393388,-5.382154786297e-05,4.952226525078e-05,0.0001222953867957,
   0.000147627918814,0.0001255118884919,7.018851252303e-05,3.573508475597e-06,
  -5.263877917656e-05,-8.353908676759e-05,-8.440733005252e-05,-6.040768693408e-05,
  -2.328007613973e-05,1.343292994109e-05, 3.88749823869e-05,4.771598033019e-05,
  4.073809420116e-05,2.339035331051e-05,3.149084058685e-06,-1.324304616624e-05,
  -2.174427733836e-05,-2.170369436731e-05,-1.530430577156e-05,-6.199144943968e-06,
  2.035036566381e-06,7.064827126383e-06,8.270468373576e-06,6.509489485376e-06
};

I use STM32L152...

HAL_ADC_Start_DMA (&hadc, (uint32_t *) adc_buf, 4); // data to be converted = 4											 
		
		HAL_Delay(10);
  printf("ADC value 3 %u \r\n",adc_val[3]);
  .
  .
  .

I want to filter adc_val[3] ...

How can I attach it to my ADC result ? and then print the result out to compare ?

Thanks

This discussion is locked. Please start a new topic to ask your question.
9 REPLIES 9
Ozone
Principal

There is a DSP library available, supposedly even included in Cube, which contains a FIR filter implementation and examples, amongst others.

As a hint, I would use float, not double.

this one X-CUBE-DSPDEMO ?

I guess so - I don't use Cube.

There had been, and are still SPL-based DSP libraries available for the "more matured" devices up to F3/F4.

I had been using these one's.

The DSP lib contains code for F0 and F3 as well, and fixed-point implementations.

I would suggest to use such one on a platformwithout FPU like the L15x, rather then float/double.

"..There had been, and are still SPL-based DSP libraries available for the "more matured" devices up to F3/F4.

I had been using these one's..."

What's the name ? where can I download it ? thanks

Here's the output from simulink, I haven't put on the chip yet,

I'm not sure yet.....

Output from ADC 180s, 3000mV max

0690X00000AQfmhQAD.jpg

Output from Simulink before filter

0690X00000AQfnGQAT.jpg

output after filter :

0690X00000AQfnaQAD.jpg

I want to filter a signal more than 4Hz, but pass 0- max 4Hz

Filter design :

0690X00000AQfnfQAD.jpg

How to get the C code for it ?

Thanks

In which directory I can see the example project ? thanks

In my copy, DSP examples from ARM are in ..\STM32F4xx_DSP_StdPeriph_Lib_V1.8.0\Libraries\CMSIS\DSP_Lib\Examples

Including one FIR example.

I think one can get the DSP lib from ARM as well. Most of the code is based on core functionalities, i.e. not vendor-specific.