# Realtime filtering on a microcontroller, with dynamic cut off frequencies

Is it possible to dynamically filter a signal in realtime on a microcontroller?

What I mean by dynamically is to change the cut off frequency of the filter on the fly.

I have some DSP knowledge and realize that I probably do not have the computational power to do an FFT on the fly. Instead I should be looking at creating an IIR or FIR filter that I can continually convolve with my signal.

The issue is now whether or not I have enough computational power to generate the filter coeffecients quickly and then convolve with the signal. I'm guessing that because of this I'm going to have to be very lenient on the response of the filter which is ok for my application.

I have looked at algorithms such as the Remez Exchange algorithm and believe this may be able to work but am not sure. I wanted to check before I start the lengthy process of implementing whether or not I am on the right track.

• "I probably do not have the computational power to do an FFT on the fly" That's not how you usually filter even if you do have the power. – endolith Mar 8 '13 at 15:52

• You can pre-compute the coefficients for a bunch (say $N=1024$) different cutoff frequencies. If your application doesn't need to provide fine control over the cutoff frequency, just read from your table and you're done. This works best if you have a lot of ROM and/or you use short FIR or IIR filters.
• If you need finer control than those $N$ values, and if the relationship between your coefficients and cutoff frequency is well-behaved (ideally, monotonic), you can use linear interpolation within the pre-computed table of coefficients. This works best with well-behaved structures like state-variable filters - for which the coefficients are monotonic functions of the cutoff frequencies, and which do not get unstable when modulating them at fast rates. This is a very common problem in musical audio applications (equalizers, synthesizer analog filter emulation...). If you use other filter topologies, make sure that they cannot get unstable when their coefficients are changed "in mid-flight" - to me, this is a harder problem than actually computing them.

The main issues are different based upon whether you're doing FIR or IIR filtering:

• FIR:

• You probably don't want to try designing filters via the Remez exchange algorithm online in a real time application. For some combinations of parameters, it can be tricky to get the algorithm to converge and/or it may take a lot of iterations, so the runtime for the filter design process isn't very predictable. That's not good for real-time systems.

• One suitable algorithm for real-time filter design (although you have less control over the resulting frequency response) is the window design method. Essentially, you create a direct finite-length approximation of the ideal filter's frequency response. You don't have the same degrees of freedom as you would with a Remez exchange equiripple filter design, but the amount of computation required here is very predictable.

• You shouldn't have to worry about stability with FIR filters; they are unconditionally BIBO stable. This guarantee continues to hold even if you change the coefficients on the fly.

• IIR:

• You will want to think about the same things regarding how to calculate coefficients on the fly if you want to do that. Often, IIR filters are based upon some analog prototype (e.g. a Butterworth filter), using the analytically-known frequency response for the prototype filter and a mapping scheme like the bilinear transform. These could be amenable to dynamic recalculation, depending upon the complexity of the prototype filter's transfer function.

• One simple topology that allows for easy tuning of the filter cutoff is a so-called "leaky integrator", which is described in detail in this answer to a previous question. It contains a single parameter $\alpha$, which varies across the range $[0, 1)$. Adjusting this parameter will directly affect the filter's cutoff frequency. However, as a really simple filter structure, you don't have a lot of control over its frequency response if you have specific requirements for its shape.

• IIR filters are unstable if any of their poles lie outside the unit circle. Therefore, when designing such a filter, you'll want to make sure that you don't generate a design that toes this line. If you have poles that are inside, but very close to, the unit circle, then finite numeric precision effects could induce instability (think of roundoff error in your calculations that pushes the effective pole locations outside the unit circle). As pichenettes pointed out already, if you use an interpolation-based method for designing filters on the fly, then you should be very mindful of this requirement.

• If the IIR filter has poles too close to the unit circle, a change in coefficients could cause large disturbances at the output even if the filter is stable. The IIR fiter will likely have far fewer coefficients to calculate/update. Probably want to keep the Q fairly low to get well behaved operation when adjusting the response. – user2718 Mar 8 '13 at 13:52