# Convolution in Fourier domain

I am computing the convolution of a disk Img in 2D numerical matrix form with a kernel Ker in the Fourier domain but the output image of the disk Convolved is not centered. The disk image after convolution appears to be shifted to the upper left region of the matrix.

The code:

ifftshift[dat_?ArrayQ, k : (_Integer?Positive | All) : All] :=
Module[{dims = Dimensions[dat]},
RotateRight[dat, If[k === All, Ceiling[dims/2],
Ceiling[dims[[k]]/2] UnitVector[Length[dims], k]]]]

ImFourier[Img_] :=
Module[{KerFun, Ker, FourierMul, Convolved},
KerFun[{x_, y_}] := ( E^(-500 x^2 - 700 y^2));  (* kernel function  *)
Ker = Table[KerFun[{x, y}], {x, -0.5, 0.5, 0.032}, {y, -0.5, 0.5, 0.032}];
FourierMul = InverseFourier[Fourier[Img]*Fourier[Ker]];
Convolved = Rescale[Chop[ifftshift[FourierMul]]];
{Return[Convolved], Return[FourierMul]}];

{Convolved, FourierMul} = ImFourier[DiskMatrix[13, 32]];
ArrayPlot[Convolved, ColorFunction -> GrayLevel]


How do I get a centered image from the Fourier domain convolution?.

I believe this is due to the slight assymetry that arises if your image dimensions are even. With an even number $$n = 2k$$ of degrees of freedom per dimension, there is also an even number of Fourier coefficients computed, namely for the basis functions $$\mathrm{e}^{-\mathrm{i} (k-1)\,t}, \dotsc, \mathrm{e}^{-\mathrm{i}\,t}, 1, \mathrm{e}^{\mathrm{i}\,t}, \dotsc, \mathrm{e}^{-\mathrm{i} k\,t}$$. That is, the mirror image of $$\mathrm{e}^{-\mathrm{i} k\,t}$$ cannot be represented.

For example, using odd image dimensions seems to work well:

ImFourier[Img_] := Module[{KerFun, Ker, FourierMul, Convolved, m, n},
{m, n} = Dimensions[Img];
Ker = Outer[{x, y} \[Function] (E^(-500 x^2 - 700 y^2)),
Subdivide[-0.5, 0.5, m - 1], Subdivide[-0.5, 0.5, n - 1]];
FourierMul = InverseFourier[Fourier[Img]*Fourier[Ker]];
Convolved = Rescale[Chop[ifftshift[FourierMul]]];
{Convolved, FourierMul}];

n = 33;
{Convolved, FourierMul} = ImFourier[DiskMatrix[13, n]];
ArrayPlot[Convolved, ColorFunction -> GrayLevel]


• Any workaround to have even dimensions?. – dykes Apr 11 '19 at 16:47