Upgrading legacy designs based on analog filters

Analog filters have been around since the beginning of electronics, ranging from simple inductor-capacitor networks to more advanced active filters with op-amps. As such, there is a rich collection of tried and tested legacy filter designs for a broad range of sensor measurement applications. However, with the performance requirements of modern IoT (Internet of Things) sensor measurement applications and lower product costs, digital filters integrated into the microcontroller’s application code are becoming the norm, but how can we get the best of both worlds?

Rather than re-inventing the wheel, product designers can take an existing analog filter transfer function, transform it to digital (via a transform) and implement it as digital filter in a microcontroller or DSP (digital signal processor). Although  analog-to-digital transforms have been around for decades, the availability of DSP design tooling for tweaking the ‘transformed digital filter’ has been somewhat limited, hindering the design and validation process.

A 2nd order analog lowpass filter is shown below, and in its simplest form, only 5 components are required to build the filter, which sounds easy. Right?

The pros

The most obvious advantage is that analog filters have an excellent resolution, as there are no ‘number of bits’ to consider. Analog filters have good EMC (electromagnetic compatibility) properties as there is no clock generating noise. There are no effects of aliasing, which is certainly true for the simpler op-amps, which don’t have any fancy chopping or auto-calibration circuitry built into them, and analog designs can be cheap which is great for cost sensitive applications.

Sound great, but what’s the bad news?

Analog filters have several significant disadvantages that affect filter performance, such as component aging, temperature drift and component tolerance. Also, good performance requires good analog design skills and good PCB (printed circuit board) layout, which is hard to find in the contemporary skills market.

These disadvantages make digital filters much more attractive for modern applications, that require high repeatability of characteristics.  Looking at an example, let’s say that you want to manufacture 1000 measurement modules after optimising your filter design. With a digital solution you can be sure that the performance of your filter will be identical in all modules. This is certainly not the case with analog, as component tolerance, component aging and temperature drift mean that each module’s filter will have its own characteristics. Also, an analog filter’s frequency response remains fixed, i.e. a Butterworth filter will always be a Butterworth filter – any changes the frequency response would require physically changing components on the PCB – not ideal!

Digital filters are adaptive and flexible, we can design and implement a filter with any frequency response that we want, deploy it and then update the filter coefficients without changing anything on the PCB! It’s also easy to design digital filters with linear phase and at very low sampling frequencies – two things that are tricky with analog.

Laplace to discrete/digital transforms

The three methods discussed herein essentially involve transforming a Laplace (analog) transfer function, \(H(s)\) into a discrete transfer function, \(H(z)\) such that a tried and tested analog filter that is already used in a design may be implemented on a microcontroller or DSP.

A selection of some useful Laplace to z-transforms are given in table below:

H(s) &\longleftrightarrow & H(z) \\ \hline
1 &\longleftrightarrow & 1 \\
\frac{\displaystyle1}{\displaystyle s}
&\longleftrightarrow& \frac{\displaystyle 1}{\displaystyle 1-z^{\scriptstyle -1}}\\
\frac{\displaystyle 1}{\displaystyle s^{\scriptstyle 2}} &\longleftrightarrow& \frac{\displaystyle
Tz^{\scriptstyle-1}}{\displaystyle (1-z^{\scriptstyle -1})^2}\\
\frac{\displaystyle 1}{\displaystyle s+a}
\frac{\displaystyle 1}{\displaystyle 1-e^{-aT}z^{-1}}\\
\frac{\displaystyle 1}{\displaystyle (s+a)^2}
&\longleftrightarrow& \frac{\displaystyle z^{-1}(1-e^{-aT})}{\displaystyle a(1-z^{-1})(1-e^{-aT}z^{-1})}\\\hline
A table of useful Laplace and z-transforms

The Bilinear z-transform (BZT)

The Bilinear z-transform (BZT), simply converts an analog transfer function, \(H(s)\) into a discrete transfer function, \(H(z)\) by replacing all \(s\) terms with the following:

s=\frac{2}{T}\frac{1-z^{-1}}{1+z^{-1}} \label{bzt}\)

where, \(T\) is the discrete system’s sampling period. However, substituting \(s=j\Omega\) and \(z=e^{jwT}\) into the BZT equation and simplifying, notice that there is actually a non-linear relationship between the analog, \(\Omega\) and discrete, \(w\) frequencies. This relationship is shown below, and is due to the nonlinearity of the arctangent function.

