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.


Creating visual noise
#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()

Question regarding white noise generation with filters.makeMask and visual.Stim
#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


#6

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.


#7

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.


#8

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