Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Computation efficiency: convolution or direct calculation

Subject: Computation efficiency: convolution or direct calculation

From: Luca

Date: 5 Dec, 2012 13:08:07

Message: 1 of 2

Hello everybody.
I wrote a code of a couple of lines in 2 minutes that computes the response of a dynamic system to a stimulus.
This is described by differential equations.
In the first draft, where I wasn't looking at all for efficiency, correct code etc.... I did

for i=2:numel(F)
c1(i) = c1(i-1) + k1*S(i)-(k2+k3)*c1(i-1)
c2(i) = c2(i-1)+k3*c1(i)
end
CC = c1+c2;
(where CC is my total system response, c1 and c2 the response of two subsystems and S the external stymulus)
With F having an huge number of elements (~100k) to achieve very fine "temporal" resolution so that using this Euler method to solve the ODE achieves the correct solution. I know this isn't the best method and I know that for loops are bad in matlab, for what speed is concerned
I also happen to know that this system has an analytical solution which is
CC = conv (R,S) with R being the impulse response and S the stimulus.
So I tried to use this to improve computation speed and "code beauty".
R = zeros (size(S));
R = (function(i));

CC = conv (R,S);
(R and S have the same size of 100k).
It turns out using tic and toc that the ugly method using the for loops takes 5 ms while the convolution using conv take 220 ms.
How's that???
(ok... maybe with the convolution I could go to much wider "time" frames to reduce the dimension of about 10-100 and become as fast or faster...)

And... which method shall I be using?
Can I improve the "for" cycle in some way?

Subject: Computation efficiency: convolution or direct calculation

From: Matt J

Date: 5 Dec, 2012 23:52:06

Message: 2 of 2

"Luca " <l.presottoRE@MOVE.campus.unimib.NOTit> wrote in message <k9ngvn$c9h$1@newscl01ah.mathworks.com>...
>
> It turns out using tic and toc that the ugly method using the for loops takes 5 ms while the convolution using conv take 220 ms.
> How's that???
> (ok... maybe with the convolution I could go to much wider "time" frames to reduce the dimension of about 10-100 and become as fast or faster...)
>
> And... which method shall I be using?
===========

It seems pretty clear why implementing as a filter as opposed to a convolution is faster. The filtering operation is very sparse whereas conv(R,S) is very non-sparse. If R and/or S happen to be sparse, you might be able to exploit the sparsity by doing sparse convolution, e.g., using

http://www.mathworks.com/matlabcentral/fileexchange/29832-n-dimensional-sparse-arrays

Otherwise, the filter seems to be the thing to use. You might be able to accelerate the filter implementation somewhat, as compared to the for-loop, using the FILTER command.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us