\omega=2\tan^{-1}\left(\frac{\Omega T}{2}\right)

Analysing the equation, it can be seen that the equally spaced  analog frequencies in the range \(-\infty<\Omega<\infty\) are nonlinearly compressed in the frequency range \(-\pi<w<\pi\) in the discrete domain. This relationship is referred to as frequency warping, and may be compensated for by pre-warping the analog frequencies by:

\Omega_c=\frac{2}{T}\tan\left(\frac{\Omega_d T}{2}\right)

where, \(\displaystyle\Omega_c\) is the compensated or pre-warped analog frequency, and \(\displaystyle\Omega_d\) is the desired analog frequency.

The ASN FilterScript command \(\texttt{bilinear}\) may be used convert a Laplace transfer function into its discrete equivalent using the BZT transform. An example is given below.

The Impulse Invariant Transform

The second transform, is referred to as the impulse invariant transform (IIT), since the poles of the Laplace transfer function are converted into their discrete equivalents, such that the discrete impulse response, \(h(n)\) is identical to a regularly sampled representation of the analog impulse response (i.e., \(h(n)=h(nT)\), where \(T\) is the sampling rate, and \(t=nT\)). The IIT is a much more tedious transformation technique than the BZT, since the Laplace transfer function must be firstly expanded using partial fractions before applying the transform.

The transformation technique is defined below:

\frac{K}{s+a} \quad\longrightarrow\quad
\frac{K}{1-e^{-aT}z^{-1}} \label{iit_def}

This method suffers from several constraints, since it does not allow for the transformation of zeros or individual constant terms (once expanded), and must have a high sampling rate in order to overcome the effects of spectral aliasing. Indeed, the effects of aliasing hinder this method considerably, such that the method should only be used when the requirement is to match the analog transfer function’s impulse response, since the resulting discrete model may have a different magnitude and phase spectrum (frequency response) to that of the original analog system. Consequently, the impulse invariant method is unsuitable for modelling highpass filters, and is therefore limited to the modelling of lowpass or bandpass type filters.

Due to the aforementioned limitations of the IIT method, it is currently not supported in ASN Filterscript.

The Matched-z transformation

Another analog to discrete modelling technique is the matched-z transformation. As the name suggests, the transform converts the poles and zeros from the analog transfer function directly into poles and zeros in the z-plane. The transformation is described below, where \(T\) is the sampling rate.


Analysing the transform equation, it can be seen that the transformed z-plane poles will be identical to the poles obtained with the impulse invariant method. However, notice that the positions of the zeros will be different, since the impulse invariant method cannot transform them.

The ASN Filterscript command \(\texttt{mztrans}\) is available for this method.

A detailed example

In order to demonstrate the ease of transforming analog filters into their discrete/digital equivalents using the analog to discrete transforms, an example of modelling with the BZT will now follow for a 2nd order lowpass analog filter.

A generalised 2nd order lowpass analog filter is given by:

H(s)=\frac{w_c^2}{s^2+2\zeta w_c s + w_c^2}

where, \(w_c=2\pi f_c\) is the cut-off frequency and \(\zeta\) sets the damping of the filter,  where a  \(\zeta=1/\sqrt{2}\) is said to be critically damped or equal to -3dB at \(w_c\). Many analog engineers choose to specify a quality factor, \(Q =  \displaystyle\frac{1}{2\zeta}\) or peaking factor for their designs. Substituting \(Q\) into \(H(s)\), we obtain:

H(s)=\frac{w_c^2}{s^2+ \displaystyle{\frac{w_c}{Q}s} + w_c^2}

Analysing, \(H(s)\) notice that \(Q=1/\sqrt{2} = 0.707\) also results in a critically damped response. Various values of \(Q\) are shown below, and as seen when \(Q>1/\sqrt{2}\) peaking occurs.

2nd order lowpass filter prototype magnitude spectrum for various value of Q:
notice that when \(Q>1/\sqrt{2}\) peaking occurs.

Before applying the BZT in ASN FilterScript, the analog transfer function must be specified in an analog filter object. The following code sets up an analog filter object for the 2nd order lowpass prototype considered herein:



Ha=analogtf(Nb,Na,1,"symbolic"); // make analog filter object

The \(\texttt{symbolic}\) keyword generates a symbolic transfer function representation in the command window. For a sampling rate of \(f_s=500Hz\) and \(f_c=30Hz\) and \(Q=0.707\), we obtain:

ASN FilterScript analog command

