Array Filter Help!!

Hi All,

Im fairly new to programming the arduino, i have experience in VB and C#.

I am currently doing a university project which requires me to meaure the frequency of a signal and apply a 2nd order low pass filter to the signal. (bessel filter)

I already have managed to measure the frequency and have called it the variable Freq.

What i now need to do is pass each measured Freq value into an array, then apply the bessel filter to it (basically just a formula to each Freq value based on other previous values in the array).

so something like this (for 100 samples): (how i think id do it in vb)

For (intcount 1 to 100) " this loop passes the frequency data into the array"
{
Array(intcount) = freq;
}

for (intcount 1 to 100)
{
FilteredArray(intcount) = 0.009(array(n)) + 0.01(array(n-1)) +0.009(array(n-1)) +1.7(filteredarray(n-1)) -0.7(filteredarray(n-2))
}

serial.write(filteredarray) " allows me to see the values for the filtered array
serial.write(array) " allows me to see values of the original array

any way as i odnt have much experience in programming arrays itno the arduino, i was hoping that one of you could perhaps suggest a way to do this^?

Thanks in advance!! :slight_smile:

Look at this and see whether you can apply any of the ideas to your problem

float rawData[100];
float filteredData[100];

void setup()
{
  for (int f = 0; f < 100; f++)
  {
    rawData[f] = measureFrequency(); 
  }

  for (int f = 0; f < 100; f++)
  {
    filteredData[f] = (0.009 * rawData[f]) + (0.01 * rawData[f-1]) + (0.009 * rawData[f-1]) + (1.7 * filteredData[f-1]); 
  }

  for (int f = 0; f < 100; f++)
  {
    Serial.println(rawData[f]);
  }

  for (int f = 0; f < 100; f++)
  {
    Serial.println(filteredData[f]);
  }
}

void loop()
{
}

int measureFrequency()
{
  int freq = 0;
  //code here to measure frequency and put it in the freq variable
  return freq;
}

Arrays in C are indexed from zero and there is no bounds checking so be careful not to stray outside the declared array dimensions.

serial.write(filteredarray)  " allows me to see the values for the filtered array

No, it won't. Dumping binary data that way is not going to prove useful with most serial monitor applications.

Careful! This line will attempt to access out-of-bounds array elements during the first pass (e.g. rawData[-1])

filteredData[f] = (0.009 * rawData[f]) + (0.01 * rawData[f-1]) + (0.009 * rawData[f-1]) + (1.7 * filteredData[f-1]); 
  }

So it does.
It's just as well that I warned myself :slight_smile:

Can you tell us how you developed those filter coefficients?