How to vectorize loop of fft ?
7 次查看(过去 30 天)
显示 更早的评论
How to vectorize a for loop of fft ?
clear all
W = ones(1,100)*1.5;
y(1:200,1:100) = 0.3;
y(1,:) = ones(1,100)*1.2;
for j=1:200;
y(j+1,:) = ifft(fft(y(j,:)).*W);
end
0 个评论
回答(1 个)
Geoff Hayes
2015-2-28
Ole - is this something that you need to vectorize or just simplified? If the above is a valid example with all elements of W being identical, then the line
y(j+1,:) = (ifft((((fft((y(j,:)))).*W))));
can be simplified to
y(j+1,:) = ifft(fft((y(j,:))).*W);
= ifft(1.5*fft((y(j,:))));
= 1.5*y(j,:);
due to the linearity of the inverse Fourier transform. And so your for loop becomes
for j=1:200;
y(j+1,:) = 1.5*y(j,:);
end
The above implies that the nth column of y (for n>1) is simply
(1.5)^(n-1)*y(1,:)
in which case you can replace your code with just
y = 1.5*ones(201,100);
y(1,:) = 1.2;
y = cumprod(z);
Try the above and see what happens!
2 个评论
Geoff Hayes
2015-3-1
Ole - what is the error? Is it
Error using .*
Matrix dimensions must agree.
because of the line
ifft(fft(y(1:200,:)).*W);
where W is just a 1x100 array and fft(y(1:200,:)) is a 200x200 array. You can only perform an element-wise multiplication on arrays/matrices of the same dimension.
A question that I should have asked sooner - why are you trying to vectorize this problem? Performance reasons? Because the recurrence relation (k+1 depends on k) in the above for loop may provide the best solution.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!