How to Speed up the FOR loop in my CODE

4 views (last 30 days)
Hi, I am working on wavelet decomposition and found a formula for it and wrote it by myself for computation.
The formula is :
This is LH filtered and i do it for LL, LH, HL and HH so i get 4 images..
Can someone check the for loop in it and help me improve the computation time of it.
Elapsed time is 10.040462 seconds. I want to reduce time as much as possible as i need to work on hundreds of images
% Wavelet Decomposition
%Decomposition wavelet 'coif1' filters
[Lo_D,Hi_D] = wfilters('coif1','d');
img = imread('tumor-1.jpg');
img= im2double(img);
figure, imshow(img), title('original image');
[nrows ncols] = size(img);
imgg = zeros(nrows+6,ncols+6);
imgg(1:nrows,1:ncols) = img(:,:);
figure, imshow(imgg);
% wavelet decomposition.
imgD_LL = zeros(size(img));
imgD_LH = zeros(size(img));
imgD_HL = zeros(size(img));
imgD_HH = zeros(size(img));
% Deconstruction
tic
for i = 1: nrows
for j = 1:ncols
for p = 1:numel(Lo_D)
for q = 1:numel(Hi_D)
tempqD_LL(q) = Lo_D(p)*Lo_D(q)*imgg(i+p,j+q);
tempqD_LH(q) = Lo_D(p)*Hi_D(q)*imgg(i+p,j+q);
tempqD_HL(q) = Hi_D(p)*Lo_D(q)*imgg(i+p,j+q);
tempqD_HH(q) = Hi_D(p)*Hi_D(q)*imgg(i+p,j+q);
end
if (length(tempqD_LL) == q)
tempQD_LL(p) = sum(tempqD_LL);
tempQD_LH(p) = sum(tempqD_LH);
tempQD_HL(p) = sum(tempqD_HL);
tempQD_HH(p) = sum(tempqD_HH);
end
end
if (length(tempQD_LL) == p )
imgD_LL(i,j) = sum(tempQD_LL);
imgD_LH(i,j) = sum(tempQD_LH);
imgD_HL(i,j) = sum(tempQD_HL);
imgD_HH(i,j) = sum(tempQD_HH);
end
end
end
toc
% Plots Deconstruction..
figure,
subplot(2,2,1), imshow(imgD_LL, [min(min(imgD_LL)) max(max(imgD_LL))]), title('Img Deconstruction - LL filter');
subplot(2,2,2), imshow(imgD_LH, [min(min(imgD_LH)) max(max(imgD_LH))]); title('Img Deconstruction - LH filter');
subplot(2,2,3), imshow(imgD_HL, [min(min(imgD_HL)) max(max(imgD_HL))]); title('Img Deconstruction - HL filter');
subplot(2,2,4), imshow(imgD_HH, [min(min(imgD_HH)) max(max(imgD_HH))]); title('Img Deconstruction - HH filter');
The Image used in above is:
I found the functions to do it but they are not same as my paper
i want to use my code below..
Thanks.

Accepted Answer

Michelle Wu
Michelle Wu on 26 Sep 2016
Edited: Michelle Wu on 26 Sep 2016
You should consider preallocating the arrays (“tempqD_LL”, “tempqD_LH”, etc.) for speed if you know their final size. Another possible way is to use vectorization, whereby you use matrix and vector operations instead of the loops.
In case where the for-loops cannot be vectorized, you could explore the possibility of using parallel for-loops (i.e. “parfor”) to accelerate the algorithm. Refer to the documentation for limitations of nested functions in parfor-loops.
Make sure that you code with “parfor” complies with the limitations stated in the doc page.
  3 Comments
Walter Roberson
Walter Roberson on 26 Sep 2016
parfor can be used in some cases where MATLAB detects that the common variable is acting as a "reduction variable". Basically, code of the form
T = T + something
or
T = T * something
can be used in parfor as long as the "something" does not use T.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by