# Creating bandpass Gaussian white noise

Given that it is a common stimulus in visual science, I was wondering whether someone has already the code to generate such a stimulus.

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

noise = np.random.random([res, res])
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

Thanks for the code. This is Gaussian smoothing, isnâ€™t it? I ll try to modify it to obtain a bandpass.

Oh sorry yes I misread your request. Bandpass filtering needs to be done in the fourier domain so a bit different.

@jon already gave a nice answer for generating static images. If you want a moving image or the possibility to control bandwidth in the orientation domain, you may be interested in the code :

http://motionclouds.invibe.net/

This easily generates images or movies and has been already suite extensively used with psychopy. As @jon mentioned, this is al done in the Fourier space.

cheers,
Laurent

1 Like

Just stumbled onto this code. Thanks Jon!

Mysteriously, I canâ€™t seem to get this to work with a units=â€śpixâ€ť window. The numpy array looks reasonable, but drawing an ImageStim where image is set to this seems to draw nothing.

Hey there, can I clarify what radius actually does in this makeMask function? (The radius of the noise blobs maybe?). The source code comments arenâ€™t really very intuitive to my simple mind. I was playing around with this value and canâ€™t be certain what this radius is actually doing just by looking at it visually, apart from knowing that increasing it makes the noise blobs coarser.

The noise stimulus class can make various kinds of visual noise including bandpass by two different routes.