## Thursday, April 21, 2016

### Low Pass and High Pass Filter

I would like to wite a note about simple first order RC circuits.

Their $$V_o$$ and $$V_i$$ relation, $$\mathbf{H}$$, can be described as follows. $$\begin{equation} \mathbf{H}_{lp}(\mathbf{s})=\frac{1}{\mathbf{s}RC+1} \end{equation}$$

$$\begin{equation} \mathbf{H}_{hp}(\mathbf{s})=\frac{\mathbf{s}RC}{\mathbf{s}RC+1}=\frac{1}{\frac{1}{\mathbf{s}RC}+1} \end{equation}$$

RC can be denoted by time constant $$\tau$$. Then, the cutoff frequency is

$$\tau = RC = \frac{1}{\omega_c} = \frac{1}{2\pi f_c}.$$

Gain and phase shift for frequency $$\omega$$ can be obtained as follows. $$\begin{equation} \lvert \mathbf{H}_{lp}(\mathbf{s}) \rvert=\frac{1}{\sqrt {(\frac{\omega}{\omega_c})^2+1}} \end{equation}$$

$$\begin{equation} \lvert \mathbf{H}_{hp}(\mathbf{s}) \rvert=\frac{1}{\sqrt {(\frac{\omega_c}{\omega})^2+1}} \end{equation}$$

$$\begin{equation} \angle \mathbf{H}_{lp}(\mathbf{s})=-\tan^{-1}\frac{\omega}{\omega_c} \end{equation}$$

$$\begin{equation} \angle \mathbf{H}_{hp}(\mathbf{s})=\tan^{-1}\frac{\omega_c}{\omega} \end{equation}$$

The transfer function for the low pass filter can be transformed from s domain to z domain. Looking up transform table, $$\frac{1}{\mathbf{s}+a}$$ is $$\frac{\mathbf{z}}{\mathbf{z}-e^{-aT}}$$. Where pole is $$\mathbf{s}=-\frac{1}{RC}$$. A Z transform can be with a constant and that pole. $$\begin{equation} \mathbf{H}_{lp}(\mathbf{z})=\frac{K}{1-\mathbf{z}^{-1} e^{-\frac{T}{RC}}} \end{equation}$$ At a low frequency ( $$\mathbf{s}=0$$ ), $$\mathbf{z}=1$$ and the gain for the low pass filer is 1. $$\begin{equation} \lim_{z \to 1} \mathbf{H}_{lp}(\mathbf{z})=\frac{K}{1-e^{-\frac{T}{RC}}} = 1 \end{equation}$$ Therefore, the z transform for the low pass filter is $$\begin{equation} \mathbf{H}_{lp}(\mathbf{z})=\frac{1-e^{-\frac{T}{RC}}}{1-\mathbf{z}^{-1} e^{-\frac{T}{RC}}}. \end{equation}$$

Let $$a = e^{-\frac{T}{RC}}$$ and $$b=1-a$$, then $$\begin{equation} \mathbf{H}_{lp}(\mathbf{z})=\frac{b}{1-a\mathbf{z}^{-1}} \end{equation}$$ In recursive equation form, $$y(k)=b . x(k)+a . y(k-1)$$

High pass filter can be represented as 1 minus low pass filter. \begin{align} \mathbf{H}_{hp}(\mathbf{s})&=\frac{\mathbf{s}RC}{\mathbf{s}RC+1} \\ &=1-\frac{1}{\mathbf{s}RC+1} \\ &=1-\mathbf{H}_{lp}(\mathbf{s}) \end{align}

Therefore, their z transforms are $$\begin{equation} \mathbf{H}_{hp}(\mathbf{z})=\frac{a-a\mathbf{z}^{-1}}{1-a\mathbf{z}^{-1}} \end{equation}$$ Generally, $$\frac{b-c\mathbf{z}^{-1}}{1-a\mathbf{z}^{-1}}$$ can be represented by a recursive equation as follow. $$y(k)=a . y(k-1)+b . x(k) - c. x(k-1)$$ For this case, $$y(k)=a[( x(k)-x(k-1)+ y(k-1)]$$

Z transform can be performed in octave as follow. Control package can be loaded by the following command.
pkg load control

For example, MatLab code for 22 kΩ resistor and 1 μF capacitor can be written as

%Low pass filter
clc;
clear all;
close all;

R=22e3 %resistance (Ohm)
C=1e-6 %capacitance (F)
fc=1/(2*pi*R*C) %cutoff frequency (Hz)
RC=R*C
H=tf(,[RC 1]) %transfer function for 1/(sRC+1)
T=0.01 %sampling period (second)
Hd=c2d(H,T,'zoh') %discrete model using zero order hold
a=exp(-T/(RC)) %just to check coefficient
b=1-a

figure;
step(H,'-',Hd,'--')%check step response

figure;
bode(H,Hd) %check bode plot

figure;
margin(Hd);%check gain margin and phase margin


%High pass filter
clc;
clear all;
close all;

R=22e3 %resistance (Ohm)
C=1e-6 %capacitance (F)
fc=1/(2*pi*R*C) %cutoff frequency (Hz)
RC=R*C
H=tf([RC 0],[RC 1]) %transfer function for 1/(sRC+1)
T=0.01 %sampling period (second)
Hd=c2d(H,T,'zoh') %discrete model using zero order hold
a=exp(-T/(RC)) %just to check coefficient

figure;
step(H,'-',Hd,'--')%check step response

figure;
bode(H,Hd) %check bode plot

figure;
margin(Hd);%check gain margin and phase margin


Example implementation in C is as follow.

// First Order RC High Pass and Low Pass filters
// Author: Yan Naing Aye
// http://coolemerald.blogspot.sg/
// Date: 2016 April 21
//-----------------------------------------------------
//External variables
float ah=0; //for high pass filter
float al=0; //for low pass filter
//-----------------------------------------------------
//initialize low pass filter
//input: (T: sampling period, fc: cutoff frequency)
void InitLowPass(float T,float fc) {al=exp(-T*2*PI*fc);}
//-----------------------------------------------------
//Low Pass Filter
float LowPass(float x){static float yl=0; yl=x+al*(yl-x); return yl;}
//--------------------------------------------------------
//initialize high pass filter
//input: (T: sampling period, fc: cutoff frequency)
void InitHighPass(float T,float fc) {ah=exp(-T*2*PI*fc);}
//-----------------------------------------------------
//High Pass Filter
float HighPass(float x){static float yh=0,xh=0; yh=ah*(x-xh+yh); xh=x; return yh;}
//-----------------------------------------------------


As a practical example, 22 kΩ resistor and 1 μF capacitor are connected as a analog circuit on a breadboard. Then an equivalent digital filter is implemented on Arduino Zero which reads analog input voltage and output the filtered voltage on A0. Then a waveform generator is used to input a sinusoidal waveform to both RC analog filter and Arduino digital filter.Then the frequency is varied from 1 Hz to 30 Hz and output is measured using an oscilloscope. It is found that they produce identical waveforms for both low pass and high pass filter. Since the digital to analog converter can only produce positive voltage, an offset value 512 is added for 0-1023 range.

Arduino program and MatLab code can be obtained at the following link.

https://github.com/yan9a/RC_Filters