Go Down

Topic: Using FIR Filter in a PWM code (Read 39 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...

Go Up