1. Write a Matlab convolution function that generates the discrete-time filter o
ID: 1766012 • Letter: 1
Question
1. Write a Matlab convolution function that generates the discrete-time filter output y[n] given a discrete- time impulse response h[n] and a discrete-time input sequence r[n], according to Equation 1. Assume that the filter is a causal FIR filter, and thus h[n] is non-zero only for n = 0 to M. Include your Matlab convolution function in your lab report Note: Remember that vectors in Matlab cannot have negative or zero-valued indices. Therefore, make sure the index numbering of your Matlab convolution function starts from 1, not 0; when the convolution sum is using h[O] or r[0], those must be the first element of your h or r vector, h(1) or (1) Similarly, note that n - k can be negative, when k takes on a value where k> n. Again, you must map your indexes correctly, and if r-2] doesn't exist in your vector, for example, then your program should not try to calculate that part of the sum. For example, if M = 2, y[0] = h[0]x[0] + h[1]+1] + h[2]x[-2]. If z = [n] + 3n-1], then your x vector in Matlab is z = [1 3]. 2-1] and 21-21 do not exist in this vector To fix this, you can either 1) add those values to the beginning of z, and calculate y[0 as given above, or 2) put a statement in the loop that is calculating the convolution sum so that it will only add terms where n-k2 0 (or the index corresponding to n -k is > 0) 2. A causal FIR filter has impulse response h[n] = n] + n-1] + n-2] + n-3]. This filter is a causal 4-point moving average (MA) filter In Matlab, create the impulse response vector h of this filter. h should have the same length as h[n] 3. Use your Matlab convolution function to find the filter output y[n] when your input z[n] = n +2 +2 1] + [n] _ [n _ 1] + [n-2]. Include your function call and its result in your lab report 4. Plot r[n] using a stem plot for n4 to 10. Label the x and y axes, and include the plot in your lab report 5. Plot y[n] as a stem plot for n4 to 10. Label the x and y axes, and include the plot in your lab reportExplanation / Answer
% SIMPLE CONVOLUTION FUNCTION conv(x,h)
% Receive two vectors and show on screen a new vector resultant of
% convolution operation
function simple_conv(f, g)
% Transform the vectors f and g in new vectors with the same length
F = [f,zeros(1,length(g))];
G = [g,zeros(1,length(f))];
% FOR Loop to put the result of convolution between F and G vectors
% in a new vector C. According to the convolution operation characteristics,
% the length of a resultant vector of convolution operation between two vector
% is the sum of vectors length minus 1
for i=1:length(g)+length(f)-1
% Create a new vector C
C(i) = 0;
% FOR Loop to walk through the vector F ang G
for j=1:length(f)
if(i-j+1>0)
C(i) = C(i) + F(j) * G(i-j+1);
else
end
end
end
% Show C vector on screen
C
end
2)
h = [0.25 0.25 0.25 0.25];% h in terms of matlab code
3)
x = [1 2 1 -1 1];
h = [0.25 0.25 0.25 0.25];
y = simple_conv(x,h)
4)
n = -4:10;
x = [0 0 x zeros(1,13-length(x))];
stem(n, x); ylabel('time');xlabel('x');
5)
n = -4:10;
y = [y zeros(1,15 -length(y))];
stem(n, y); ylabel('y');xlabel('time');
6) both our function and inbuilt fuction gives the same output
% code using inbuilt function
x = [1 2 1 -1 1];
h = [0.25 0.25 0.25 0.25];
y = conv(x,h)
n = -4:10;
x = [0 0 x zeros(1,13-length(x))];
stem(n, x); ylabel('time');xlabel('x');
y = [y zeros(1,15 -length(y))];
stem(n, y); ylabel('y');xlabel('time');
3.1)
1)
x = [1 2 1 -1 1];
h = [0.25 0.25 0.25 0.25];
y = conv(h,x)
n = -4:10;
x = [0 0 x zeros(1,13-length(x))];
stem(n, x); ylabel('time');xlabel('x');
y = [y zeros(1,15 -length(y))];
stem(n, y); ylabel('y');xlabel('time');
2)
x = [1 2 1 -1 1];
h = [0.25 0.25 0.25 0.25];
w= conv(x,h)
n = -4:10;
x = [0 0 x zeros(1,13-length(x))];
stem(n, x); ylabel('time');xlabel('x');
w = [w zeros(1,15 -length(w))];
stem(n, w); ylabel('w');xlabel('time');
3) if we compare the above y and w values they both are the same hence proving the commutativity of convoltion
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.