Sei sulla pagina 1di 5

Lab [2]: [Convolution]

Objective(s)
a) To study linear convolution without using built in function and the function

conv in MATLAB

b) To study circular convolution without using built in function in MATLAB

Description
The MATLAB Signal Processing Toolbox is required.

This section will contain material to help students successfully conduct the experiment. It
may contain the list of experimental steps that the students will have to go through for
successful experimentation. It can guide students on common pitfalls and points to note.
Example(s)
An example(s) to make the students understand the concepts better as discussed in the
description above.
Theory:
Moving average is a simple operation used usually to suppress noise of a signal: As the name
implies, the moving average filter operates by averaging a number of points from the input
signal to produce each point in the output signal. In equation form, this is written:

Where x[ ] is the input signal, y[ ] is the output signal, and M is the number
of points in the average. For example, in a 5 point moving average filter, point
80 in the output signal is given by:

As an alternative, the group of points from the input signal can be chosen
symmetrically around the output point:

Moving average by convolution


As you may have recognized, calculating the simple moving average is similar to the
convolution: in both cases a window is slid along the signal and the elements in the window
are summarized. So, give it a try to do the same thing by using convolution. Use the
following parameters:

The desired output is:

As first approach, let us try what we get by convolving the x signal by the following k kernel:

>> x = [1 7 1 4 4 7 1];
>> k = [1 1 1];
>> y = conv(x, k, 'same')
y=
8 9 12 9 15 12 8
The output is exactly three times larger than the expected. It can be also seen, that the output
values are the summary of the three elements in the window. It is because during convolution
the window is slid along, all of the elements in it are multiplied by one and then summarized:

To get the desired values of y, the output shall be divided by 3:


>> x = [1 7 1 4 4 7 1];
>> k = [1 1 1];
>> y = conv(x, k, 'same');
>> y = y / 3
y=
2.6667 3.0000 4.0000 3.0000 5.0000 4.0000 2.6667

By a formula including the division:

But would not it be optimal to do the division during convolution? Here comes the idea by
rearranging the equation:

So we shall use the following k kernel:

In this way we will get the desired output.

Task:
1. Write MATLAB code to apply moving averaging filter on a noisy signal using
moving averaging equation

2. Apply Moving Averaging on the same signal using convolution.


3. Apply both task on a noisy sound wave, observe the difference before and after
applying the filter.
Code:

Task 1:

close all
clear all
clc
x=[1 7 1 4 4 7 1];
M=3;
y=zeros(1,5);
for i=1:5
sum=0;
for j=0:2
sum=sum + x(i+j);
end
y(i)=sum;
end

subplot(3,1,2),stem(y/3),xlim([1 5]),ylim([0 5])


title('Convulation Method')

Task 2:

close all
clear all
clc
x=[1 7 1 4 4 7 1];
k=[1 1 1];
w=conv(x,k,'valid');
subplot(3,1,1),stem(w/3),xlim([1 5]),ylim([0 5])
title ('Summation Method')

Task 3:

Audio_Signal = audioread('D:/Music/file_example.wav');
Audio_Signal = Audio_Signal (:,1);
w2=conv(Audio_Signal,k,'valid');
subplot(4,1,3),stem(w2/3)
title('Sound wave with filter')

Fs=44100;
audiowrite('D:\Music\fileLowPass(4).wav',w2/3,Fs);

Output:

Potrebbero piacerti anche