Changing opacity of a stimulus in a sine wave

Hi everyone,

I’m looking for a way to change the opacity of a stimulus but according to a sine wave, I want it to smoothly go from white to transparent and go smoothly back to white. The speed of this change should be a frequency variable

So I want a white rectangle of which I manipulate the opacity (from 50% to 30% and back to 50%) with a certain frequency. And instead of having a flashing/blinking effect, I want a sine function to make it smooth.

Thank you in advance!

If you’re using a script, you can import math an then call math.sin, multiply it by some constant, and make that the opacity property of your shape.

import math
# I'm assuming you have a ShapeStim already called Shape

r=0.0
while drawShape:
    r+=.1 # or however fast you want it to change.
    Shape.setOpacity(abs(math.sin(r)*.5)) # the constant should be the maximum opacity you want.
    win.flip()

Generally speaking something along those lines should work. The tricky bit is just going to be getting the math right. The sin function will return a value between +1 and -1, and the opacity property of an object is between 0.0 and 1.0. As long as you get the absolute value, you can just multiply the output of the sin function by the maximum opacity you want, as long as the minimum is 0, or you can multiply it by what you add to some base opacity (e.g.,
setOpacity(.3+abs(math.sin(r)*.2)) for the 50% to 30% range you suggested, or use subtraction if you want to start from 50%).

Anyway modifying the opacity is easy, the rest is just figuring out the math.

2 Likes