Code Optimization, for loop
이전 댓글 표시
Is it possible to make this code faster?
%
RevPSD = zeros(size(X));
for m = Tmin + 1:size(X,2)
for i = Tmin:Tmax
if m - i <= 0
break
else
RevPSD(:,m) = RevPSD(:,m) + exp(-2*delta*i*hop/fs)*(Xpsd(:,m-i));
end
end
end
댓글 수: 2
Christoph F.
2017년 9월 28일
Two suggestions:
The inner loop only loops from Tmin to (m-1). The extra comparison of m and i inside the loop could be removed if the loop condition is changed, e.g.
for i = Tmin:(m-1)
RevPSD(:,m) = RevPSD(:,m) + exp(-2*delta*i*hop/fs)*(Xpsd(:,m-i));
end
The values of the term
exp(-2*delta*i*hop/fs)
only depend on i can could be pre-calculated for every possibly value of i outside the loop. e.g.
exptable = exp(-2*delta*(Tmin:Tmax)*hop/fs);
for m = Tmin + 1:size(X,2)
for i = Tmin:(m-1)
RevPSD(:,m) = RevPSD(:,m) + exptable(i-Tmin+1)*(Xpsd(:,m-i));
end
end
Jan
2017년 9월 28일
@Christoph F: Please post this in the answer sections.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 ループと条件付きステートメント에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!