2

I'm having a problem with finding a faster way to convolve multiple vectors. All the vectors have the same length M, so these vectors can be combined as a matrix (A) with the size (N, M). N is the number of vectors.

Now I am using the below code to convolve all these vectors:

B=1;

for i=1:N

B=conv(B, A(i,:));

end

I found this piece of code becomes a speed-limit step in my program since it is frequently called. My question is, is there a way to make this calculation faster? Consider M is a small number (say 2).

2
  • How big is N? If it's large (say 10000), then your B vector is growing over time which will also slow things down. Commented Feb 8, 2014 at 1:59
  • For my application, N usually is not very big (<100), but it is true that B is growing over time. I do not know yet how could pre-allocate space for B in this case. Commented Feb 8, 2014 at 2:19

2 Answers 2

2

It should be quite a lot faster if you implement your convolution as multiplication in the frequency domain.

Look at the way fftfilt is implemented. You can't get optimal performance using fftfilt, because you want to only convert back to time domain after all convolutions are complete, but it nicely illustrates the method.

Sign up to request clarification or add additional context in comments.

Comments

1

Convolution is associative. Combine the small kernels, convolve once with the data.

Test data:

M = 2; N = 5; L = 100;
A = rand(N,M);
Bsrc = rand(1,L);

Reference (convolve each kernel with data):

B = Bsrc;
for i=1:N,
    B=conv(B, A(i,:));
end

Combined kernels:

A0 = 1;
for ii=1:N,
    A0 = conv(A0,A(ii,:));
end
B0 = conv(Bsrc,A0);

Compare:

>> max(abs(B-B0))
ans =
   2.2204e-16

If you perform this convolution often, precompute A0 so you can just do one convolution (B0 = conv(Bsrc,A0);).

5 Comments

Good point. However, in my specific case, Bsrc is simply 1. It'd be really great if the inner loop calculation (A0 part) can be further speed up.
"All vectors have the same length" according to the question.
Sorry for the confusion. each vector has been put as a column of A. B is just being used for saving the convolution result and initialized to 1.
@Kanzy I think you mean each vector is a row of A. But what would you do with A0, out of curiosity? Does A change frequently?
@chappjc Yes, A changes frequently. Each time I need to convolve all rows of A together. So there is no A0 in my original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.