Vectorize loop to speed up

Hi,
This segment of code takes 13 seconds to run and as it runs hundreds of times it needs to be drastically speed optimized. Wonder if it can be done. Parallelizing the loop doesn't seem to help much.
Parameters are:
tau is a cell array of 384 each a matrix of 202 x 202 double elements.
FFT is a cell array of 384 each a vector of 5000 double elements.
omega is a vector of doubles of length 5000.
the outer loop 'flow' to 'fhigh' is 1:1:150
Converting cell arrays to arrays helps only slightly.
Thanks in advance.
P=zeros(size(tau{1}));
for k=flow:fhigh
F=zeros(size(tau{1}));
for j=1:nchan
F=F+FFT{j}(k)*exp(i*omega(k)*tau{j});
end
F=F.*conj(F);
P=P+F;
end

5 件のコメント

Jan
Jan 2022 年 5 月 16 日
It would be very useful, if you provide some input data, e.g. created by rand(). It is hard to optimize some coe without running it. It would not be efficient, if anyone, who tries to answer your question, starts to write soe code to invent input data. So this is your turn.
Matt J
Matt J 2022 年 5 月 16 日
the outer loop 'flow' to 'fhigh' is 1:1:150
Why are omega and FFT{j} of length 5000 if only 150 of their elements are used in the loop?
Kamran
Kamran 2022 年 5 月 16 日
Sorry, only 150 of 5000 are used. But it could be different flow and fhigh though the length(150) is constant.
Jan
Jan 2022 年 5 月 18 日
@Kamran: Ich would really try it, but hesitate to invent some input data, because in my opinion, it is your turn to provide them.
FFT{j}(k) has some costs, which can be avoided, but exp(i*omega(k)*tau{j}) is most likely the bottleneck.
Kamran
Kamran 2022 年 5 月 20 日
Sorry for the late answer. Need to generate some data first. Get back later.

サインインしてコメントする。

回答 (2 件)

Matt J
Matt J 2022 年 5 月 16 日
編集済み: Matt J 2022 年 5 月 16 日

0 投票

Fcell=cellfun(@(x)x(:),FFT,'uni',0);
Fmat=cell2mat(Fcell(:)');
Tau=cellfun(@(x)x(:)',tau,'uni',0);
Tmat=cell2mat(Tau(:));
P=0;
for k=flow:fhigh
F=abs( Fmat(k,:)*exp((1i*omega(k)).*Taumat) ).^2;
P=P+F;
end
P=reshape(P, size(tau{1}) );

2 件のコメント

Kamran
Kamran 2022 年 5 月 16 日
Thanks. But it is almost the same. Still 13 seconds. Perhaps I should make a mex in C.
Matt J
Matt J 2022 年 5 月 16 日
編集済み: Matt J 2022 年 5 月 16 日
You might try again with a parfor-loop, but with the simplified loop implementation above. In this form, it might be easier for parfor to slice.

サインインしてコメントする。

Matt J
Matt J 2022 年 5 月 16 日
編集済み: Matt J 2022 年 5 月 16 日

0 投票

The operations look like IFFTs, though possibly you have irregular time and frequency sampling. Even so, you should possibly consider a compromise where you take the IFFT with pre- and post-interpolation to get the sampling that you need.

1 件のコメント

Kamran
Kamran 2022 年 5 月 16 日
Yes, the sampling is irregular so I have to do it this way. Thanks for the help.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 5 月 16 日

コメント済み:

2022 年 5 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by