Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

convolution

0 views
Skip to first unread message

rockin...@gmail.com

unread,
Sep 30, 2008, 1:06:08 AM9/30/08
to
hi,
1. i need to convolve matrix A ( size 100 x 100 x 15 x 15) with a
kernel B (size 1 x 1 x 5 x 1).
i am doing this:

result = convn(A,B);

but it is not as fast as i'd like. I want to do this convolution more
like:

A = reshape A to a 2D matrix
B1 = reshape(B,5,1);
result = conv2(A,B);

Could anyone tell me how to reshape/permute A to obtain the same
result?

2. I also want to convolve A with a kernel C (size 1 x 1 x 1 x 5), how
should I modify A, so that I could use conv2 in this case (instead of
convn)?

Thanks!!!!!

rockin...@gmail.com

unread,
Sep 30, 2008, 7:44:55 AM9/30/08
to

bump

Carlos Adrian Vargas Aguilera

unread,
Sep 30, 2008, 8:53:02 AM9/30/08
to
Use the COnvolution Theorem and the FFTN with the option NFFT as a power of 2 for every dimension (zero-padding)...

Carlos

rockin...@gmail.com

unread,
Sep 30, 2008, 9:14:17 AM9/30/08
to
Thanks Carlos,

I think what I want is to use conv2, since my kernels are small and 1-
D. FFT would be slower due to padding and both FFT and IFFT
operations.

On Sep 30, 8:53 am, "Carlos Adrian Vargas Aguilera"

Carlos Adrian Vargas Aguilera

unread,
Sep 30, 2008, 9:31:02 AM9/30/08
to
Mhh, dont think but OK...

take a look to PERMUTE, and then RESHAPEs

Carlos

Bruno Luong

unread,
Sep 30, 2008, 9:58:01 AM9/30/08
to
rockin...@gmail.com wrote in message <cf425ced-9863-48e9...@2g2000hsn.googlegroups.com>...
>
> bump

A=round(10*rand(50,41,32,63));
B=round(10*rand(1,5,1,1));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
C=convn(A,B);
toc

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
d=find(size(B)>1);
if length(d)~=1
error('B must have only 1 non-singleton dimension');
end
p=circshift(1:ndims(A),[1 -d+1]);
Ap=permute(A,p);
sC=size(Ap);
Ap=reshape(Ap,size(Ap,1),[]);
C2 = conv2(Ap, B(:));
sC(1)=size(C2,1);
C2 = ipermute(reshape(C2,sC),p);
toc
%%%%%%%%%%%%%%%%%%%%%%%%%%

isequal(C,C2)

% Bruno

rockin...@gmail.com

unread,
Sep 30, 2008, 11:48:57 AM9/30/08
to
Thanks Bruno, you're the man!

shamsa

unread,
Oct 4, 2008, 12:57:01 AM10/4/08
to
"i want to implement the convolution function using matlab that produces the output whose length is same as input".

i am using the CONV function but it produces the output whose length is
outputlength= inputlength +impluse response length-1

please help me

Bruno Luong

unread,
Oct 4, 2008, 2:28:02 AM10/4/08
to
"shamsa " <sham...@yahoo.com> wrote in message <gc6t2t$brr$1...@fred.mathworks.com>...

> "i want to implement the convolution function using matlab that produces the output whose length is same as input".
>

Use conv2 with the third option instead.

Bruno

shamsa

unread,
Oct 4, 2008, 2:49:01 AM10/4/08
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <gc72di$563$1...@fred.mathworks.com>...

con2 gives 2d ans
but i want only 1d
my question is that
let suppose
x[n]=[1 2] %input
h[n]=[4 3 2 1] % impluse response
y[n]=conv(h,x); %
in this case y[n] has length of 5 but i just want that y[n] has same length as input(e.g 2 in this case)
please suggest me some way out of this
thanks

Bruno Luong

unread,
Oct 4, 2008, 2:57:01 AM10/4/08
to

>
> con2 gives 2d ans
> but i want only 1d
> my question is that
> let suppose
> x[n]=[1 2] %input
> h[n]=[4 3 2 1] % impluse response
> y[n]=conv(h,x); %

x=[1 2]
h=[4 3 2 1]
y=conv2(h,x,'same')

shamsa

unread,
Oct 4, 2008, 10:35:02 AM10/4/08
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <gc743t$cio$1...@fred.mathworks.com>...
this gives the ans y=[11 8 5 2] that has the length of 4 but i just want length of 2.bec inputx[n] has length of 2...


Bruno Luong

unread,
Oct 4, 2008, 10:52:01 AM10/4/08
to
"shamsa " <sham...@yahoo.com> wrote in message <gc7uum$j5a$1...@fred.mathworks.com>...

> this gives the ans y=[11 8 5 2] that has the length of 4 but i just want length of 2.bec inputx[n] has length of 2...
>

Try to think why the above gives length 4, and not 2 nor 5 (=4+2-1).

Bruno


0 new messages