webee

Simple First Order Digital Filter Design

Introduction

It is often necessary to filter data from sensors or audio streams in order to suppress unwanted noise. And, most of the time, a simple first order filter is all that is required. The technique discussed below describes a quick design solution that will allow you to design and implement a first order using fixed-point math.

Background

In the z-domain, the equation for a first order filter looks like: Where:

In the time domain, the above transforms into: The Meat of the Matter

If we replace: with k and solve the above to get unity gain, then our difference equation can be simplified to: The trick now is to determine what those k values need to be. First, let’s translate a to our cutoff frequency and T to our sampling rate:

a=2πfc
T=1/fs

k can now be readily derived given our desired cutoff frequency and sampling rate: As an example, let’s assume a sampling rate of 44.1Khz and a desired cutoff frequency of 100Hz. Then, we can derive k from: Our difference equation that realizes this filter becomes:

yn = (1-0.9859)xn+0.9859yn-1
=0.0141xn+0.9859yn-1

To be sure, we can substitute the identity term: into the equation for H(z) above and then use Mathcad to create a bode plot:

According to Mathcad, the magnitude of the above plot equals 0.707 at 100 Hz which is equivalent to the response of an analog filter at the same frequency.

The code to implement this filter looks like:

// Example code for 100Hz Digital Low Pass Filter

// Coefficients are designed for 44.1Khz Sampling rate

// The int type is assumed to be 16 bits

#define k1 0.9859 // the k coefficient

#define k2 1-k1 // 1-k

int 100hz_lpf(int x)

\{

static int y1 = 0;

int y0;

y0 = k2*x + k1*y1;

y1 = y0;

return y0;

\} 