How to perform 2 dimensional circular convolution

24 ビュー (過去 30 日間)
Palguna Gopireddy
Palguna Gopireddy 2022 年 3 月 7 日
編集済み: Matt J 2025 年 1 月 6 日
We have 'conv' and 'conv2' functions in matlab to find 1-D and 2-D linear convolution.
But for circular convolution we have only 'cconv' for 1-D convolution. Is there 2-D circular convolution function in matlab or there is any way to acheive 2-D circular convolution in MATLAB.
I tried using 'conv2(A,B,'same'), but it is not same as 2-D circular convolution.

採用された回答

Matt J
Matt J 2022 年 3 月 7 日
編集済み: Matt J 2025 年 1 月 6 日
function z=cyconv(x,y)
%Non-Fourier domain cyclic convolution.
%
% z=cyconv(x,y)
%
%Note: y can be shorter in any of its dimensions than x and will be
%zero-padded implicitly.
xsiz=num2cell(size(x));
ysiz=num2cell(size(y));
subs=cellfun(@(n,m)[n-(m-2):n,1:n],xsiz, ysiz,'uni',0);
x=x(subs{:});
z=convn(x,y,'valid');

その他の回答 (1 件)

Matt J
Matt J 2022 年 3 月 7 日
編集済み: Matt J 2022 年 3 月 7 日
Using FFTs
out = ifft2(fft2(A).*fft2(B));
  2 件のコメント
Ahmed
Ahmed 2025 年 1 月 5 日
this is better
Matt J
Matt J 2025 年 1 月 6 日
編集済み: Matt J 2025 年 1 月 6 日
@Ahmed if by better, you mean faster, then that won't always be true. If you are convolving a large array with a short kernel, it is often faster to work in the non-Fourier domain. Example:
n=5000;
A=rand(n);
B=rand(5);
tic;
out1 = real(ifft2(fft2(A).*fft2(B,n,n)));
toc;
Elapsed time is 0.786392 seconds.
tic;
out2 = cyconv(A,B);
toc;
Elapsed time is 0.284089 seconds.
percentDifference=norm(out1-out2,'inf')/norm(out1,'inf')*100
percentDifference = 8.2314e-14
function z=cyconv(x,y)
%Non-Fourier domain cyclic convolution
%
% z=cyconv(x,y)
xsiz=num2cell(size(x));
ysiz=num2cell(size(y));
subs=cellfun(@(n,m)[n-(m-2):n,1:n],xsiz, ysiz,'uni',0);
x=x(subs{:});
z=convn(x,y,'valid');
end

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

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by