# psychopy.filters - helper functions for creating filters¶

This module has moved to psychopy.visual.filters but you can still (currently) import it as psychopy.filters

Various useful functions for creating filters and textures (e.g. for PatchStim)

psychopy.visual.filters.butter2d_bp(size, cutin, cutoff, n)[source]

Bandpass Butterworth filter in two dimensions.

Parameters
sizetuple

size of the filter

cutinfloat

relative cutin frequency of the filter (0 - 1.0)

cutofffloat

relative cutoff frequency of the filter (0 - 1.0)

nint, optional

order of the filter, the higher n is the sharper the transition is.

Returns
numpy.ndarray

filter kernel in 2D centered

psychopy.visual.filters.butter2d_hp(size, cutoff, n=3)[source]

Highpass Butterworth filter in two dimensions.

Parameters
sizetuple

size of the filter

cutofffloat

relative cutoff frequency of the filter (0 - 1.0)

nint, optional

order of the filter, the higher n is the sharper the transition is.

Returns
numpy.ndarray:

filter kernel in 2D centered

psychopy.visual.filters.butter2d_lp(size, cutoff, n=3)[source]

Create lowpass 2D Butterworth filter.

Parameters
sizetuple

size of the filter

cutofffloat

relative cutoff frequency of the filter (0 - 1.0)

nint, optional

order of the filter, the higher n is the sharper the transition is.

Returns
numpy.ndarray

filter kernel in 2D centered

psychopy.visual.filters.butter2d_lp_elliptic(size, cutoff_x, cutoff_y, n=3, alpha=0, offset_x=0, offset_y=0)[source]

Butterworth lowpass filter of any elliptical shape.

Parameters
sizetuple

size of the filter

cutoff_x, cutoff_yfloat, float

relative cutoff frequency of the filter (0 - 1.0) for x and y axes

alphafloat, optional

offset_x, offset_yfloat

offsets for the ellipsoid

nint, optional

order of the filter, the higher n is the sharper the transition is.

Returns
numpy.ndarray:

filter kernel in 2D centered

psychopy.visual.filters.conv2d(smaller, larger)[source]

Convolve a pair of 2d numpy matrices.

Uses fourier transform method, so faster if larger matrix has dimensions of size 2**n

Actually right now the matrices must be the same size (will sort out padding issues another day!)

psychopy.visual.filters.getRMScontrast(matrix)[source]

Returns the RMS contrast (the sample standard deviation) of a array

psychopy.visual.filters.imfft(X)[source]

Perform 2D FFT on an image and center low frequencies

psychopy.visual.filters.imifft(X)[source]

Inverse 2D FFT with decentering

psychopy.visual.filters.make2DGauss(x, y, mean=0.0, sd=1.0, gain=1.0, base=0.0)[source]

Return the gaussian distribution for a given set of x-vals

Parameters
• x – should be x and y indexes as might be created by numpy.mgrid

• y – should be x and y indexes as might be created by numpy.mgrid

• mean (float) – the centre of the distribution - may be a tuple

• sd (float) – the width of the distribution - may be a tuple

• gain (float) – the height of the distribution

• base (float) – an offset added to the result

psychopy.visual.filters.makeGauss(x, mean=0.0, sd=1.0, gain=1.0, base=0.0)[source]

Return the gaussian distribution for a given set of x-vals

Parameters
mean: float

the centre of the distribution

sd: float

the width of the distribution

gain: float

the height of the distribution

base: float

an offset added to the result

psychopy.visual.filters.makeGrating(res, ori=0.0, cycles=1.0, phase=0.0, gratType='sin', contr=1.0)[source]

Make an array containing a luminance grating of the specified params

Parameters
res: integer

the size of the resulting matrix on both dimensions (e.g 256)

ori: float or int (default=0.0)

the orientation of the grating in degrees

cycles:float or int (default=1.0)

the number of grating cycles within the array

phase: float or int (default=0.0)

the phase of the grating in degrees (NB this differs to most PsychoPy phase arguments which use units of fraction of a cycle)

gratType: ‘sin’, ‘sqr’, ‘ramp’ or ‘sinXsin’ (default=”sin”)

the type of grating to be ‘drawn’

contr: float (default=1.0)

contrast of the grating

Returns

a square numpy array of size resXres

psychopy.visual.filters.makeMask(matrixSize, shape='circle', radius=1.0, center=0.0, 0.0, range=- 1, 1, fringeWidth=0.2)[source]

Returns a matrix to be used as an alpha mask (circle,gauss,ramp).

Parameters
matrixSize: integer

the size of the resulting matrix on both dimensions (e.g 256)

shape: ‘circle’,’gauss’,’ramp’ (linear gradient from center),

‘raisedCosine’ (the edges are blurred by a raised cosine) shape of the mask

scale factor to be applied to the mask (circle with radius of [1,1] will extend just to the edge of the matrix). Radius can asymmetric, e.g. [1.0,2.0] will be wider than it is tall.

center: 2x1 tuple or list (default=[0.0,0.0])

the centre of the mask in the matrix ([1,1] is top-right corner, [-1,-1] is bottom-left)

fringeWidth: float (0-1)

The proportion of the raisedCosine that is being blurred.

range: 2x1 tuple or list (default=[-1,1])

The minimum and maximum value in the mask matrix

psychopy.visual.filters.makeRadialMatrix(matrixSize, center=0.0, 0.0, radius=1.0)[source]

Generate a square matrix where each element values is its distance from the centre of the matrix.

Parameters
• matrixSize (int) – Matrix size. Corresponds to the number of elements along each dimension. Must be >0.

• radius (float) – scale factor to be applied to the mask (circle with radius of [1,1] will extend just to the edge of the matrix). Radius can be asymmetric, e.g. [1.0,2.0] will be wider than it is tall.

• center (2x1 tuple or list (default=[0.0,0.0])) – the centre of the mask in the matrix ([1,1] is top-right corner, [-1,-1] is bottom-left)

Returns

Square matrix populated with distance values and size == (matrixSize, matrixSize).

Return type

ndarray

psychopy.visual.filters.maskMatrix(matrix, shape='circle', radius=1.0, center=0.0, 0.0)[source]

Make and apply a mask to an input matrix (e.g. a grating)

Parameters
matrix: a square numpy array

array to which the mask should be applied

shape: ‘circle’,’gauss’,’ramp’ (linear gradient from center)