Breaking

Be Strong Let's Try!

Wednesday, 18 August 2021

What is Convolution And How It Work In MATLAB 2021

What is Convolution And How It Work In MATLAB 2021 

What is The Objective Of This Topic:

 To study linear convolution with and without using built in function.

What Is The  Description:

 The MATLAB Signal Processing Toolbox is required. This section will contain material to help you  successfully conduct the learn and have some experiment. It may contain the list of experimental steps that you will have to go through for successful experimentation. It can guide you on common pitfalls and points to note. 

What Is Linear Convolution: 

Convolution is an operation between the input signal to a system, and its impulse response, resulting in the output signal. In discrete time, convolution of two signals involves summing the product of the two signals, where one of the signals is “flipped and shifted”. It doesn't matter which signal is flipped and shifted. Have to take care to get limits of sum correct. Convolution Sum is given by:



Let’s try to understand the concept of convolution sum through an example. Suppose we have a Linear Time Invariant (LTI) System having an impulse response h[n] and an input signal x[n] is applied to the system. We will have a look at each step involved in the convolution of these signals. The input signal x[n] is given by:





Step I:- Graph x[n] and h[n] as function of k.


Step II:- Determine h[n-k] with the smallest n that prevents any overlap between x[k] and h[n-k]. First we reflect h[k] about k=0 to obtain h[-k].


Note that x-axis has been extended on the left to include a few additional points. These will be useful later. Second we shift h[-k] by –n. This result in h[n-k] and is equivalent to shifting h[-k] towards the left side by n. We begin with large and negative value of n such that there is no overlap between the non-zero values of the two signals.



Step III A:- Increase n until x[k] and h[n-k] overlaps. We choose the smallest value on n such that x[k] and h[n-k] start overlapping. In this case the overlap starts at n=-5. The output y[n] is zero for n<- 5


Step III B:- Calculate output y[n] from the overlapping region. We multiply the overlapping values of x[k] and h[n-k] and add the results. In this case there is only one overlap occurring at k=-2. The product is therefore 8(1)=8. The output y[n] is 8 for n=-5 and is shown in figure below.

Increment n by 1. Repeat Step 3B until h[n-k] slides past x[k]. 


Step IV:- In step 4 we increment n by 1. This is equivalent to shifting h[n-k] toward the right-hand side by 1. 

Still h[n-k] is overlapping with x[n] we repeat step III B. We multiply the overlapping values of x[k] and h[n-k] and add the results. In this case there are 2 overlaps occurring at k=-2 and k=-1. The sum of product is therefore 4(1)+8(1)=12. The output y[n] is 12 for n=-4 and is shown in the figure below:
 
And so on…. Since h[n-k] do not overlap with x[n] then the convolution sum is now 0 for n>3. Final Result

What Is Moving Average:

 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:

Some of the task on MATLAB for the practice lets start:
01: a) Write a MATLAB function which takes two signals x[n] and h[n] as parameters and perform the convolution sum of the two signals.
b) Using MATLAB built-in function of convolution to perform the convolution sum of two same signals and compare the results to the result of  task 1. The result of convolution should be same. MATLAB function for convolution is conv. 

PART (a) and PART (b)

CODE:

Function :

function [y] = Convolution (x,n1,h,n2)

x1 = [zeros(1,length(h)-1) x zeros(1,length(h)-1)];

h1 = [fliplr(h) zeros(1,length(x)+length(h)-2)];

n_start = n1(1)+n2(1);

n_end = n1(end) + n2(end);

nn=n_start:n_end;

count = 1;

for d = n_start:n_end

    y(count) = sum(x1.*h1);

    h1 = circshift(h1,[0,1]);

    count = count + 1;   

end

Script :
n1 = -4:4 ;

x =[0.5 0.5 0 2 1 2 0 0.5 0.5];

n2 = -1:3;

h=[1 0 1 2 1];

y = Convolution(x,n1,h,n2)

 

subplot(2,2,1)

stem(n1,x);

title('x[n]')

subplot(2,2,2)

stem(n2,h);

title('h[n]')

subplot(2,2,3)

stem(y);

title('y[n] using formula ')

subplot(2,2,4)

dd=conv(x,h)

stem (dd)

title('y[n] using conv')
 

OUTPUT:



02: a) Write MATLAB code to apply moving averaging filter on a noisy signal using moving averaging equation
b) Apply Moving Averaging on the same signal using convolution. 

PART (a) and PART (b)

CODE:
x=[1 7 1 4 4 7 1]

M=3

k=ones(1,M)

k=k/M

y1=conv(x,k,'same')

y2=zeros(1,N)

for i=1:N

for j=-1:M-2

    if i+j<=N && i+j>0

     y2(i)=y2(i)+x(j+i)

    end

end

end

y2=(1/M).*y2;

subplot(3,1,1)

stem(x);

title('Input signal x(n)');

subplot(3,1,2)

stem(y1);

title('Output Signal y(n) by Equation');

subplot(3,1,3)

stem(y2);

title('Output Signal y(n) by Convulotion');

 

OUTPUT:

 




Thanks For Visiting!



No comments:

Post a Comment

Pages