MEE 0828

SOFTWARE IMPLEMENTATION

OF DIGITAL FILTERS

Satish kumar Are Manoranjan Reddy Thangalla Saikrishna Gajjala

This thesis is presented as part of Degree of Master of Science in Electrical Engineering

Blekinge Institute of Technology August 2008

Blekinge Institute of Technology School of Engineering Department of Processing Supervisors: Nedelko Grbic Mikael Swartling

2

ABSTRACT This thesis proposes to create a MATLAB GUI (Graphical User Interface) to replace an existing laboration exercise in at Blekinge Institute of Technology. MATLAB is a matrix-based technical computing language widely used throughout the scientific, engineering and mathematical communities. A GUI provides a graphical interface between the program and the user, facilitating ease and frequency of use. Development of a MATLAB GUI for this laboration exercise will benefit the students and increase the awareness towards designing of digital filters.

The developed software provides an interface between audio recording and playback hardware and the user when exploring parameters. This software is designed for analyzing digital filter characteristics such as amplitude, phase and pole/zero locations which are useful in designing an appropriate filter. This can be achieved by entering arbitrary filter parameters.

3

4

ACKNOWLEDGMENTS We would like to express our sincerest thanks and gratitude to Mikael Swartling for being our mentor on this journey. His guidance, patience and support throughout this project have been a blessing. We would also like to thank our teacher of signal processing Nedelko Grbic for providing such a good project and interesting lectures on signal processing. Finally we would like to thank Mikael Åsman for support and suggestions towards the project.

5

Table of Contents Introduction ...... 7 Chapter 1: DIGITAL FILTERS ...... 8 1.1 Background ...... 8 1.2 FIR filters ...... 9 1.3 Types of windows ...... 10 1.3.1 Rectangular window ...... 10 1.3.2 Bartlett window ...... 11 1.3.3 Hanning window ...... 12 1.3.4 Hamming window...... 13 1.3.5 Blackman window ...... 14 1.4 Design considerations ...... 15 1.5 Observations ...... 16 1.6 IIR filters...... 17 1.6.1 Bilinear transformation ...... 17 1.6.2 Butterworth approximation ...... 17 1.6.3 Chebyshev approximation ...... 18 1.7 Design considerations ...... 19 1.8 Observations ...... 20 Chapter 2 : Developed Software ...... 21 2.1 Digital filter software GUI ...... 21 2.1.1 Filter selection panel ...... 22 2.1.2 Input Selection panel ...... 22 2.1.3 Filter specifications panel ...... 23 2.2 Digital filter analysis ...... 24 2.2.1 Plot panel ...... 24 2.2.2 Pole/zero plot ...... 25 Conclusion ...... 26 Appendix ...... 27 References ...... 29

6

Introduction

Existing problem For analysis of digital filters, DOS based software was developed years back at Blekinge Institute of Technology for students of signal processing. However, this software have drawbacks such as portability, inefficient use of computer resources, less accessibility and bulk hardware system due to floating point DSP which requires extra hardware for floating point operations. The developed software has some constraints as it is not user friendly.

Possible solution With extensive advancements in scientific software, a user friendly graphical user interface was developed using MATLAB graphical user interface for the analysis of digital filters. This GUI will overcome the complexities mentioned earlier. The developed software will help the user to analyze the filters in an efficient manner due to the availability of input , windows, various types of filters such as lowpass, highpass, bandpass, and bandstop filters, and pole/zero plot with filter coefficients.

7

Chapter 1: Digital Filters

1.1 Background The term ‘filter’ is frequently used in signal processing. A filter is a frequency selective device that removes unwanted information from the original message signal. Unwanted signals can be noise or other undesired information. Digital filters are more versatile when compared to the analog filters in their characteristics such as programming flexibility, ability to handle both low as well as high frequency signals accurately. Also the hardware requirement is relatively simple and compact. In real world signals are analog in nature. A simple signal flow block diagram that explains how the signal is processed to acquire desired output signal is shown in figure 1.1.

ADC PROCESSOR DAC unfiltered sampled digitally filtered analog digitised filtered signal signal signal

Figure 1.1: Signal flow block diagram.

Analog to digital conversion is an engineering process that enables digital processor to interact with real world signals. The input to the processor should be properly sampled and quantized. Sampling and quantization restrict the amount of information a digital signal contain. In the figure 1.1 an interface is provided between analog signal and the called analog to digital converter (ADC). The output from ADC is input to the processor. In applications output from the processor is to be given to user in analog form such as speech communications, for this an interface is provided from digital domain to the analog domain. This interface is called digital to analog converter (DAC). Thus the signal is provided in analog for to the user as shown in figure 1.1. The processor in figure 1.1 can be anywhere from a large programmable digital computer to a small microprocessor which contains digital filters.

