How can I Implement a convolution function in MATLAB and perform it on the following signals and plot the results ?
68 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Vaban Dust
am 14 Mär. 2018
Kommentiert: Walter Roberson
am 29 Dez. 2021
Hi guys,
I have the following function:
impulse_response = zeros(1, length(input) + length(delta) - 1 );
for t_samp = 1:length(input)
for c_samp = 1:length(delta)
index = t_samp + c_samp - 1;
value = delta(c_samp) * input(t_samp);
impulse_response(index) = impulse_response(index) + value;
end
end
I am new to Matlab and I want to understand how to Implement a convolution function in MATLAB and perform it on the following signals and plot the results ?
Task (1):
x[n] = [1,1,1,1] h[m] = [1,0,−1] x[n] = sin(n) h[m] = [−1,−2,8,−2,−1] 20 ≤ n < 20
Task (2): Given the input signal x[n]=0.3∗sin(n/5)+sin(n/50) : -Create your own delta signal h[m] that removes the higher frequency sinusoidal component to get yl[n].
Thanks in advance for your kind explanation.
2 Kommentare
Akzeptierte Antwort
Abraham Boayue
am 16 Mär. 2018
Hey Vaban, you already got a code that implements convolution in matlab, all you need to do is to use it to solve your exercises: Alternatively, you could use matlab built in function called conv(), but it seems like your teacher wants you to learn how to code in matlab. Here is how you can use the code you have. Ex. Task 1.
% This is just a cleaner code, same as the one you posted
clear variables
close all
n = -20:20;
x = sin(n);
h = [-1,-2,8,-2,-1];
N = length(x);
M = length(h);
Ny = N + M -1;
y = zeros(1,Ny);
for i = 1:N
for k = 1:M
y(i+k-1) = y(i+k-1) + h(k)*x(i);
end
end
m = 0: Ny-1;
% Make plot
figure
stem(m,y,'linewidth',3,'color','m')
grid;
a = title('Output of an LTI System y(n)');
set(a,'fontsize',14);
a = ylabel('y(n)');
set(a,'Fontsize',14);
a = xlabel('n');
set(a,'Fontsize',14);
% Using matlab built in function (you get the same results)
figure
y2 = conv(x,h);
stem(m,y2,'linewidth',3,'color','r')
grid;
a = title('Output y(n) using conv(x,h)');
set(a,'fontsize',14);
a = ylabel('y(n)');
set(a,'Fontsize',14);
a = xlabel('n ');
set(a,'Fontsize',14);
% % input = [1 1 1 1]; % input = x (n) = [1 1 1 1]
% % delta = [1 0 -1]; % h(m) = [-1 0 1]
% impulse_response = zeros(1, length(input) + length(delta) - 1 );
% for t_samp = 1:length(input)
% for c_samp = 1:length(delta)
% index = t_samp + c_samp - 1;
% value = delta(c_samp) * input(t_samp);
% impulse_response(index) = impulse_response(index) + value;
% end
% end
% Ny = length(input) + length(delta)-1;
% y = impulse_response;
% m = 0:Ny-1;
% % plot
% stem(m,y,'linewidth',3,'color','b')
% grid;
% a = title('Output of an LTI System y(n)');
% set(a,'fontsize',14);
% a = ylabel('y(n)');
% set(a,'Fontsize',14);
% a = xlabel('n [1 4]');
% set(a,'Fontsize',14);
3 Kommentare
Niklas Endler
am 20 Okt. 2020
Correct me if I'm wrong but this only works in cases where the step function h[m] is symmetric?
Walter Roberson
am 20 Okt. 2020
discrete convolution and discrete filtering are the same mathematical operation, but they use the opposite convention on whether the matrix is applied left-to-right or right-to-left.
>> conv([1 2 3],[1 2 3])
ans =
1 4 10 12 9
>> conv([1 2 3],fliplr([1 2 3]))
ans =
3 8 14 8 3
conv() essentially flips the second vector and moves the result from left to right across the first vector; assume that there are zeros outside the first vector. This operation is thematically consistent with continuous convolution,
as opposed to ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/386793/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/386788/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/386793/image.png)
Weitere Antworten (3)
Ankur Agrawal
am 21 Sep. 2021
Bearbeitet: Walter Roberson
am 29 Dez. 2021
n = -20:20;
x = sin(n);
h = [-1,-2,8,-2,-1];
N = length(x);
M = length(h);
Ny = N + M -1;
y = zeros(1,Ny);
for i = 1:N
for k = 1:M
y(i+k-1) = y(i+k-1) + h(k)*x(i);
end
end
m = 0: Ny-1;
0 Kommentare
Abdullah Mohmmed
am 29 Dez. 2021
Write a MATLAB code to calculate and plot the two-sided convolution between the two discrete time signals x(n) and h(n) shown below
1 Kommentar
Walter Roberson
am 29 Dez. 2021
No signal definitions were shown.
And if they had been, we would have just referred you to the discussion above, which includes full code.
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!