12
1 Tharindu De Silva s3140219 RMIT University School of Electrical and Computer Engineering EEET2369 Signals and Systems Lab Experiment #4 Implementing FIR filters in MATLAB Lecturer: Dr. Katrina Neville Tutor: Refat Kibria Student Name: Tharindu De Silva Student Number: S3140219 Group: Monday 13:30-15:30 Submission Due Date: 7 th May 2012

EEET2369_Lab4_3140219

Embed Size (px)

Citation preview

Page 1: EEET2369_Lab4_3140219

1 Tharindu De Silva s3140219

RMIT University

School of Electrical and Computer Engineering

EEET2369 – Signals and Systems

Lab Experiment #4

Implementing FIR filters in MATLAB

Lecturer: Dr. Katrina Neville

Tutor: Refat Kibria

Student Name: Tharindu De Silva

Student Number: S3140219

Group: Monday 13:30-15:30

Submission Due Date: 7th

May 2012

Page 2: EEET2369_Lab4_3140219

2 Tharindu De Silva s3140219

Introduction

Analogue signals are sampled and quantized to transform into the digital or discrete domain.

These samples are stored as digitized data which represents the original waveform. Discrete-time

filtering systems can then be applied on these discrete-time domain signals in order to apply

different visual and sonic effects. Filters are systems used to change an input signals

characteristics. Finite impulse response (FIR) filters are types of filters that are used in digital

signal processing to manipulate a digitized signal.

FIR filters are defined as systems that produce an output as a sum of a finite number of weighted

samples of an input signal [1]. An FIR filter can be mathematically represented as;

Where bk is the filter coefficient.

Another important form of representing a FIR filter is through its impulse response where an unit

impulse sequence is regarded as the input of the system [1]. In order to derive the output of a FIR

filter, we use the process of convolution in the time domain. This is a mathematical process

where the output is derived from convolving the input signal x[n] with the system’s impulse

response h[n].

FIR filters can be designed to give many useful characteristics to an input signal. Common types

of signals that can be used in this case are audio and image signals. In the practices of audio and

image processing, FIR filters can be used to add different kinds of effects.

In this laboratory exercise, basic design and implementation of FIR filters will be discussed. In

the final section magnitude and phase responses of FIR filters will be analysed. MATLAB will be

used to design and implement FIR filters on audio and image files. MATLAB is a very useful

platform to digitally design and test FIR filters on digital signals. MATLAB provides some very

interesting functions in its signal processing toolbox that enables the user to define impulse

responses to design filters and to apply these onto signals using convolution. As seen in the

previous laboratory exercise, being able to load audio and image files onto MATLAB and

manipulate these signals gives a better feel about the technique being analysed. Both sonic and

visual perception will be used to observe the effects to understand filtering in a perceptive point

of view. Two simple types of filters, First difference FIR filter and a running average FIR filter,

will be designed, implemented, and analysed to understand the system’s characteristics.

Frequency response analysis of these FIR filter designs will be performed to confirm, what

students perceive with what is actually happening in the frequency domain.

This exercise will enable students to have a basic understanding of FIR filters, their frequency

response and their practical applications.

Page 3: EEET2369_Lab4_3140219

3 Tharindu De Silva s3140219

Task 1

A sample speech audio file is loaded on to MATLAB and plotted on both time and frequency

domains. A time vector of the length of the .wav file was needed to be generated and it was done

using simple logic used in previous laboratory tasks (See appendix for code).

Figure 1

Task 2

An impulse response vector is created in MATLAB for a given FIR filter whose impulse response

is given by ;

h[n] = δ[n] + 1/2δ[n-4000] + 1/4δ[n-8000] .

each δ term has a gain and delay. The 1st term has a gain of 1 and there’s no delay and the 2nd

term has a gain of 0.5 and a delay of 4000 and so on.

MATLAB’s stem() function is used to plot this filter. First, each Impulse response needed to be

defined in the ‘h(delay) = gain’ form (see appendix for code).

Thus for our filter, the impulse response vector will be h(1) = 1, h(4000) = 0.5 and h(8000) =

0.25. The Impulse response plot for this FIR filter is shown in figure 2.

Page 4: EEET2369_Lab4_3140219

4 Tharindu De Silva s3140219

Figure 2

Task 3

This FIR filter is applied to the .wav file and soundsc() function is used to listened to the filtered

