How to Speed up the FOR loop in my CODE

조회 수: 4 (최근 30일)
Gopichandh Danala
Gopichandh Danala 2016년 9월 20일
댓글: Walter Roberson 2016년 9월 26일
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.

채택된 답변

Michelle Wu
Michelle Wu 2016년 9월 26일
편집: Michelle Wu 2016년 9월 26일
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
Michelle Wu
Michelle Wu 2016년 9월 26일
Actually according to the When to Use parfor doc page, it seems that "parfor" may not be a good choice for you because you cannot use a parfor-loop when an iteration in your loop depends on the results of other iterations. Each iteration must be independent of all others.
I would suggest look more into the possibility of vectorizing your nested loop. You may refer to the Vectorization doc page for more information.
Walter Roberson
Walter Roberson 2016년 9월 26일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by