Go Down

Topic: Using FIR Filter in a PWM code (Read 2320 times) previous topic - next topic

noonhe

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 :
Code: [Select]

// 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

DVDdoug

Quote
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).

noonhe

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...

Grumpy_Mike

#3
Jun 25, 2017, 10:17 am Last Edit: Jun 25, 2017, 10:18 am by 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.

pjrc

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

noonhe

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.

Grumpy_Mike

#6
Jun 25, 2017, 02:43 pm Last Edit: Jun 25, 2017, 02:44 pm by 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?

noonhe

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!!!!

Grumpy_Mike

Quote
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/

MarkT

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?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

noonhe

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.

jremington

Try searching the web for FIR filters.

Grumpy_Mike

Thank you, this site is for IIR filters. I want to use FIR Filters.
Why? Do you know the difference?

Power_Broker

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.


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.
"The desire that guides me in all I do is the desire to harness the forces of nature to the service of mankind."
   - Nikola Tesla

jremington

#14
Jun 27, 2017, 03:24 am Last Edit: Jun 27, 2017, 03:25 am by jremington
Very first google hit for "fir filter designer" http://t-filter.engineerjs.com/

Go Up