Using FIR Filter in a PWM code

Hello.
I have a pwm code for Arduino Uno and It takes in audio on ADC0, and plays it out the PWM.
here is the code :

// adc_to_pwm.pde
// takes in audio data from the ADC and plays it out on
// Timer1 PWM.  16b, Phase Correct, 31.25kHz - although ADC is 10b.
#define PWM_FREQ 0x00FF // pwm frequency - see table
#define PWM_MODE 0 // Fast (1) or Phase Correct (0)
#define PWM_QTY 2 // number of pwms, either 1 or 2

void setup() {
  // setup ADC
  ADMUX = 0x60; // left adjust, adc0, internal vcc
  ADCSRA = 0xe5; // turn on adc, ck/32, auto trigger
  ADCSRB =0x07; // t1 capture for trigger
  DIDR0 = 0x01; // turn off digital inputs for adc0
  // setup PWM
  TCCR1A = (((PWM_QTY - 1) << 5) | 0x80 | (PWM_MODE << 1)); // 
  TCCR1B = ((PWM_MODE << 3) | 0x11); // ck/1
  TIMSK1 = 0x20; // interrupt on capture interrupt
  ICR1H = (PWM_FREQ >> 8);
  ICR1L = (PWM_FREQ & 0xff);
  DDRB |= ((PWM_QTY << 1) | 0x02); // turn on outputs
  
  sei(); // turn on interrupts - not really necessary with arduino
}

void loop() {
  while(1); // gets rid of jitter
}

ISR(TIMER1_CAPT_vect) {
  
  // get ADC data
  unsigned int temp1 = ADCL; // you need to fetch the low byte first
  unsigned int temp2 = ADCH;
  // although ADCH and ADCL are 8b numbers, they are represented
  // here by unsigned ints, just to demonstrate how you would
  // use numbers larger than 8b.
  
  // output high byte on OC1A
  OCR1AH = temp2 >> 8; // takes top 8 bits
  OCR1AL = temp2; // takes bottom 8 bits
  
  // output low byte on OC1B
  OCR1BH = temp1 >> 8;
  OCR1BL = temp1;
}

So I should use low pass and high pass filter with FIR in this code to get high or low frequencies. and I want to use this FIR Arduino library but I am confused and don't know how to use it in this code. I should use 2KHz cut-off frequency for my FIR and I have to give the value in ADCH and ADCL in ISR to filter and get results. the pwm frequency is 31.25KHz and how to use FIR with 2KHz in this code? should I change my PWM?? should I call filter code in ISR function? please guide me! thank you

So I should use low pass and high pass filter with FIR in this code to get high or low frequencies.

What's the filtering for? With no filtering you should get-out whatever goes-in (except for any sampling or PWM artifacts).

Filtering with the Arduino won't solve any aliasing problems. The anti-aliasing filter goes in front of the ADC. And it's not going to "smooth out" the PWM either because that has to be done after the PWM (or after the DAC).

DVDdoug:
What's the filtering for? With no filtering you should get-out whatever goes-in (except for any sampling or PWM artifacts).

Filtering with the Arduino won't solve any aliasing problems. The anti-aliasing filter goes in front of the ADC. And it's not going to "smooth out" the PWM either because that has to be done after the PWM (or after the DAC).

I should write code for an audio controller and I've been told to use FIR filters in this code and get output...

No digital filters on the Arduino is going to improve anything with regards to the sound quality.
Who ever told you it would is either misunderstanding the situation or simply is wrong.

Is "I've been told to use" an academic assignment?

pjrc:
Is "I've been told to use" an academic assignment?

It's not an assignment but I should do it to learn and prepare for a serious project.

Using an unnecessary filter prepairs you for nothing.

What do you hope this filter will do?

How are you going to test it?

Grumpy_Mike:
Using an unnecessary filter prepairs you for nothing.

What do you hope this filter will do?

How are you going to test it?

I have a board. I just want to get results form low pass and highpass filters. just don't know how to find coefficients and order for 2khz cutoff frequency!!!!

how to find coefficients and order for 2khz cutoff frequency

You do not find the order of a filter you specify the order of a filter. You can get the coefficients of a digital filter here:- http://www.schwietering.com/jayduino/filtuino/

Grumpy_Mike:
No digital filters on the Arduino is going to improve anything with regards to the sound quality.
Who ever told you it would is either misunderstanding the situation or simply is wrong.

Well, with the obvious exception of noise-shaping filter you mean?

Grumpy_Mike:
You do not find the order of a filter you specify the order of a filter. You can get the coefficients of a digital filter here:- http://www.schwietering.com/jayduino/filtuino/

Thank you, this site is for IIR filters. I want to use FIR Filters.

Try searching the web for FIR filters.

noonhe:
Thank you, this site is for IIR filters. I want to use FIR Filters.

Why? Do you know the difference?

Grumpy_Mike:
No digital filters on the Arduino is going to improve anything with regards to the sound quality.
Who ever told you it would is either misunderstanding the situation or simply is wrong.

I think OP is using this project purely for learning purposes - not for a practical system.

noonhe:
I have a board. I just want to get results form low pass and highpass filters. just don't know how to find coefficients and order for 2khz cutoff frequency!!!!

Finding FIR coefficients is not an easy or trivial process for someone new to DSP (digital signal processing). I would suggest using MATLAB (if you have a copy) and using some of the FIR filter coefficient finding functions and/or other DSP functions already built in to the app. I'd also suggest taking a look at this document.

Very first google hit for "fir filter designer" http://t-filter.engineerjs.com/

noonhe:
Hello.
I have a pwm code for Arduino Uno and It takes in audio on ADC0

Did you also consider using an analog low-pass filter at the input to the arduino? That's to ensure that only signals having frequencies in the audio range get sampled by the arduino - to avoid 'aliasing'.

  1. FIR filters can be much slower to calculate for a given filter specification, a bad idea on a low-performance 8-bit
    microcontroller. Use IIR unless you need phase-linear.

  2. Filter design starts with a specification of phase behaviour, passband, stopband, ripple and attentuation, from this the type and then order is determined. Then the filter is designed.

  3. Its easy to do in Python using the scipy.signal library. Checkout buttord, butter, ellipord, eliip, cheby1ord, checy1, bessel functions.