# how to do convolution without commands

19 visualizaciones (últimos 30 días)
omar chavez el 27 de Nov. de 2011
Comentada: Sai Jothsna Sri el 7 de Sept. de 2023
Hi, im trying to do convolution without any of the commands in matlab.
Just plain for, this is because im trying to use a code that can also be implemented on C.
So far I have this
x=[1 2 3]
h=[-1 0 3]
Z=[0]
[M,N]=size(x)
[L,O]=size(h)
A=N+O-1
for n=1:1:N
for o=1:1:O
y(n,o)=x(n)*h(o)
end
end
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Sai Jothsna Sri el 7 de Sept. de 2023
sreya sathe

Iniciar sesión para comentar.

Chandra Kurniawan el 27 de Nov. de 2011
Hi, Omar. Do you seeking for 2D convolution code without Matlab toolbox command?? I have the code below
function B = convolve(A, k);
[r c] = size(A);
[m n] = size(k);
h = rot90(k, 2);
center = floor((size(h)+1)/2);
left = center(2) - 1;
right = n - center(2);
top = center(1) - 1;
bottom = m - center(1);
Rep = zeros(r + top + bottom, c + left + right);
for x = 1 + top : r + top
for y = 1 + left : c + left
Rep(x,y) = A(x - top, y - left);
end
end
B = zeros(r , c);
for x = 1 : r
for y = 1 : c
for i = 1 : m
for j = 1 : n
q = x - 1;
w = y -1;
B(x, y) = B(x, y) + (Rep(i + q, j + w) * h(i, j));
end
end
end
end
Save the following code with filename 'convolve.m'. And then create a new m-file and type this code :
clear; clc;
I = [4 4 3 5 4;
6 6 5 5 2;
5 6 6 6 2;
6 7 5 5 3;
3 5 2 4 4];
k = [0 -1 0;
-1 4 -1;
0 -1 0];
Hsl = convolve(I, k)
Watch at the result! You can also compare the result with the matlab toolbox command 'conv2'
Bnd = conv2(I,k,'same')
The both results are same :
Hsl =
6 3 -2 8 9
9 3 0 2 -3
2 0 2 6 -3
9 6 0 2 1
1 8 -6 5 9
Bnd =
6 3 -2 8 9
9 3 0 2 -3
2 0 2 6 -3
9 6 0 2 1
1 8 -6 5 9
>>
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
David Young el 27 de Nov. de 2011
This is a little more complex than necessary - you don't need the first loop that reflects A, just change the index computation in the second loop to reflect the mathematical definition of convolution.

Iniciar sesión para comentar.

### Más respuestas (5)

Wayne King el 27 de Nov. de 2011
I take it when you say "without commands", you really are just saying without conv(). It appears to me you have 1-D vectors from your initial post. Specifically, you give the example:
x=[1 2 3];
h=[-1 0 3];
You can exploit the relationship between linear convolution, circular convolution, and the DFT by extending the length of your input vectors with zero-padding, multiplying their DFTs, and then taking the inverse DFT.
x = [1 2 3]';
h = [-1 0 3]';
N = length(x)+length(h)-1;
x1 = [x; zeros(N-length(x),1)];
h1 = [h; zeros(N-length(h),1)];
convxh = ifft(fft(x1).*fft(h1));
Compare convxh with
conv(x,h)
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
omar chavez el 27 de Nov. de 2011
yes you are right. The thing is that, since the code should be able to apply in several platforms I cant use ifft or fft. Im trying to prove that a code can be put in several languages, like matlab, C,etc

Iniciar sesión para comentar.

DI el 16 de Mzo. de 2015
The first answer is not actually full size.
Full size will be like this:
% Written by Dizeng 3/15/2015. Full convolution.
function B = convolve(A, k)
[r,c] = size(A);
[m,n] = size(k);
h = rot90(k, 2);
center = floor((size(h)+1)/2);
Rep = zeros(r + m*2-2, c + n*2-2);
for x = m : m+r-1
for y = n : n+r-1
Rep(x,y) = A(x-m+1, y-n+1);
end
end
B = zeros(r+m-1,n+c-1);
for x = 1 : r+m-1
for y = 1 : n+c-1
for i = 1 : m
for j = 1 : n
B(x, y) = B(x, y) + (Rep(x+i-1, y+j-1) * h(i, j));
end
end
end
end
Hope it will be helpful to others...
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
Savannah Quinn el 13 de Sept. de 2020
I am getting an index out of bounds due to h(i,j)
Editada: Enrique de José María Flores Rodríguez el 16 de Mzo. de 2021
Seem works good on a DICOM image on 2021, thank you !

Iniciar sesión para comentar.

SALVADOR CANAS el 15 de En. de 2018
Those convolutions are convolutions with padding=1, how do you do for padding=0?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Sk Group el 25 de Oct. de 2021
Convolution without conv function in MATLAB | Complete CODE | Explanation | Example And Output
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

VIGNESH B S el 28 de Jul. de 2022
% The code below is for convolution without conv command.
% Idea behind it is multiplying a element of x with every element in h and
% adding them with a shift.
% Eg: x = [1,2] and h = [4,5,6] say, h is transformed to [3,4,5,0] ; no.of
% zeros to add after h is given by min(len(x) ,len(h)) -1). als zero is
% added so problems with vector addn is removed. (ouput of conv is of same
% length as transformed h).
%then you perform x(1).*h = [4,5,6,0] and x(2).*h = [8,10,12,0] when x(i)
%if i>2 -> you add zeros to the result of x(i).*h in at index 1 and shift
%it. y = conv of x and h = [4,5,6,0] + [0,8,10,12].
clc
clear
x = input('Enter x [..] '); %getting input x and h
Unable to run the 'fevalJSON' function because it calls the 'input' function, which is not supported for this product offering.
h = input('Enter h [..] ');
if length(x)>length(h)
temp_var = x;
x = h;
h = temp_var;
end
y = zeros(1,(length(x)+length(h)-1));
min_val = [length(x),length(h)];
r = min(min_val)-1; ;%minimum of x and h -1
hi = [h,zeros(1,r)];
for i = 1:length(x)
temp = x(i).*hi;
% disp(temp)
if i>=2
tempi = [zeros(1,i-1),temp(1:end-i+1)];
else
tempi = temp;
end
y = y + tempi;
end
disp('The convolution of x and h is:' );
disp(y)
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Read, Write, and Modify Image en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by