how to do convolution without commands
이전 댓글 표시
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
채택된 답변
추가 답변 (5개)
Wayne King
2011년 11월 27일
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)
DI
2015년 3월 16일
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
Maryam Soltanlou
2018년 11월 6일
why do you use + in 'loop'? h(x,y)=int(M(m-i,n-j)k(i,j)didj
Savannah Quinn
2020년 9월 13일
I am getting an index out of bounds due to h(i,j)
Enrique de José María Flores Rodríguez
2021년 3월 16일
편집: Enrique de José María Flores Rodríguez
2021년 3월 16일
Seem works good on a DICOM image on 2021, thank you !
SALVADOR CANAS
2018년 1월 15일
0 개 추천
Those convolutions are convolutions with padding=1, how do you do for padding=0?
Sk Group
2021년 10월 25일
0 개 추천
Convolution without conv function in MATLAB | Complete CODE | Explanation | Example And Output
For complete detailed post visit: https://www.swebllc.com/convolution-without-function-in-matlab/
% 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
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)
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
