Help on speed optimization of convolution code
1 次查看(过去 30 天)
显示 更早的评论
I want to calculate the convoluted value of a quantity based on the relationship
c(x) = integral (crs(x')*abs(x')dx') / integral (abs(x')*dx'),
actually it is the convolution of a spectrum (crs in my case) with a slit funtion (abs respectively).
I have "translated" a code i had made in the past in vb to matlab, it works fine but it is very slow, so i am asking any help/advice for speed optimization.
My code is below
w1=-1.5;
w2=1.5;
for ii=1:size(w_sl) %size = 6
for w=w_sl(ii)-1.5:0.01:w_sl(ii)+1.5
sf=0;
z=0;
for j=1:size(wv,1) % size = 1956
if wv(j)>w+w1 && wv(j)<w+w2
for l=1:size(Slit005,1)
if wv_sl(l)==wv(j)-w % if wavelength match exactly then
z=z+crs(j)*abs(l); %calculate the integral (sum) of spectrum + slit function
sf=sf+abs(l);
break
end
if wv_sl(l)>wv(j)-w %if wavelength doesn't match exactly then interpolate slit function to current wavelength
yint=(abs(l)*(wv(j)-w-wv_sl(l-1))-abs(l-1)*(wv(j)-w-wv_sl(l)))/(wv_sl(l)-wv_sl(l-1));
z=z+crs(j)*yint;
sf=sf+yint;
break
end
end
end
end
if sf>0
m=m+1;
crs_O3(m,:)=[w z/sf];
end
end
end
Thank you very much
0 个评论
采纳的回答
Andrew Newell
2011-6-12
You could probably speed it up considerably by using the MATLAB function filter (see also Filter Implementation and Analysis).
Note that abs is the name of a MATLAB builtin function, so you shouldn't use it for a variable name.
1 个评论
Andrew Newell
2011-6-21
Unfortunately i didn't manage to implement, i am not just new in matlab answers but in matlab in general, so the way i am programming is much more different than matlab.. but to be totally honor i didn't search it very much. I must finish some tasks till the end of the month so for the moment, unfortunately, i can't afford time working on new techniques (i was expecting that somebody could provide an optimization on my posted code), i keep your answer in my mind so as to search on it in the near future. Sure conv2 function that imageanalyst suggested doesn't work for what i need to do, so i 'll vote your answer.
更多回答(1 个)
Image Analyst
2011-6-12
Just use the built-in function "conv2" - it's already highly optimized.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!