我想用bsxfun替换for循环来计算Matlab中的卷积。以下是脚本:
for Rx = 1:Num_Rx
for Tx= 1:Num_Tx
Received(Rx,:)=Received(Rx,:)+conv(squeeze(channel(Rx,Tx,:))', Transmitted(Tx,:));
end
end
% Received is a Num_Rx by N matrix, Transmitted is a Num_Tx by N matrix and channel is a 3D matrix with dimension Num_Rx, Num_Tx, N.
当我将代码更改为:
Received = bsxfun(@plus, Received, bsxfun(@conv, permute(squeeze(channel), [3 1 2]), Transmitted));
错误出来了,说"输入数组的两个非一维必须匹配"。
如何更正此行?多谢!
为什么要
用bsxfun
替换循环? 如果卷积中涉及的大小不是特别小,那么卷积将占用大部分开销,并且循环和此调用的某些矢量化版本之间的差异将很小。
如果你能负担得起临时存储并且它不会过多地弄乱你的数字,那么你有一个选择,那就是使用 FFT 来做这个卷积。 那看起来像
Transmitted = reshape(Transmitted, [1 Num_Tx size(Transmitted, 2)]);
N = size(Transmitted, 3) + size(channel, 3) - 1;
Received = ifft(fft(channel, N, 3).*fft(Transmitted, N, 3), N, 3);
Received = squeeze(sum(Received, 2));