sodiumnitrate
sodiumnitrate

Reputation: 3131

Blurring a 3D numpy array with periodic boundary conditions

I have a 3D matrix that I'd like to (Gaussian) blur. This I can do with scipy.ndimage.filters.gaussian_filter(). My question is, what can I do so that the pixels on the edges are blurred so that periodic boundary conditions are obeyed?

What I mean by this is that when the element at a[0,:,:] is considered, the averaged value assigned to this element should also be affected by the elements in a[-1,:,:].

I've thought of concatenating the same array a multiple times so that I have an array of the form [[[a,a,a],[a,a,a],[a,a,a]],[[a,a,a],[a,a,a],[a,a,a]],[[a,a,a],[a,a,a],[a,a,a]]], i.e. an array that consists of a 3 by 3 grid of as. I would then blur the resulting array. Since my matrix is fairly large (200 by 200 by 200), I am trying to avoid doing this. (I could consider a subarray of the resulting large array, where I leave enough margin around a at the center. However, that would require determining the size of that margin every time I change the blur radius.)

Is there an easy and efficient way to do this?

Upvotes: 1

Views: 1061

Answers (1)

bpachev
bpachev

Reputation: 2212

Setting the mode keyword argument to 'wrap' will enforce periodic boundary conditions. The code would look something like the following.

result = gaussian_filter(a, sigma = 1., mode='wrap')

Replacing sigma with your actual parameter(s), of course.

Upvotes: 3

Related Questions