Applying the BZT via the \(\texttt{bilinear}\) command without prewarping,


The complete frequency response of the transformed digital filter is shown below, where it can be seen that the at \(30Hz\) the magnitude is \(-3dB\) and the phase is \( -90^{\circ}\), which is as expected. Notice also how the filter’s magnitude roll-off  is affected by the double zero pair at Nyquist (see the z-plane chart below), leading to differences from its analog cousin. Pole-zero chart 2nd order LPF

2nd order LPF

The pole-zero positions may be tweaked within ASN Filterscript or via the ASN Filter Designer’s interactive pole-zero z-plane plot editor by just using the mouse!


The complete code for transforming a generalised 2nd order analog  lowpass filter prototype into its digital equivalent using the BZT via ASN FilterScript is given below:

ClearH1;  // clear primary filter from cascade
interface Q = {0.1,10,0.02,0.707};
interface fc = {10,200,10,40};



Ha=analogtf(Nb,Na,1,"symbolic"); // make analog filter object
Hd=bilinear(Ha,0,"symbolic"); // transform Ha via BZT into digital object, Hd


Download demo now

Licencing information

All-pass filters

All-pass filters provide a simple way of altering/improving the phase response of an IIR without affecting its magnitude response. As such, they are commonly referred to as phase equalisers and have found particular use in digital audio applications.

In its simplest form, an All-pass  filter can be constructed from a first order transfer function, i.e.,

\( A(z)=\Large{\frac{r+z^{-1}}{1+r z^{-1}}}  \, \,  \normalsize{; r<1} \)

Analysing \(\small A(z)\), notice that the pole and zero lie on the real z-plane axis and that the pole at radius \(\small r\) has a zero at radius \(\small 1/r\), such that the poles and zeros are reciprocals of another. This property is key to the all-pass filter concept, as we will now see by expanding the concept further to a second order all-pass filter:

\( A(z)=\Large\frac{r^2-2rcos \left( \frac{2\pi f_c}{fs}\right) z^{-1}+z^{-2}}{1-2rcos \left( \frac{2\pi f_c}{fs}\right)z^{-1}+r^2 z^{-2}} \)

Where, \(\small f_c\) is the centre frequency, \(\small r\) is radius of the poles and \(\small f_s\) is the sampling frequency. Notice how the numerator and denominator coefficients are arranged as a mirror image pair of one another.  The mirror image property is what gives the all-pass filter its desirable property, namely allowing the designer to alter the phase response while keeping the magnitude response constant or flat over the complete frequency spectrum.

Frequency response of all-pass filter:
Notice the constant magnitude spectrum (shown in blue).


An All-pass filter may be implemented in ASN FilterScript as follows:

ClearH1;  // clear primary filter from cascade

interface radius = {0,2,0.01,0.5};   // radius value
interface fc = {0,fs/2,1,fs/10};     // frequency value

Num = {radius^2,-2*radius*cos(Twopi*fc/fs),1};
Den = reverse(Num); // mirror image of Num
Gain = 1;

For a detailed disucssion on IIR filter phase equalisation, and the ASN Filter designer’s APF (all-pass filter) design tool, please refer to the following article.


Download demo now


Licencing information


A  Peaking or Bell filter is a type of audio equalisation filter that boosts or attenuates the magnitude of a specified set of frequencies around a centre frequency in order to perform magnitude equalisation. As seen in the plot in the below, the filter gets its name from the shape of the its magnitude spectrum (blue line) which resembles a Bell curve.

Frequency response (magnitude shown in blue, phase shown in purple) of a 2nd order Bell filter peaking at 125Hz.

All-pass filters

Central to the Bell filter is the so called All-pass filter. All-pass filters provide a simple way of altering/improving the phase response of an IIR without affecting its magnitude response. As such, they are commonly referred to as phase equalisers and have found particular use in digital audio applications.

A second order all-pass filter is defined as:

\( A(z)=\Large\frac{r^2-2rcos \left( \frac{2\pi f_c}{fs}\right) z^{-1}+z^{-2}}{1-2rcos \left( \frac{2\pi f_c}{fs}\right)z^{-1}+r^2 z^{-2}} \)

Notice how the numerator and denominator coefficients are arranged as a mirror image pair of one another.  The mirror image property is what gives the all-pass filter its desirable property, namely allowing the designer to alter the phase response while keeping the magnitude response constant or flat over the complete frequency spectrum.

A Bell filter can be constructed from the \(A(z)\) filter by the following transfer function:


