How can I implement these for loops efficiently using covolution?

1 回表示 (過去 30 日間)
MAWE
MAWE 2023 年 8 月 1 日
編集済み: Bruno Luong 2023 年 8 月 25 日
I have this code
for xx=1:length(x)
for kk=1:length(x)
xSinc(xx) = xSinc(xx)+x(kk)*sinc(xx-kk-delta/T);
end
end
How can implement this efficiently using convultion in MATLAB?
  1 件のコメント
Torsten
Torsten 2023 年 8 月 1 日
If you don't know how to spell the method, you'd better stick to your loop solution.

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

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 8 月 1 日
編集済み: Dyuman Joshi 2023 年 8 月 1 日
Simple multiplication would be good enough -
xx = 1:length(x);
kk = 1:length(x);
xSinc(xx) = xSinc(xx) + x(kk)*sinc(xx-kk'-delta/T);
  5 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 8 月 22 日
Please attach your code, so that I can reproduce the error and suggest solutions to tackle the issue.
MAWE
MAWE 2023 年 8 月 23 日
OK, it is working. I just needed to transpose one vector. Here another problem I have, the second vector depends on the first as
for xx = 1:length(x)
for kk=max(xx-L,1):min(xx+L,length(x))
xSinc(xx) = xSinc(xx)+x(kk)*sinc(xx-kk-delta/T);
end
end
Currently I am doing this
for xx = 1:length(x)
kk=max(xx-L,1):min(xx+L,length(x))
xSinc(xx) = xSinc(xx)+x(kk)*sinc(xx-kk'-delta/T);
end
Is it possible to write this for loop in vector form for efficient implementtaion?

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

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2023 年 8 月 23 日
編集済み: Bruno Luong 2023 年 8 月 25 日
Use conv
x = (0:0.2:5).^2;
L = 3;
delta = rand; T = rand;
% Your method
xSinc = zeros(size(x));
for xx = 1:length(x)
for kk=max(xx-L,1):min(xx+L,length(x))
xSinc(xx) = xSinc(xx)+x(kk)*sinc(xx-kk-delta/T);
end
end
xSinc
xSinc = 1×26
-0.0051 -0.0070 0.0352 0.1578 0.3593 0.6413 1.0038 1.4468 1.9704 2.5745 3.2591 4.0243 4.8700 5.7962 6.8029 7.8902 9.0580 10.3063 11.6351 13.0445 14.5344 16.1048 17.7558 20.0963 21.1378 24.2622
% conv method
xSinc2 = conv(x, sinc((L:-1:-L)+delta/T), 'same')
xSinc2 = 1×26
-0.0051 -0.0070 0.0352 0.1578 0.3593 0.6413 1.0038 1.4468 1.9704 2.5745 3.2591 4.0243 4.8700 5.7962 6.8029 7.8902 9.0580 10.3063 11.6351 13.0445 14.5344 16.1048 17.7558 20.0963 21.1378 24.2622
norm(xSinc2-xSinc)
ans = 5.7220e-15
plot(xSinc, 'b')
hold on;
plot(xSinc2, 'r.')
  3 件のコメント
MAWE
MAWE 2023 年 8 月 23 日
Can I ask about the undelying logic you used? Why for exampled you defined
kk = -L:L;
and then fliped the order in
K = flip(sinc(kk+delta/T));
Bruno Luong
Bruno Luong 2023 年 8 月 23 日
編集済み: Bruno Luong 2023 年 8 月 25 日
I can't explain more than it comes from definition of conv adapted to your code.
Have you tried to do some study of the code or you just ask without study conv?

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by