Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.
|
|
Luca
Posts:
77
Registered:
6/6/12
|
|
Computation efficiency: convolution or direct calculation
Posted:
Dec 5, 2012 8:08 AM
|
|
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?
|
|
|
|