After some algebraic simplication, we obtain the transfer function for the Peaking or Bell filter as:

\(H(z)=\Large{\frac{1}{2}}\left[\normalsize{(1+K)} + \underbrace{\Large\frac{k_2 + k_1(1+k_2)z^{-1}+z^{-2}}{1+k_1(1+k_2)z^{-1}+k_2 z^{-2}}}_{all-pass filter}\normalsize{(1-K)} \right] \)

  • \(K\) is used to set the gain and sign of the peak
  • \(k_1\) sets the peak centre frequency
  • \(k_2\) sets the bandwidth of the peak


A Bell filter may easily be implemented in ASN FilterScript as follows:

ClearH1;  // clear primary filter from cascade
interface BW = {0,2,0.1,0.5}; // filter bandwidth
interface fc = {0, fs/2,fs/100,fs/4}; // peak/notch centre frequency
interface K = {0,3,0.1,0.5}; // gain/sign



Pz = {1,k1*(1+k2),k2}; // define denominator coefficients
Qz = {k2,k1*(1+k2),1}; // define numerator coefficients
Num = (Pz*(1+K) + Qz*(1-K))/2;
Den = Pz;
Gain = 1;

This code may now be used to design a suitable Bell filter, where the exact values of \(K, f_c\) and \(BW\) may be easily found by tweaking the interface variables and seeing the results in real-time, as described below.

Designing the filter on the fly

Central to the interactivity of the FilterScript IDE (integrated development environment) are the so called interface variables. An interface variable is simply stated: a scalar input variable that can be used modify a symbolic expression without having to re-compile the code – allowing designers to design on the fly and quickly reach an optimal solution.

As seen in the code example above, interface variables must be defined in the initialisation section of the code, and may contain constants (such as, fs and pi) and simple mathematical operators, such as multiply * and / divide. Where, adding functions to an interface variable is not supported.

An interface variable is defined as vector expression:

interface name = {minimum, maximum, step_size, default_value};

Where, all entries must be real scalars values. Vectors and complex values will not compile.

Real-time updates

All interface variables are modified via the interface variable controller GUI. After compiling the code, use the interface variable controller to tweak the interface variables values and see the effects on the transfer function. If testing on live audio, you may stream a loaded audio file and adjust the interface variables in real-time in order to hear the effects of the new settings.

Comb filters have found use as powerline (50/60Hz) harmonic cancellation filters in audio applications, and form the basis of so called CIC (cascaded integrator–comb) filters used for anti-aliasing in decimation (sample rate reduction), and anti-imaging in interpolation (sample rate increase) applications.

The frequency response of a comb filter consists of a series of regularly-spaced troughs, giving the appearance of a comb. As seen in the plot below, the spacing of each trough appears at either odd or even harmonics of the desired fundamental frequency.

Frequency response of a typical FIR comb filter (odd harmonics cancellation):
\(f_s=500Hz\),  \(f_c=25Hz\), \(L=10\) and \(\alpha=1\)

An FIR comb filter can be described by the following transfer function:

\(H(z)=1+\alpha z^{-L}\)
\(\Rightarrow Y(z)=X(z)\left[1+\alpha z^{-L}\right]\)

Clearly, the comb filter is simply a weighted delayed replica of itself, specifiied by \(L\). Taking inverse z-transforms, we obtain the difference equation needed for implementation,

\(y(n)=x(n)+\alpha x(n-L)\)

where, \(\alpha\) is used to set the Q (bandwidth) of the notch and may be either positive or negative depending on what type of frequency response is required. In order to elaborate on this, negative values of \(\alpha\) have their first trough at DC and their second trough at the fundamental frequency. Clearly this type of comb filter can be used to remove any DC components from a measured waveform if so required. All subsequent troughs appear at even harmonics up to and including the Nyquist frequency.

Positive values of \(\alpha\) on the other hand, only have troughs at the fundamental and odd harmonic frequencies, and as such cannot be used to remove any DC components.

Application to powerline interference cancellation

The affectivity of the comb filter is dependent on the sampling frequency, \(f_s\), as \(L\) is limited to integer values only. Also, a relationship between \(f_s\), as \(L\) and will be dependent on the sign of \(\alpha\). Thus, for the purposes of the mains cancellation application considered in this discussion, only positive values of will be considered, as we need only cancel odd harmonics.

A simple relationship for determining  \(L\) can be summarized for positive values of \(\alpha\) as follows:

\(L=ceil\left( \large{\frac{f_s}{2f_c}}\right)\)