audio file.

Figure 3

Page 5: EEET2369_Lab4_3140219

5 Tharindu De Silva s3140219

We use convolution to apply the filter onto the audio file. Figure 3 shows the time domain and

frequency domain plots for the filtered signal.

When we listen to the filtered signal, we can hear the echo phenomena occurring. Since our

impulse response had delay terms, segments of the input signal is delayed when passed through

the filter. In the convolution process, the input data is passed through the delay paths of the filter

and we can hear the echo effect.

We can define the specific gains corresponding to these delay paths. This means we can control

how much of delay we want and how many times we want the delay to occur. This is a very

interesting phenomena for sound production where the sound engineer can design and implement

FIR filters to add customized echo and reverb effects to audio signals.

Convolution in the time domain is multiplication in the frequency domain. In the frequency plot,

we see that the magnitude has doubled. This observation proves the above theory.

Task 4

In this task we apply a first difference FIR filter to .png image file. First we define the impulse

response vector for this filter. The conv2() function is used to apply this filter onto the image. The

filtered image is shown in figure 4.

Figure 4

A first difference filter is given by the equation y[n] = x[n] – x[n-1] with the frequency response

of 1-cosώ + jsinώ. Magnitude of this system will be 1-cosώ. When we obtain the magnitude plot

from this, we can see that the filter supresses the low frequency components of the input signal

[1]. Thus this FIR filter acts as a high pass filter.

Page 6: EEET2369_Lab4_3140219

6 Tharindu De Silva s3140219

The high spatial frequency areas in the original image are shown in figure 5.

Figure 5

In figure 4, we see that the high frequency components in the original image are let through while

the low frequency components are suppressed. This proves our hypothesis that the first difference

FIR filter is a high pass filter.

Task 5

We created a 5 point running average FIR filter and apply this onto the same image file used in

the previous task. The defined impulse response vector is shown as;

runavh = (1/5)*ones(1,5)

Again we use the conv2() function to filter the image. The filtered image is shown in figure 6.

An L point running average filter can be represented as

When we plot the magnitude response for this filter we can see that all the high frequency

components are suppressed [1]. Thus we can say that this filter acts as a low pass filter. Again

referring back to figure 4 we can note that all the high spatial frequency areas have been blotted

out; high frequency components haven’t been let through by our filter. This proves our hypothesis

that the running average filter is indeed a low pass filter.

Page 7: EEET2369_Lab4_3140219

7 Tharindu De Silva s3140219

Figure 6

When the length of the filter is increased, more high spatial frequency components are cut out.

Thus the quality decreases. This point was confirmed using an 11 point running average FIR filter

defined by runavh = (1/11)*ones(1,11). Likewise, when the length is decreased, the quality

increases.

Task 6

The frequency response of the above FIR filters are plotted in MATLAB using the freqs()

function. The syntax for the function is as follows.

Freqs(num, den) ; whereas num and den are the numerator and the denominator coefficients in

the transfer function for the filter.

For the ‘num’ parameter, we can use the impulse response vector. For FIR filters, the

denominator is always 1. Magnitude and the phase plots for the first difference FIR filter is

shown in figure 7.

Page 8: EEET2369_Lab4_3140219

8 Tharindu De Silva s3140219

Figure 7

On the magnitude plot, we see that the filter only allows the high frequency components to pass

through. This plot confirms the hypothesis that the first difference FIR filter acts as a high pass

filter.

When analysing the running average filters, we use two different filters to better understand how

they behave. Frequency plots for 11 point and 5 point running average filters are shown by

figures 8 and 9 respectively.

Figure 8

11 point running average filter

Page 9: EEET2369_Lab4_3140219

9 Tharindu De Silva s3140219

Figure 9

5 point running average filter

Comparing figures 8 and 9, we can clearly see that when the length of the filter increases, more

high frequency components are supressed (note the curves). This feature explains why the quality

of the image deteriorated when we increased the length of the running average filter.

Even this filter can be used on this simple image file to filter frequencies, the running average

filter is a poor low pass filter due to its slow roll off and stop band attenuation [2].

Page 10: EEET2369_Lab4_3140219

10 Tharindu De Silva s3140219

Conclusion

FIR filters can be designed and implemented to give echo and reverberation effects to audio

and blurring and sharpening effects to images. These filters can be designed to acts as high/low

