Digital Filter FIR Audio Signal

I'm making an audio filter and I don´t know what is the mistakke that I've made. Im trying to itroduce an audio signal and process it with the arduino but I don´t get what I want. I got the coefficients of the filter with the tool of matlab of Filter designer.
This is the code that I've tried to do.


#define orden 11  
const float coeficientes_b[orden]={ 
7.39142315096373e-19,
0.00930428314501815,
0.0475777661344175,
0.122363546361145,
0.202246558429840,
0.237015691859159,
0.202246558429840,
0.122363546361145,
0.0475777661344175,
0.00930428314501815,
7.39142315096373e-19*/
};

float memoria[orden]; 
unsigned int y_n;


void setup() {
  pinMode(12,OUTPUT);
  DDRD=0b11111111;
 
  ADMUX|=B00000000; 
  ADMUX|=B01000000;
  ADCSRA|=B00000010; 
  ADCSRA|=B11000000; 
  

}

void loop() {

  for(int n=1; n<=orden; n++){
    memoria[n]=0;}

  while(1){
    digitalWrite(12,HIGH);
    
    for(int k=orden; k!=1; k--){
      memoria[k]=memoria[k-1];
      } 
      
  memoria[1]=ADCL | (ADCH<<8);//
  y_n=0; 
  for(int n=1; n<=orden; n++ ){
    y_n+= coeficientes_b[n] * memoria[n]; }
  
  digitalWrite(12, LOW);
  delayMicroseconds(383);
  PORTD=y_n&255;/
  }
}

I put some variables in spanish because that´s my language and I apologize if i can´t express myself in a good way because my writing mistakes. But, I hope that some one could help me. Greatings and Thanks.

For an 'n' element array, indices go from zero to n - 1.
Therefore there is no element with the index 'n' (in your case "orden")

Then, Have I change the initial value of n? Instead I start that "for" with zero, do I? or what changedo i have to do?

Please use code tags when posting, so that it looks like this corrected code:

for(int n=0; n<orden; n++){
memoria[n]=0;}
...
for(int n=0; n<orden; n++ ){
y_n+= coeficientes_b[n] * memoria[n]; }

When dealing with digital filters it is normal to use the concept of a ring buffer. That is a buffer (array) of size n. You then have a pointer variable for where you write to the buffer each sample you take and increment the input pointer. You then test to see if this pointer is greater than the end of he buffer and if it is you set it to the start of the buffer, which is zero. This is called wrapping round the pointer.

Then you take a copy of that input pointer and decrement it, so that it points to the last sample, get that sample and multiply by the first filter coefficient repeat the decrement and multiply making sure to wrap round this pointer when it reaches -1. When you reach the value of the input pointer you stop and output the sample you have accumulated.

Note this will only output an 8 bit value so you will have to scale the output value rather than just truncate it. You also need an 8 bit D/A converter and restoration filter to the 8 output bits of port D. Are they all free to use?

Also note that an audio input signal must be biased at the mid rail point and so the A/D reading of this mid rail point must be subtracted from the sample value you read to make the samples are zero at this point.

What your code seems to be doing is applying all the coefficients to the single input sample, this is not how digital filters work. Each coefficient must be applied to each one of the last n samples in turn.

You can't create a float type variable with this statement that only works to make two bytes into an int. after that you need to convert it into a float type variable.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.