简体   繁体   English

用bsxfun卷积而不是Matlab中的循环

[英]convolution with bsxfun instead of loops in Matlab

I want to replace the for loops with bsxfun to calculate convolution in Matlab. 我想用bsxfun替换for循环以在Matlab中计算卷积。 Following is the script: 以下是脚本:

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.

When I changed code as: 当我将代码更改为:

Received = bsxfun(@plus, Received, bsxfun(@conv, permute(squeeze(channel), [3 1 2]), Transmitted));

Error came out, which said "two non-single-dimension of input arrays must be matched". 出现错误,表示“输入数组的两个非单维必须匹配”。

How could I correct this line? 我该如何纠正这一行? Thanks a lot! 非常感谢!

Why do you want to replace the loops with bsxfun ? 为什么要用bsxfun替换循环? If the sizes involved in the convolution aren't particularly small, then the convolution is going to take up most of your overhead and the difference between loops and some vectorized version of this call is going to be minimal. 如果卷积中涉及的大小不是特别小,则卷积将占用您的大部分开销,并且循环和此调用的某些矢量化版本之间的差异将很小。

One option you have, if you can afford the temporary storage and it doesn't mess with your numerics too much, is to use the FFT to do this convolution instead. 如果您能负担得起临时存储并且不会过多干扰数字,则可以使用FFT进行此卷积。 That would look something like 看起来像

 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM