Bandpass filter

Hello
How to implement this program to stm32f103

#define NZEROS 16
#define NPOLES 16
#define GAIN 2.733054109e+10

static float xv[NZEROS+1], yv[NPOLES+1];

static void filterloop()
{ for (;:wink:
{ xv[0] = xv[1]; xv[1] = xv[2]; xv[2] = xv[3]; xv[3] = xv[4]; xv[4] = xv[5]; xv[5] = xv[6]; xv[6] = xv[7]; xv[7] = xv[8]; xv[8] = xv[9]; xv[9] = xv[10]; xv[10] = xv[11]; xv[11] = xv[12]; xv[12] = xv[13]; xv[13] = xv[14]; xv[14] = xv[15]; xv[15] = xv[16];
xv[16] = next input value / GAIN;
yv[0] = yv[1]; yv[1] = yv[2]; yv[2] = yv[3]; yv[3] = yv[4]; yv[4] = yv[5]; yv[5] = yv[6]; yv[6] = yv[7]; yv[7] = yv[8]; yv[8] = yv[9]; yv[9] = yv[10]; yv[10] = yv[11]; yv[11] = yv[12]; yv[12] = yv[13]; yv[13] = yv[14]; yv[14] = yv[15]; yv[15] = yv[16];
yv[16] = (xv[0] + xv[16]) - 8 * (xv[2] + xv[14]) + 28 * (xv[4] + xv[12])

  • 56 * (xv[6] + xv[10]) + 70 * xv[8]
  • ( -0.9999996903 * yv[0]) + ( 15.0671415160 * yv[1])
  • (-107.3207279400 * yv[2]) + (479.5899447000 * yv[3])
  • (-1504.6930028000 * yv[4]) + (3514.0764694000 * yv[5])
  • (-6318.5794832000 * yv[6]) + (8922.2254164000 * yv[7])
  • (-9998.7316198000 * yv[8]) + (8922.2257618000 * yv[9])
  • (-6318.5799724000 * yv[10]) + (3514.0768775000 * yv[11])
  • (-1504.6932358000 * yv[12]) + (479.5900375300 * yv[13])
  • (-107.3207528700 * yv[14]) + ( 15.0671455990 * yv[15]);
    next output value = yv[16];
    }
    }

By now you should know How to post code properly. Read it. Do it.
It is also a bit of a mystery how you could think that a question about bandpass filter code belonged in the Website and Forum group.

Pete

static void filterloop()
  { for (;;)
      { xv[0] = xv[1]; xv[1] = xv[2]; xv[2] = xv[3]; xv[3] = xv[4]; xv[4] = xv[5]; xv[5] = xv[6]; xv[6] = xv[7]; xv[7] = xv[8]; xv[8] = xv[9]; xv[9] = xv[10]; xv[10] = xv[11]; xv[11] = xv[12]; xv[12] = xv[13]; xv[13] = xv[14]; xv[14] = xv[15]; xv[15] = xv[16];
        xv[16] = next input value / GAIN;
        yv[0] = yv[1]; yv[1] = yv[2]; yv[2] = yv[3]; yv[3] = yv[4]; yv[4] = yv[5]; yv[5] = yv[6]; yv[6] = yv[7]; yv[7] = yv[8]; yv[8] = yv[9]; yv[9] = yv[10]; yv[10] = yv[11]; yv[11] = yv[12]; yv[12] = yv[13]; yv[13] = yv[14]; yv[14] = yv[15]; yv[15] = yv[16];

That is the stupidest looking mess I've seen today.

You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?
You obviously know about for loops, so why the hell aren't you using them?

ted:
Hello
How to implement this program to stm32f103

#define NZEROS 16
#define NPOLES 16
#define GAIN 2.733054109e+10

static float xv[NZEROS+1], yv[NPOLES+1];

static void filterloop()
{ for (;:wink:
{ xv[0] = xv[1]; xv[1] = xv[2]; xv[2] = xv[3]; xv[3] = xv[4]; xv[4] = xv[5]; xv[5] = xv[6]; xv[6] = xv[7]; xv[7] = xv[8]; xv[8] = xv[9]; xv[9] = xv[10]; xv[10] = xv[11]; xv[11] = xv[12]; xv[12] = xv[13]; xv[13] = xv[14]; xv[14] = xv[15]; xv[15] = xv[16];
xv[16] = next input value / GAIN;
yv[0] = yv[1]; yv[1] = yv[2]; yv[2] = yv[3]; yv[3] = yv[4]; yv[4] = yv[5]; yv[5] = yv[6]; yv[6] = yv[7]; yv[7] = yv[8]; yv[8] = yv[9]; yv[9] = yv[10]; yv[10] = yv[11]; yv[11] = yv[12]; yv[12] = yv[13]; yv[13] = yv[14]; yv[14] = yv[15]; yv[15] = yv[16];
yv[16] = (xv[0] + xv[16]) - 8 * (xv[2] + xv[14]) + 28 * (xv[4] + xv[12])

  • 56 * (xv[6] + xv[10]) + 70 * xv[8]
  • ( -0.9999996903 * yv[0]) + ( 15.0671415160 * yv[1])
  • (-107.3207279400 * yv[2]) + (479.5899447000 * yv[3])
  • (-1504.6930028000 * yv[4]) + (3514.0764694000 * yv[5])
  • (-6318.5794832000 * yv[6]) + (8922.2254164000 * yv[7])
  • (-9998.7316198000 * yv[8]) + (8922.2257618000 * yv[9])
  • (-6318.5799724000 * yv[10]) + (3514.0768775000 * yv[11])
  • (-1504.6932358000 * yv[12]) + (479.5900375300 * yv[13])
  • (-107.3207528700 * yv[14]) + ( 15.0671455990 * yv[15]);
    next output value = yv[16];
    }
    }

couple of things:
you may want to change "#define GAIN 2.733054109e+10" to "const double GAIN 2.733054109e+10"
//double - >8 byte -> 2.3E-308 to 1.7E+308 ->15 decimal places
//define is usually 2 bytes long max value of 65 535 (unsigned)

secondly, I'm assuming that "next input value" will be be likely a analog read.

so ignoring PaulS not so helpful comment, you can implement this code as follows for in arduino sketch:


void setup(){

// initailise you inputs/ serial comms etc
}

void loop(){

//put the content of the for loop here. replace "next input value" with analogRead(1) for example if you a reading from A1 (assuming arduino UNO here)

}

Also may be sensible to put all the decimal constants you are using as const double global variables (just in case the complier does something funny with them)

sherzaad:
you may want to define "#define GAIN 2.733054109e+10" as follows:

#define GAIN 2.733054109e+10UL //unsigned long. define generally holds a int value. GAIN is obviously is greater than the largest value an int can hold 65 535!

GAIN is also larger than can be held in a 32 bit UL !!!

gfvalvo:
GAIN is also larger than can be held in a 32 bit UL !!!

you right! I realised that and was changing my post to use double as you put that commenct! :slight_smile:

I have error A and warrning B

A. error: #137: expression must be a modifiable lvalue

" next output value = yv16];" - what I should put in this line ?
I put 1
1 = yv[16]; _ I have error A

B, warning: #177-D: function "filterloop" was declared but never referenced

/* Infinite loop /
/
USER CODE BEGIN WHILE /
while (1)
{
/
USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}
/* USER CODE END 3 */

What I should put for loop code ?

sherzaad:
couple of things:
you may want to change "#define GAIN 2.733054109e+10" to "const double GAIN 2.733054109e+10"
//double - >8 byte -> 2.3E-308 to 1.7E+308 ->15 decimal places
//define is usually 2 bytes long max value of 65 535 (unsigned)

secondly, I'm assuming that "next input value" will be be likely a analog read.

so ignoring PaulS not so helpful comment, you can implement this code as follows for in arduino sketch:


void setup(){

// initailise you inputs/ serial comms etc
}

void loop(){

//put the content of the for loop here. replace "next input value" with analogRead(1) for example if you a reading from A1 (assuming arduino UNO here)

}

Also may be sensible to put all the decimal constants you are using as const double global variables (just in case the complier does something funny with them)

put the content of the for loop here - This is my problem - how to create it ?

Ted, sorry for the stupid question, but have you used the arduino IDE before?

Looking at your last post I've got a feeling that you know programming but not how to use the arduino IDE.

Also you should look at the examples for how to used the IOs on your arduino board. what IO are you referring to with "1"

in the code you posted the for loop is the infinite loop. did you try putting the contents of the for loop in to void loop routine(that's your infinite loop in the IDE)? (I assume yes and that's wen you got those errors)

one more thing, how do you want to view the output of your bandpass filter? Serial monitor? an output to another IO?

lastly to be clear, you ARE trying to code using Arduino IDE?

That is the stupidest looking mess I've seen today

The code was undoubtedly generated by another program.

There are filter design websites that do that automatically, so you don't have to have a clue what you are doing.

Thanks
sherzaad

I have 2 errors and 1 warrning.

../Src/main.c(64): error: #20: identifier "next" is undefined
next output value = yv[16];
../Src/main.c(64): error: #65: expected a ";"
next output value = yv[16];
../Src/main.c(49): warning: #177-D: function "filterloop" was declared but never referenced

ted:
Thanks
sherzaad

I have 2 errors and 1 warrning.

../Src/main.c(64): error: #20: identifier "next" is undefined
next output value = yv[16];
../Src/main.c(64): error: #65: expected a ";"
next output value = yv[16];
../Src/main.c(49): warning: #177-D: function "filterloop" was declared but never referenced

try changing "next output value" to next_output_value. It might compile but I don't expect the to see anything as this is a dummy input

have a look here to see how to use your board

Those errors are from Keil

From arduino I have this one:

C:\Users\OWNER\Documents\Arduino\sketch_sep26b\sketch_sep26b.ino:39:18: fatal error: main.h: No such file or directory

The filter code was gene was generated by
https://www-users.cs.york.ac.uk/~fisher/mkfilter/trad.html
Much easer to use than Matlab

This is my first project with programing, beginner with Arduino.

try changing "next output value" to next_output_value. It might compile but I don't expect the to see anything as this is a dummy input - not working.

I am studying your link

Much easer to use than Matlab

You don't seem to be having much luck with it so far.

First, you need to learn how a standard Arduino program is constructed. Start with the simple examples that come with the IDE.

hello Ted,

I had some time so I tried your code in Arduino IDE and it compiled!

#define NZEROS 16
#define NPOLES 16

const double GAIN = 2.733054109e+10;
const double A = -0.9999996903;
const double B = 15.0671415160;
const double C = -107.3207279400;
const double D = 479.5899447000;
const double E = -1504.6930028000;
const double F = 3514.0764694000;
const double G = -6318.5794832000;
const double H = 8922.2254164000;
const double I = -9998.7316198000;
const double J = 8922.2257618000;
const double K = -6318.5799724000;
const double L = 3514.0768775000;
const double M = -1504.6932358000;
const double N = 479.5900375300;
const double P = -107.3207528700;
const double Q = 15.0671455990;

double xv[NZEROS + 1], yv[NPOLES + 1];

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  double Next_Input_Value, Next_Output_Value;

  xv[0] = xv[1]; xv[1] = xv[2]; xv[2] = xv[3]; xv[3] = xv[4]; xv[4] = xv[5]; xv[5] = xv[6]; xv[6] = xv[7]; xv[7] = xv[8]; xv[8] = xv[9]; xv[9] = xv[10]; xv[10] = xv[11]; xv[11] = xv[12]; xv[12] = xv[13]; xv[13] = xv[14]; xv[14] = xv[15]; xv[15] = xv[16];
  xv[16] = Next_Input_Value / GAIN;

  yv[0] = yv[1]; yv[1] = yv[2]; yv[2] = yv[3]; yv[3] = yv[4]; yv[4] = yv[5]; yv[5] = yv[6]; yv[6] = yv[7]; yv[7] = yv[8]; yv[8] = yv[9]; yv[9] = yv[10]; yv[10] = yv[11]; yv[11] = yv[12]; yv[12] = yv[13]; yv[13] = yv[14]; yv[14] = yv[15]; yv[15] = yv[16];
  yv[16] =   (xv[0] + xv[16]) - 8 * (xv[2] + xv[14]) + 28 * (xv[4] + xv[12])
             - 56 * (xv[6] + xv[10]) + 70 * xv[8]
             + ( A * yv[0]) + ( B * yv[1])
             + (C * yv[2]) + (D * yv[3])
             + (E * yv[4]) + (F * yv[5])
             + (G * yv[6]) + (H * yv[7])
             + (I * yv[8]) + (J * yv[9])
             + (K * yv[10]) + (L * yv[11])
             + (M * yv[12]) + (N * yv[13])
             + (P * yv[14]) + ( Q * yv[15]);

  Next_Output_Value = yv[16];
}

As I said before, don't expect to see anything as Next_Input_Value and Next_Output_Value are both internal variables. Once you decide what your input and output are gonna be subtitute those variables for them

You will also need to know your sampling rate (how often to read your input in the loop)

Yes it is, what you did ? Thanks
There is another issue please look at new topic.

http://www.stm32duino.com/viewtopic.php?f=19&t=3064