# ECET 350 Lab 03 Problems 2015

Generating and

Filtering Digital Signals in Matlab

Introduction to Moving Average Filters

Moving averages

are the simplest of all non-recursive or finite impulse response (FIR),

low-pass filters. The term moving means that the average is taken over a moving

or changing with time set of input samples. Also, in a moving average filter,

the filter coefficients are all the same, meaning they are the reciprocal of

the number of input terms to be averaged.

The difference

equation for a FIR filter is.png”>

For example, if

a moving average of length 5 is to be performed, the difference equation

summation may be expanded into the following form.

y[n]=0.2(x[n]+x[n?1]+x[n?2]+x[n?3]+x[n?4])

All the b k

coefficients are equal to the reciprocal of the length, which in this

case, is equal to one fifth, or 0.2.

As with all FIR

filters, this also introduces the need for storing not only the current input

sample but previous or delayed samples, as well. This is usually implemented in

a buffer referred to as a circular buffer. The circular designation means that

when a new sample input is available, the oldest sample in the buffer is always

overwritten by the newest one.

Again, if the

moving average filter length is 5, that means the average is taken of the

latest five samples of the input signal. To illustrate this, suppose we have

the following input sample sequence shown in Table 1.

Sample

Number

First

Second

Third

Fourth

Fifth

Sixth

Seventh

Eighth

Ninth

Sample

Value

3

-2

5

1

4

-3

-1

6

2

Table 1: Input Sample Sequence

Assuming that

all sampling and antialiasing filtering requirements have been met for the

system and assuming that the sampling frequency equals F s, a new

digital sample is provided as an input to the filter once every sampling

period, which is the reciprocal of F s, which is 1/F s=

T s, where T sis the sampling period. In addition, all

input samples prior to the start of the filtering process have been set to a

value of 0.

Table 2 shows

the moving average filter inputs and the calculated results after each new

sample is received. Please note that until the fifth sample is received, the

input signal buffer is in what is called a boundary condition, meaning that not

enough samples have been received to calculate an accurate average yet.

Sample

Number

Filter

Input

1

2

3

4

5

6

7

8

9

x[n]

3

3

3

3

3

-3

-3

-3

-3

x[n -1]

0

-2

-2

-2

-2

-2

-1

-1

-1

x[n -2]

0

0

5

5

5

5

5

6

6

x[n -3]

0

0

0

1

1

1

1

1

2

x[n -4]

0

0

0

0

4

4

4

4

4

Mov

Avg

0.6

0.2

1.2

1.4

2.2

1.0

1.2

1.4

1.6

Table 2: Moving Average Samples and Filter Calculations

When sample 1

is received, it is stored in the x[n]element of the input buffer while

the remaining elements still contain their initialized values of zero. When

sample 2 is received, it is stored in the x[n-1]element. When sample 3

is received, it is stored in the x[n-2]element, and so on until the

sixth sample is received. At this point, the buffer is completely filled with

the five latest samples when the sixth arrives; the oldest sample in the

buffer, which at this time is the x[n]sample, is overwritten with the

newest sample.

When sample 7

is received, it is used to overwrite the next oldest sample, which is the x[n-1]

element. This process continues with the newest sample overwriting the

oldest until the filter is turned off. The calculated moving average value for

each of the input sequences is shown in the last row of Table 2.

This simplified

version works because all the filter coefficients in a moving average filter

are the same. In most digital filters, the coefficients are not identical and a

more sophisticated scheme is required to match the input sample sequence with

the proper filter coefficient, but that will be covered in a later lecture and

lab.

Generating a discrete or sampled sine

wave in Matlab

a. To generate a sine wave in Matlab,

sampling frequency (or rate), the number of samples, the frequency, amplitude

and phase of the sine wave must be defined.

fs=8000; %sampling rate

Ts=1/fs; %sampling

period

n=0:1:249; %indices

(250 samples will be generated)

frequency=500; %500 Hz

frequency

A=sqrt(2); %Amplitude

x=A*sin(2*pi*500*n*Ts) %Sine wave

stem(n,x); %Plot

xlabel(‘Number of

samples’);ylabel(‘Sample value’);grid;

b. Add noise to the sine wave

v=sqrt(0.1)*randn(1,250); % generate Gaussian random noise

x_noise=x+v; %Add noise

to sine wave

stem(n,x_noise); %Plot

xlabel(‘Number of samples’);ylabel(‘Sample value’);grid;

Compare the clean and noisy versions of the

signal and record your observations. What is the frequency original (clean)

sinusoidal signal?

c. Letâ€™s

look at the spectra (frequency content) of the clean signal.

N=length(x);

f=[0:N/2]*fs/N;

Axk=2*abs(fft(x))/N;

Axk(1)=Axk(1)/2; % calculate single side spectrum

for x(n)

plot(f,Axk(1:N/2+1));

xlabel(‘Frequency

(Hz)’);

ylabel(‘Amplitude |X(f)| ‘);grid;

Using the above code snippet, can you plot the frequency

spectrum of the noisy signal?

What are the differences between the two sets

of spectra? What is the apparent frequency of noise?

d. Now we will use a moving average filter to remove

the noise using the filter function from Matlab.

a=1;

win_size=4; %Window size=4

b=(1/win_size)*ones(1,win_size);

y=filter(b,a,x_noise);

Plot the output signal y using the code snippets from

part a. How does the filtered signal compare to the noisy signal? Label your

graph.

e. Calculate the root mean square error

(rmse):

The filtered signal (y) should be close to the actual

signal (x). Root mean square error allows us to measure how far the error

between the two signals is from zero. Visit.stanford.edu/~susan/courses/s60/split/node60.html”>http://statweb.stanford.edu/~susan/courses/s60/split/node60.html for more details.

Use the following command to find the

rmse=

sqrt(sum((x-y).^2)/length(y))

Repeat the filter function with window sizes of 3, 5 and

7 and calculate the rmse values.

Window size

RMSE

3

4

5

7

Which window size gives you the least rmse?

**30%**with the discount code: ESSAYHELP