8

The digital filters are two types based on their ; (FIR) and infinite impulse response (IIR) filters. FIR filters have same time delay for all frequencies (), relatively insensitive to quantization and are always stable. FIR filters can be designed in different ways, for example window method, frequency sampling method, weighted least squares method, minimax method and equiripple method. Out of these methods, the window technique is most conventional method for designing FIR filters.

1.2 FIR filters A finite impulse response filter of length with input and output is described by the difference equation

= + − + ⋯ + − +

= − where is the set of filter coefficients. The of this filter in domain can be represented as

= A window in filter design provides trade off between resolution that is the width of the peak and spectral leakage that is the amplitude of the tails of desired impulse response. The desired specification for linear phase filter is the Fourier transform of the desired impulse response, and this can be represented as

= and the inverse as

= where is the desired frequency response and is the corresponding impulse response. As is infinite duration, the sample response must be truncated. Truncation is 9

performed by multiplying desired sample response with a in time domain which gives sample response of filter represented as

= where is a window function. Various types of windows were used when designing the FIR filters.

1.3 Types of Windows

1.3.1 Rectangular window The rectangular window has excellent resolution characteristics for signals of comparable strength. The rectangular window is defined as

, = 0,1,2, … − 1 = , ℎ The frequency response of the window function is the Fourier transform which, is defined as

= The amplitude response of the rectangular window function is

/ = , − ≤ ≤ / and the phase response is

− − , ≥ = − − + , < 0 The actual impulse response can be expressed in as convolution which leads to of . As increases, becomes narrower, thereby reducing the smoothing effect. In figure 1.2 it is observed that as increases, the main lobe becomes narrower. However, the amplitude of the side lobes is unaffected. The frequency response of a lowpass FIR filter designed using rectangular window is shown in figure 1.3 with for different window lengths, where cutoff frequency is the characteristic frequency which determines the type of the filter.

10

0

-50 Magnitude(dB) -100

M=9 M=15 M=21 -150 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 Normalized frequency

Figure 1.2: Frequency response for Rectangular window.

20

0

-20

-40

-60

-80 Magnitude(dB)

-100

-120 M=9 M=15 -140 M=21

0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 Normalized frequency

Figure 1.3: Lowpass FIR filter designed with Rectangular Window.

1.3.2 Bartlett window A Bartlett window is a triangular shaped window function. The Bartlett window has higher side lobe attenuation than the rectangular window. The Bartlett window is defined as

− − = − − The frequency response for Bartlett window is shown in figure 1.4 and figure 1.5 shows the frequency response of a lowpass FIR filter designed using Bartlett window. 11

0

-50 Magnitude(dB) -100

M=9 M=15 M=21 -150 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 Normalized frequency

Figure 1.4: Frequency response for Bartlett window.

20

0

-20

-40

-60

-80 Magnitude(dB)

-100

-120 M=9 M=15 -140 M=21

0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 Normalized frequency

Figure 1.5: Lowpass FIR filter designed with Bartlett Window.

1.3.3 Hanning window The Hanning window is a raised cosine window and can be used to reduce the side lobes while preserving a good frequency resolution compared to the rectangular window. It is commonly used as general purpose window for the analysis of continuous signals. The Hanning window is defined as

= . − −

12

The frequency response for Hanning window is shown in figure 1.6 and figure 1.7 shows the frequency response of a lowpass FIR filter designed using Hanning window.

0

-50 Magnitude(dB) -100

M=9 M=15 M=21 -150 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 Normalized frequency

Figure 1.6: Frequency response for Hanning window.

20

0

-20

-40

-60

-80 Magnitude(dB)

-100

-120 M=9 M=15 -140 M=21

0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 Normalized frequency

Figure 1.7: Lowpass FIR filter designed with Hanning Window.

1.3.4 Hamming window The Hamming window is, like the Hanning window, also a raised cosine window. The Hamming window exhibits similar characteristics to the Hanning window but further suppress the first side lobe. The Hamming window is defined as

= . − . −

13

The frequency response for Hamming window is shown in figure 1.8 and figure 1.9 shows the frequency response of a lowpass FIR filter designed using Hamming window.

0

-50 Magnitude(dB) -100

M=9 M=15 M=21 -150 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 Normalized frequency

Figure 1.8: Frequency response for Hamming window.

20

0

-20

-40

-60

-80 Magnitude(dB)

-100

-120 M=9 M=15 -140 M=21

0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 Normalized frequency

Figure 1.9: Lowpass FIR filter designed with Hamming Window.

