本文介绍了验证卷积定理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要目标是证明卷积定理有效(只是提醒:卷积定理表示idft(dft(im) .* dft(mask)) = conv(im, mask)).我正在尝试编程.

My main goal is to show that the convolution theorem works (just a reminder: the convolution theorem means that idft(dft(im) .* dft(mask)) = conv(im, mask)). I'm trying to program that.

这是我的代码:

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.我想我缺少一些强制转换,因为如果使用强制转换来加倍,则矩阵中的数字彼此接近.也许我在浇铸或填充的位置上有一些错误?

I want to compare resIFFT and resConv. I think I'm missing some casting because I am getting numbers in the matrix closer one to another if I'm using casting to double.Maybe I have some mistake in the place of the casting or the padding?

推荐答案

  1. 为了使用DFT计算线性卷积,您需要将两个信号都填充为零,否则结果为.尽管您不必手动填充信号,但 fft2 可以为您完成此操作如果您将其他参数添加到函数调用中,例如:

  1. In order to compute the linear convolution using DFT, you need to post-pad both signals with zeros, otherwise the result would be the circular convolution. You don't have to manually pad a signal though, fft2 can do it for you if you add additional parameters to the function call, like so:

fft2(X, M, N)

在执行转换之前,它会填充(或截断)信号X以创建M by N信号.
在每个维度上填充每个信号的长度等于两个信号的长度之和,即:

This pads (or truncates) signal X to create an M-by-N signal before doing the transform.
Pad each signal in each dimension to a length that equals the sum of the lengths of both signals, that is:

M = size(im, 1) + size(mask, 1);
N = size(im, 2) + size(mask, 2);

  • 只是为了好的做法,而不是:

  • Just for good practice, instead of:

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

    您可以写:

    b = ones(4) / 16;
    

  • 无论如何,这是固定代码(仅出于示例目的,我生成了一个随机图像):

    Anyway, here's the fixed code (I've generated a random image just for the sake of the example):

    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(:)))
    

    应该得到的结果应该为零:

    The result you should get is supposed to be zero:

    ans =
    
        8.5265e-014
    

    足够近了.

    这篇关于验证卷积定理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-15 02:58