How do you make a discrete convolution if you can't analyze an index less than 1

3 views (last 30 days)
I'm trying to make a convolution filter but I can't find the sum becuase when g(n-k are less than zero matlab doesn't know what to do.
n = 0:1:100;
g1 = .25*cos((n/6)*pi).*unit(n);
g2 = .5*cos((pi/2)*n).*unit(n);
g3 = .25*cos(((6*pi)/5)*n).*unit(n);
for i = 1:21
g(i) = g1(i)+g2(i)+g3(i);
end
h = [-.027 0 .033 -.038 0 .053 -.067 0 .133 -.266 .356 -.266 .133 0 -.067 .053 0 -.038 .033 0 -.027];
yi = zeros(21,21);
y = zeros(1,21);
for i = 1:21
for j = 1:21
yi(j) = cumsum(g(i-j)*h(j));
end
y(i) = yi(j);
end
subplot(2,1,1)
plot(g)
subplot(2,1,2)
plot(y)
function [s]= unit(x)
for i = 1:length(x)
if x(i) > 0
s(i) = 1;
elseif x(i) == 0
s(i) = 1;
else
s(i) = 0;
end
end
end

Answers (1)

Image Analyst
Image Analyst on 20 Mar 2018
No need to write a convolution routine yourself. Simply use the built in conv() function.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!