Help on speed optimization of convolution code
Show older comments
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
Accepted Answer
More Answers (1)
Image Analyst
on 12 Jun 2011
0 votes
Just use the built-in function "conv2" - it's already highly optimized.
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!