1.3.5 Blackman window The Blackman window is similar to the Hanning and the Hamming windows. An advantage with the Blackman window over other windows is that it has better stopband attenuation and with less passband ripple. The Blackman window is defined as

14

= . − . + . − − The frequency response for Blackman window is shown in figure 1.10 and figure 1.11 shows the frequency response of a lowpass FIR filter designed using Blackman window.

0

-50 Magnitude(dB) -100

M=9 M=15 M=21 -150 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 Normalized frequency

Figure 1.10: Frequency response for Blackman window.

20

0

-20

-40

-60

-80 Magnitude(dB)

-100

-120 M=9 M=15 -140 M=21

0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 Normalized frequency

Figure 1.11: Lowpass FIR filter designed with Blackman Window.

1.4 Design considerations A design consideration when designing digital FIR filter is selecting a window. This can be done with the help of frequency specifications of the required filter. In general, the frequency

15

specification consists of pass and stopband cutoff frequencies and attenuations. The length of the filter can be determined by the main lobe width.

Table 1 shows the side lobe attenuation and main lobe width for different windows. Table 2 shows the desired impulse response functions for various filters. Specifically, stopband attenuation provides for a user to select an appropriate window.

Window Side lobe attenuation Approximate Main lobe width Rectangular -20dB Bartlett -27dB 4 Hanning -40dB 8 Hamming -50dB 8 Blackman -70dB 8 12

Table 1: Comparison of main lobe width and side lobe attenuation for different window types.

Filter Type Desired impulse response

− 1 Lowpass sin − 2 ∙ − 1 − 2

− 1 Highpass sin − − 1 2 − − ∙ 2 − 1 − 2

− 1 Bandpass sin − − 1 2 2 cos − ∙ ∙ 2 − 1 − 2

− 1 Bandstop sin − − 1 − 1 2 − − 2 cos − ∙ ∙ 2 2 − 1 − 2

Table 2: Desired impulse responses for filter types.

1.5 Observations From the window frequency response plots shown in the Figures 1.2, 1.4, 1.6, 1.8 and 1.10, one can observe that as M increases, the main lobe becomes narrower, side lobe amplitudes remain unaffected but width of the sidelobes decreases. The rectangular window provides less width in mainlobe and higher sidelobes in contrast with other windows. Using the window function the ringing effects at the band edges vanishes which results in lower

16

sidelobes, thereby increase in the width of the transition band of the lowpass FIR filter as shown in Figures 1.3, 1.5, 1.7, 1.9 and 1.11.

1.6 IIR filters Impulse response functions of IIR filters are non-zero over an infinite length of time. IIR filters can be described using a difference equation as

= − − − where and are the filter coefficients. The transfer function of IIR filters can be expressed as

= + These filters can be designed by the bilinear transformation method. These filters are designed using their analog counterparts rather than discrete time analysis.

1.6.1 Bilinear transformation The bilinear transformation method is commonly used in designing digital IIR filters to obtain filter coefficients. As mentioned in section 1.6, digital filters are designed with their analog counterparts, so it must be transformed into discrete time domain. This transformation can be done with the .

− = + 1.6.2 Butterworth approximation The amplitude response of a is given as

Ω = Ω Ω + where is order of the filter, Ω is the passband frequency, Ω is the analog frequency of the filter specifications, and is the maximum value of the amplitude function.

17

Figure 1.12

Ω is the stopband frequency shown in the figure 1.12.The filter order can be approximated as

− /

= Ω Ω where is the filter parameter which determines the band edge value of Ω and is the maximum allowed value for the amplitude function at the stopband edge.

Ω = The system transfer function in polynomial form is given by

= Ω Ω Ω + + ⋯ + + The Butterworth filter coefficients are provided in appendix table A. 1.6.3 Chebyshev approximation The amplitude response of a is given by

Ω = Ω Ω +

18

Figure 1.13

Figure 1.13 gives us the specifications for order of the Chebyshev filter, where the ripple is defined as and is the maximum value of the amplitude Ω = ∙ + dB function, is the Chebyshev polynomial. The required filter order can be Ω approximated as

− = Ω Ω where is the filter parameter related to ripple in the passband and is the maximum allowed stopband ripple. The system transfer function in polynomial form is given by

∙ ∙ + = Ω Ω + + ⋯ + The Chebyshev filter coefficients are provided in appendix tables B1, B2, B3 and B4. 1.7 Design considerations Designing of IIR filters are tricky when compared with FIR filters, here we always consider a lowpass filter and then transforms the desired filter to and from this lowpass filter. A 19

technique called “pre-warping” is used to map digital frequencies into analog frequencies. The warping frequencies are given by