where, \(f_c\) is the desired centre point of the fundamental notch frequency. Based on this expression, we can re-calculate the sampling frequency, such that \(f_c\) is a true multiple of \(f_s\)

\(f_{snew}=2f_c L\)


For the example considered herein, i.e. \(f_s=500Hz\) and \(f_c=25Hz\), we obtain \(L=10\). However, if \(f_c=60Hz\), we would need \(L=5\), and a new sampling rate of \(600Hz\) respectively, although it’s interesting to note that \(f_s=480Hz\) for \(L=4\) would also suffice.


An FIR comb filter may be implemented in ASN FilterScript as follows:

ClearH1;  // clear primary filter from cascade
interface L = {4,20,1,5}; // delay
interface alpha = {-1,1,0.01,1};

Num = {1,zeros(L-1),alpha}; // numerator coefficients
Den = {1};  
Gain = 1/sum(abs(Num));



Download demo now


Licencing information


In signal processing, the need sometimes arises to nudge or fine-tune the sampling instants of a signal by a fraction of a sample. An FIR Farrow delay filter is typically employed to achieve this task, and may be combined with a traditional integer delay line in order to achieve a universal fractional length delay line.

A Fractional delay based on an FIR Farrow structure may be defined as:

\(H(z)=(1-\alpha)+\alpha z^{-1}; \; 0 \leq \alpha \leq 1 \)

Which produces a fractional linear delay of \(\alpha\) between 0 and 1 samples. However, a more universal building block can be achieved by combining the Farrow delay structure with an integer delay, \(\Delta\)

\(H(z)=(1-\alpha) z^{-\Delta}+\alpha z^{-(\Delta+1)}\)

The plot shown below shows the magnitude (blue) and phase (purple) spectra for \(\Delta=9, \, \alpha=0.52\). As seen, the fractional delay element results in a non-flat magnitude spectrum at higher frequencies.

Frequency reponse of Farrow delay filter.


A Farrow delay filter may be implemented in ASN FilterScript as follows:

ClearH1;  // clear primary filter from cascade

interface alpha = {0,1,0.02,.5}; // fractional delay
interface D = {1,30,1,10};       // integer delay

Num = {zeros(D),1-alpha,alpha}; // numerator coefficients
Den = {1};                      // denominator coefficient
Gain = 1/sum(Num);              // normalise gain at DC



Download demo now


Licencing information


The moving average (MA) filter is perhaps one of the most widely used FIR filters due to its conceptual simplicity and ease of implementation. As seen in the diagram below, notice that the filter doesn’t require any multiplications, just additions and a delay line, making it very suitable for many extreme low-power embedded devices with basic computational capabilities.

However, despite its simplicity, the moving average filter is optimal for reducing random noise while retaining a sharp step response, making it a versatile building block for smart sensor signal processing applications.

A moving average filter of length \(L\) for an input signal \(x(n)\) may be defined as follows:

\(y(n)=\large{\frac{1}{L}}\normalsize{\sum\limits_{k=0}^{L-1}x(n-k)}\) for \(\normalsize{n=0,1,2,3….}\)

Where, a simple rule of thumb states that the amount of noise reduction is equal to the square-root of the number of points in the average. For example, an MA of length 9 will result in a factor 3 noise reduction.

Frequency response of an MA filter of length 9. Notice the poor stopband attentuation at around -20dB.


  • Most commonly used digital lowpass filter.
  • Optimal for reducing random noise while retaining a sharp step response.
  • Good smoother (time domain).
  • Unity valued filter coefficients, no MAC (multiply and accumulate) operations required.
  • Conceptually simple to implement.


  • Inflexible frequency response: nudging a conjugate zero pair results in non-unity coefficients.
  • Poor lowpass filter (frequency domain): slow roll-off and terrible stopband attenuation characteristics.


The MA filter may be implemented in ASN FilterScript as follows:

ClearH1;  // clear primary filter from cascade

Hd=movaver(8,"symbolic");  // design an 8th order MA 
Num = getnum(Hd);   // define numerator coefficients
Den = {1};          // define denominator coefficients
Gain = getgain(Hd); // define gain

A more computationally efficient implementation of the MA filter is discussed here.

Further reading

  • Understanding Digital Signal Processing, Chapter 5, R. G. Lyons
  • The Scientist and Engineer’s Guide to Digital Signal Processing, Chapter 15, Steven W. Smith

Download demo now

Licencing information