psychopy.org | Reference | Downloads | Github

Creating bandpass Gaussian white noise


#1

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


#2

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()

#3

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


#4

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


#5

@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 :

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