Creating bandpass Gaussian white noise

The code below does it. Note that, for the code below, the radius below is the confusing bit. Here’s an explanation:

It’s units are “fractions of the stimulus” in this case and the radius is specified to 3xSigma (for the gaussian). SO if you have a stimulus width of 3 deg (so radius of 1.5) then setting the filter radius to be 0.1 means that the radius was actually 0.1*3/2 = 0.15 deg = 3xSigma. So if you want to quote the gaussian filter in terms of its sigma (common practice) it would be radiusXstimSize/2/3 = 0.05 deg in this case.

Here’s the code to create it:

from psychopy import filters
import numpy as np
from psychopy import visual, event

def makeFilteredNoise(res, radius, shape='gauss'):
    noise = np.random.random([res, res])
    kernel = filters.makeMask(res, shape=shape, radius=radius)
    filteredNoise = filters.conv2d(kernel, noise)
    filteredNoise = (filteredNoise-filteredNoise.min())/(filteredNoise.max()-filteredNoise.min())*2-1
    return filteredNoise
    
filteredNoise = makeFilteredNoise(256, 0.1)

win = visual.Window([400,400], monitor='testMonitor')
stim = visual.ImageStim(win, image = filteredNoise, mask=None)
stim.draw()
win.flip()
event.waitKeys()
1 Like