【问题标题】:Verify the convolution theorem验证卷积定理
【发布时间】:2012-12-11 03:34:00
【问题描述】:

我的主要目标是证明卷积定理有效(只是提醒一下:卷积定理意味着idft(dft(im) .* dft(mask)) = conv(im, mask))。我正在尝试对其进行编程。

这是我的代码:

function displayTransform( im )
% This routine displays the Fourier spectrum of an image.
% 
% Input:      im - a grayscale image (values in [0,255]) 
% 
% Method:  Computes the Fourier transform of im and displays its spectrum,
%                    (if F(u,v) = a+ib, displays sqrt(a^2+b^2)).
%                    Uses display techniques for visualization: log, and stretch values to full range,
%                    cyclic shift DC to center (use fftshift).
%                    Use showImage to display and fft2 to apply transform.  

%displays the image in grayscale in the Frequency domain
imfft = fft2(im);
imagesc(log(abs(fftshift(imfft))+1)), colormap(gray);

% building mask and padding it with Zeros in order to create same size mask
b = 1/16*[1 1 1 1;1 1 1 1; 1 1 1 1; 1 1 1 1];
paddedB = padarray(b, [floor(size(im,1)/2)-2 floor(size(im,2)/2)-2]);
paddedB = fft2(paddedB);
C = imfft.*paddedB;
resIFFT = ifft2(C);

%reguler convolution
resConv = conv2(im,b);
showImage(resConv);

end

我想比较resIFFTresConv。我想我错过了一些铸造,因为如果我使用铸造来加倍,我会让矩阵中的数字更接近另一个。 也许我在铸造或填充的地方有一些错误?

【问题讨论】:

  • 我在这里没有看到问题。我所看到的只是代码转储和您“缺少某些东西”的声明。嗯,是的,你可能是,我们也是 - 即清楚地描述你的问题。
  • @IlmariKaronen 我试图澄清我的问题。请再次查看。
  • @Androidy - 仍然不清楚。将您的 imb 转换为双精度。
  • @Shai 我想在 matlab 中编写一个简单的演示程序来证明卷积定理有效。我的想法是拍摄一张与面具 b 进行卷积的图像。在另一边 ifft2(fft2(im).*fft2(b))。然后比较两个结果的值。但是我的问题是我得到了两个不同的矩阵。
  • 是否可以使用两个不同长度的向量

标签: matlab convolution dft


【解决方案1】:
  1. 为了使用 DFT 计算线性卷积,您需要在两个信号后用零填充,否则结果将是 circular convolution。不过,您不必手动填充信号,如果您在函数调用中添加其他参数,fft2 可以为您完成,如下所示:

    fft2(X, M, N)
    

    这会在进行转换之前填充(或截断)信号 X 以创建 M×N 信号。
    将每个维度中的每个信号填充到等于两个信号长度之和的长度,即:

    M = size(im, 1) + size(mask, 1);
    N = size(im, 2) + size(mask, 2);
    
  2. 只是为了良好的做法,而不是:

    b = 1 / 16 * [1 1 1 1; 1 1 1 1; 1 1 1 1; 1 1 1 1];
    

    你可以写:

    b = ones(4) / 16;
    

无论如何,这是固定代码(我生成了一个随机图像只是为了示例):

im = fix(255 * rand(500));            % # Generate a random image
mask = ones(4) / 16;                  % # Mask

% # Circular convolution
resConv = conv2(im, mask);

% # Discrete Fourier transform
M = size(im, 1) + size(mask, 1);
N = size(im, 2) + size(mask, 2);
resIFFT = ifft2(fft2(im, M, N) .* fft2(mask, M, N));
resIFFT = resIFFT(1:end-1, 1:end-1);  % # Adjust dimensions

% # Check the difference
max(abs(resConv(:) - resIFFT(:)))

你应该得到的结果应该是零:

ans =

    8.5265e-014

足够接近。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-24
    • 2017-11-17
    • 2020-03-27
    • 2017-01-02
    • 2014-05-09
    • 1970-01-01
    • 2018-11-11
    • 2020-08-20
    相关资源
    最近更新 更多