IIR Filter on the KL25Z

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

IIR Filter on the KL25Z

911 Views
nick92
Contributor II

Hey guys, I'm trying to implement a simple 2 pole IIR filter on my KL25Z. Coefficients were calculated using Matlab, it's a Butterworth Highpass, second order, Fs at 10 KHz, and Cut off at 60 Hz. I'm feeding in a mixed signal sin(2pi80t)+sin(2pi175t) which is generated by one of the boards to the the other board which is running the IIR filter program.

Here's the code: http://imgur.com/NDqYXfT

Here's what I'm feeding into my filtering board:http://imgur.com/d7NzRzG

Here's the filter board DAC output: http://imgur.com/Vmjn9LH

I'm expecting the 80Hz sinewave to remain at the output, or at least something similar (I know these filters are not perfect), but the output looks like an audiowave or some sort. Can't figure out why. You guys have an idea what it is or maybe there's some error in my code (most likely). Thanks a lot guys!

Labels (1)
0 Kudos
4 Replies

623 Views
tomasmickus
Contributor II

Hi, i have used IIR filters on mcu.

y[j]=B[0]*x[j]+B[1]*x[j-1]+B[2]*x[j-2]-A[1]*y[j-1]-A[2]*y[j-2]; A,B coefficients were from matlab, And it worked.

Your code looks good. But filter to work, for(;;) cycle must sample at 10kHzrate as you chosen in matlab. And now i dont see any timing in code, that means that it samples as fast as it can. Maybe thats a problem. I think that coefficients are correlated with sampling frequency, in matlab [B,A]=butter(order,Wn,'type'), Wn=0:1, Wn = fcutoff/fsampling. Nice oscilloscope thought.

623 Views
nick92
Contributor II

Hey Tomas,

Thanks for the reply. Yes, that makes sense. How did you set your ADC to sample at a specific frequency? Did you use a wait function or a timer of some sort?

Cheers,

- Nick.

0 Kudos

623 Views
tomasmickus
Contributor II

How i do it, add TimerInt_LDD (Timer Interrupt) component from processor expert component library, in component inspector set interrupt period, compile... Events.c file have intrrupt functions.

//Events.c

bool need_to_sample = 0;

void TI1_OnInterrupt(LDD_TUserData *UserDataPtr)

{

  need_to_sample= 1;

  /* Write your code here ... */

}

/* END Events */

//ProcessorExpert.c

extern bool need_to_sample;

int main(){

for(;;)

if(need_to_sample){

//your code

need_to_sample=0;

}
}

BUT. This configuration only protects that mcu would not sample at higher rates than set, but not from lowers. Figure out yourselt :smileyhappy:

0 Kudos

623 Views
nick92
Contributor II

Hey Tomas, thanks for your suggestion, I will try a few things now.

0 Kudos