Ω = Ω = where angular frequencies of pass and stop bands of digital filters. After “pre warping” frequencyand transformation is needed for highpass, bandpass and bandelimination filters shown in table 3.

Filter Frequency Transformation Highpass and = = Bandpass and , Bandelimination = Ω − Ω = Ω − Ω Ω ∙ Ω = Ω ∙ Ω = Ω and , = = Ω ∙ Ω = Ω ∙ Ω = Ω

Table 3: Transform to frequencies of lowpass filter.

After the frequency transformation, the designed lowpass filter is transformed into the desired filter type according to the table 4.

LP HP BP BS

Ω Ω + Ω +

Table 4: Transform back to original filter (HP, BP, BS) from lowpass filter.

1.8 Observations Implementing an IIR filter with certain stopband-attenuation and transition-band requirements typically requires far fewer filter taps than an FIR filter meeting the same specifications. This leads to a significant reduction in the computational complexity required to achieve a given frequency response. However, filter requires feedback to implement an IIR system, which introduces stability issues. In addition, the filter is of nonlinear phase.

20

Chapter 2: Developed software

2.1 Digital Filter Software GUI A digital filter software GUI is shown in figure 2.1 for design and analysis of digital filters. The developed filter software GUI consists of the following parts.

 Filter selection panel

 Input selection panel

 Filter specifications panel

 A panel for different plots for analysis

 A button to display the pole/zero

 A button to exit the GUI

Figure 2.1: A simple GUI for design and analysis of digital filters.

21

2.1.1 Filter Selection Panel The user can choose to design and analyze a FIR or IIR digital filter.

2.1.2 Input Selection Panel The input selection panel consists of settings for the input signal to the digital filter. These specifications are input signal type, sampling rate and frequency of the signal which are essential to the analysis of the digital filters.

I. Input In the Input dropdown list, different types of input signal types can be selected. The signal types are sine wave, square wave, sawtooth wave and white Gaussian noise. The different signal types are shown in figure 2.2.

Figure 2.2: The different signal types used in the analysis of digital filters.

22

II. Sampling Frequency The sampling rate is the rate at which the input signal for the filter is sampled. For perfect reconstruction of a signal the sampling rate should be greater than twice the maximum frequency of the signal being sampled.

III. Frequency The frequency determines the number of cycles per unit time of a signal. As the frequency increases the number of cycles of the input wave also increases. The range of the frequency is from to which can be adjusted with the help of a slider and edit box where the user can0 directly − 1enter the desired frequency.

2.1.3 Filter specifications panel

I. Filter Type The user can create lowpass, highpass, bandpass and bandstop. These filters types can be selected from the dropdown list in the Input selection panel.

II. Filter Order The filter order mainly determines the width of the transition band. The higher the order, the narrower is the transition between the passband and stopband, giving a sharper cutoff in the frequency response. This can be clearly observed in figure 2.3 for orders of 9, 15 and 31.The transition bandwidth can be improved by increasing the order of the filter. The filter order can be adjusted between 0 to 31. The filter order can be adjusted by setting the slider or entering the value in edit box.

Figure 2.3: Plot for different filter orders.

23

III. Cut off frequencies Cutoff frequency is the frequency where the filter is designed to cross over from the passband to the stopband. The cutoff frequency can be adjusted by setting the values of sliders or entering the values in the edit box. The cutoff frequencies are in the range from to . 0 − 1 A single cutoff frequency is available for lowpass and highpass filters where as for bandpass2 and bandstop two cutoff frequencies are available.

2.2 Digital filter analysis After designing a digital filter, the filters can be analyzed by selecting different input signals and examining the filter and filter response in different plots.

2.2.1 Plot Panel The plot panel consists of three dropdown lists. The dropdown lists presents options for displaying input and output signals in time domain, linear and logarithmic FFT plots, and linear and logarithmic filter amplitude responses.

Dropdown list  Time x: shows the input signal in time domain  Time y: shows the filtered signal in time domain  Time x+y : shows the input and output on top of each other in time domain  FFT linear x: shows the amplitude of the FFT of the input signal in linear scale.  FFT linear y: shows the amplitude of the FFT of the output signal in linear scale.  FFT linear x+y : shows the amplitude of the FFT of the input and output signal on top of each other in linear scale.  FFT logarithmic x: shows the amplitude of the FFT of the input signal in logarithmic scale.  FFT logarithmic y: shows the amplitude of the FFT of the output signal in logarithmic scale.  FFT logarithmic x+y : shows the amplitude of the FFT of the input and output signal on top of each other in logarithmic scale.  FFT linear h: shows the FFT of impulse response in linear scale.  FFT logarithmic h: shows the FFT of impulse response in logarithmic scale.

