# Why does GaussianFilter not have a periodic option?

Let us say I have a table:

tab = RandomReal[{-1, 1}, {128, 128}];


I can filter it using GaussianFilter as follows

GaussianFilter[tab, 12] // Image // ImageAdjust


But imagine I want the resulting image/cube to be periodic.

Question:

How come GaussianFilter not have a Periodic option?

I would ideally want to have

 GaussianFilter[tab, 8,Periodic->True] // Image // ImageAdjust


to produce something like this?

This should be easily done using Fourier Transform?

In principle it could work for tensors of any rank, like so?

tab = RandomReal[{-1, 1}, {32, 32, 32}]
GaussianFilter[tab, 8,Periodic->True] // Image3D // ImageAdjust


You know the GaussianFilter[] option Padding->"Periodic"  ? I think that's the option you're looking for!

tab = RandomReal[{-1, 1}, {256}];


evaluates a periodic result too.

This option evaluates the 2D-example too

tab = RandomReal[{-1, 1}, {128, 128}];



• Arg! you are right... Oh well so much for spending a few hours coding this :-) Nov 4, 2021 at 9:35
• @Chris Sorry ;-) Nov 4, 2021 at 9:44
• Its on me for not remembering this. I had used it in the past. Mathematica has so many options and ways of implementing them it is a maze. Nov 4, 2021 at 9:53

Ok I cheated a bit: I knew the answer :-) But I believe it should become a built in option to GaussianFilter!

This function will do the trick for even tensors:

fftIndgen[size_] :=
Range[0, Quotient[size, 2]], {0, Quotient[size, 2] - 1},
"ReflectedNegation"];
FourierGaussianFilter::usage =
"FourierGaussianFilter[tab,size] does Gaussian periodic filtering";
FourierGaussianFilter::odd = "tensor size should be even";
FourierGaussianFilter[data_, R_] :=
Module[{d = data // TensorRank, l = data // Length},
If[OddQ[l] == True, Message[FourierGaussianFilter::odd];
Abort[]];
InverseFourier[
Fourier[data]*
Exp[-0.5 R^2 Map[# . # &,
Outer[List, Sequence @@ Table[fftIndgen[l], d]
], {d}]
]] // Re // Chop]


Should work for tensors on any rank. E.g.

tab = RandomReal[{-1, 1}, {256}];
{tab, FourierGaussianFilter[tab, 5]} // ListLinePlot


It also works for, say, rank 4 tensors:

tab = RandomReal[{-1, 1}, 2{8, 8, 8, 8}];