pass filters in filtering out certain frequency components from a signal, changing its

characteristics, thus attributing visual and sonic effects. This practical application has given FIR

filters a profound importance in the field of signal processing and sound engineering. In Audio

recording and processing software like Adobe Audition, echo, frequency band splitter and

graphic equalizer effects are realized with the use of FIR filters [3].

First difference and running average filters are examples of high and low pass filters respectively.

These types of filters can be used in applications where the input signal needs to attenuated in

regards to its frequency. We can observe the frequency response of a FIR system and determine

whether the filter would act as a high or low pass filter. This is an important fact when it comes to

the design of such filters.

MATLAB provides some useful tools to help engineers design FIR filters. Filter coefficients can

be defined and applied to waveform using convolution functions. Furthermore MATLAB provide

tools to analyse frequency responses of such systems enabling the engineer to evaluate their

design before progressing on to the actual hardware design phase. This process of 2 point

evaluation (hand written and simulated), as we’ve performed in this exercise, is vital when it

comes to hardware design.

Embedded environments like Analogue devices’ Blackfin family microprocessors are designed

for optimal signal processing with more emphasis on audio. Software environments like VDSP++

enables a developer to implement FIR filter systems to process audio on real time systems using

Blackfin processors. Further research can be conducted in implementing FIR filters on processors

optimized for video processing systems with high processing capacities. Firmware could be

developed for such processors to implement FIR filters to use on video signals which can be

beneficial for video and animation designers.

References

1. James H. McClellan, Ronald W. Schafer, Mark A. Yoder, DSP first: A multimedia

approach. New Jersey: Prentice Hall, 1999, chapter 5.

2. Stephen W. Smith, The scientist and engineers guide to digital signal processing. San

Diego, California : California technical publishing, 1997, chapter 15.

3. Adobe systems INC, “Adobe Audition 3 – Help Resource Centre,”

http://help.adobe.com/en_US/Audition/3.0/help.html?content=WS58a04a822e3e5010548

241038980c2c5-7f1c.html

Page 11: EEET2369_Lab4_3140219

11 Tharindu De Silva s3140219

Appendix

Given below is the MATLAB code used for this exercise.

Task 1

[x,fs,bits]=wavread('speech_dft.wav');

N = length(x); Ts = 1/fs; T = Ts * N; t = 0:Ts:T-Ts; % Time vector of length of the audio file

F = fs/N; % Frequency step for FFT display f = (-fs/2):F:(fs/2)-F; % Frequency vector (same length as the time vector) X=fftshift(fft(x))*Ts; % FFT of the signal x

subplot(2,1,1), plot(t, x) subplot(2,1,2); plot(f, abs(X));

Task 2

h(1) = 1; h(4000) = 0.5; h(8000) = 0.25;

stem(h), axis ([-1000 10000 0 2]);

Task 3

N = length(y); Ts = 1/fs; T = Ts * N; t = 0:Ts:T-Ts; % Time vector of length of the audio file

F = fs/N; % Frequency step for FFT display f = (-fs/2):F:(fs/2)-F; % Frequency vector (same length as the time vector) X=fftshift(fft(y))*Ts; % FFT of the signal x

subplot(2,1,1), plot(t, y) subplot(2,1,2); plot(f, abs(X));

Task 4

Data=imread('C:\Users\Dismal\Documents\RMIT\Semester 1, 2012\Signals and

systems\lab 4\barbara.png');

fdiffh = [1, -1]; % impulse response vector

yy = conv2(Data, fdiffh); yy = conv2(yy, fdiffh.'); imshow(yy);

Page 12: EEET2369_Lab4_3140219

12 Tharindu De Silva s3140219

Task 5

Data=imread('C:\Users\Dismal\Documents\RMIT\Semester 1, 2012\Signals and

systems\lab 4\barbara.png');

runavh = (1/5)*ones(1,5);

yy = conv2(Data, runavh); yy = conv2(yy, runavh.');

yy = uint8(yy); imshow(yy);

Task 6

Data=imread('C:\Users\Dismal\Documents\RMIT\Semester 1, 2012\Signals and

systems\lab 4\barbara.png');

runavh = (1/11)*ones(1,11); fdiffh = [1, -1]; freqz(fdiffh,1); % frequency response of the first difference filter freqz(runavh,1); % frequency response of the running average filter