The FFT is a standard method for decomposition of a signal into harmonic components. It is the faster version of the Discrete Fourier Transform (DFT). FFT generates the frequency spectrum for a time domain waveform. The FFT of the impulse response, referred to as the

24

frequency response function, completely characterizes the system. Once the system frequency response is known, one can predict how that system will react to any waveform.

2.2.2 Pole/zero Plot A pole/zero plot is the graphical representation of the transfer function in the complex Z plane which helps to convey properties of the system such as stability, causality, minimum phase and region of convergence.

A linear time invariant system is said to be minimum phase if the system and its inverse are stable and casual. For a system to be minimum phase all its poles and zeros must be inside the unit circle. The user can determine whether a digital filter is stable that is output of digital filter is bounded for all possible bounded inputs. It is known that for IIR filters to be stable is that all its poles lie inside unit circle where as for FIR filters all its poles lie inside unit circle.

Figure 2.4: A simple pole/zero plot.

A pole/zero plot GUI is shown in figure 2.4. It contains buttons to add, remove and move filter coefficients and a plot of poles and zeros. The pole/zero plot GUI also contains clear, restore and update filter coefficients in the main GUI window. In the pole/zero GUI the user can also pan and zoom the pole/zero plot.

25

Conclusion

This Master thesis report presents a software implementation of digital filters using Matlab GUI in a user friendly environment. The developed software is useful for aspirant students in designing and analysis of the digital filters. The software consists of radio buttons, pop-up menus, sliders, edit boxes, axes, push buttons and list boxes all these are placed in different panels and all are working properly. The evaluated performance using the specific theoretical filter parameters match the performance with the software. The software is working properly towards accurate results upon manual testing.

26

Appendix

Table A: Coefficients in Butterworth polynomials.

2 2 2 2.613 3.141 2.613 3.236 5.236 5.236 3.236 3.864 7.464 9.141 7.464 3.864 4.494 10 .103 14 .606 14 .606 10 .103 4.494 5.126 13 .138 21 .848 25 .691 21 .848 13 .138 5.126

Table B1: Chebyshev filter coefficients . . 0.5 = 0.349, = 0.122 2.863 1.516 1.426 0.716 1.535 1.253 0.379 1.025 1.717 1.197 0.179 0.752 1.309 1.937 1.172 0.095 0.432 1.172 1.589 2.172 1.159 0.045 0.282 0.756 1.648 1.869 2.413 1.151 0.024 0.152 0.573 1.148 2.184 2.149 2.657 1.146

Table B2: Chebyshev filter coefficients . . 1 = 0.509, = 0.259 1.965 1.102 1.098 0.491 1.238 0.989 0.276 0.743 1.454 0.953 0.123 0.580 0.974 1.689 0.937 0.069 0.307 0.939 1.202 1.931 0.928 0.031 0.214 0.549 1.357 1.429 2.176 0.923 0.017 0.107 0.448 0.447 1.837 1.655 2.423 0.920

27

Table B3: Chebyshev filter coefficients . . 2 = 0.765, = 0.585 1.307 0.823 0.804 0.327 1.022 0.738 0.206 0.517 1.256 0.716 0.082 0.459 0.693 1.499 0.705 0.051 0.210 0.771 0.867 1.745 0.701 0.020 0.166 0.383 1.144 1.039 1.994 0.698 0.013 0.073 0.359 0.598 1.579 1.212 2.242 0.696

Table B4: Chebyshev filter coefficients . . 3 = 0.998, = 0.995 1.002 0.708 0.645 0.251 0.928 0.597 0.177 0.405 1.169 0.581 0.063 0.408 0.549 1.415 0.575 0.044 0.163 0.699 0.691 1.663 0.571 0.016 0.146 0.300 1.052 0.831 1.911 0.568 0.011 0.056 0.321 0.472 1.467 0.972 2.161 0.567

28

References

Proakis, J.G. and Manolakis, D.G., Digital signal processing: principles, algorithms and applications , Third Edition,(1996), ISBN -81-203-1129-9. Signal Processing 2 Lecture Notes by Nedelko.G and Collection of formulas by M.Dahl, S.Johansson, M.Winberg (Department of Telecommunication and Signal Processing, BTH). Stephen J. Chapman, MATLAB® Programming for Engineers, Third Edition,(2005), ISBN:981-254-893-9. Vinay K. Ingle, John G. Proakis, Digital Signal Processing Using MATLAB®, (2003),Fourth Reprint, ISBN:981-240-215-2. Released notes from matlab.

29