How to manipulate contrast/opacity in a flickering polygon

I am trying to present flickering squares using a sin wave function (i.e., stimulus fades in and out at a constant frequency). However, I don’t want opacity to go between the range of 0 (transparent) and 1 (opaque). Rather, I want to be able to manipulate the range as I fit (e.g. I want to have it between 0.5-1, 0.2-0.8, 0.4-0.9 etc).
I will use this function to set the frequency and the opacity:
1.0+0.5*np.sin(2 * np.pi * t * float(freq_one)) [Alphabets in rectangles]
However, I am not sure about the corresponding contrast range (it does not go between 0 and 1, but how do I know the exact range?).
Thanks!
-Mia

Dear psychopy community,

Can you tell me why I did not get any answer (so that I don’t do the same mistake again)
-Is the question not clear?
Or
-Is the question not suitable for this forum?

Thanks!
-Mia

Hi mia,

it is unclear what you want to do (and you do not provide any code or images that will explain what you are doing)…

I think you want to shift the amplitude and the baseline of your sine wave. You could use a modification to the formula, for example,

Amplitude = 0.5
Baseline = 0.5 
start = core.getTime()
cnt = 0
var = []
while cnt < 600:
    second = core.getTime() - start
    sin_val_one =Amplitude * np.sin((2 * np.pi * second * float(freq_one)) + 0) + Baseline
    var.append(sin_val_one)
    rect_one.opacity = sin_val_one
    win.flip()
    cnt += 1

To know the exact range you could use min(var), max(var) to get min and max values…

check this resource https://www.graphpad.com/guides/prism/7/curve-fitting/reg_sine_wave_nonzero_baseline.htm?toc=0&printWindow
or many other resources online.

Have fun,

1 Like

Hi Natalia,

Thanks a lot for the response. I will be careful about being clear in the future posts (I also just realized the link doesn’t work, so I fixed it).
What I did not understand was what min(var) and max(var) meant in opacity terms. Now, it is much clearer with the link you sent. Thanks a bunch!

Cheers,
-Mia

Hi Mia,

Amplitude = 0.5
Baseline = 0.5 
start = core.getTime()
cnt = 0
var = []
while cnt < 600:
    second = core.getTime() - start
    sin_val_one =Amplitude * np.sin((2 * np.pi * second * float(freq_one)) + 0) + Baseline
    var.append(sin_val_one)
    rect_one.opacity = sin_val_one
    win.flip()
    cnt += 1
print min(var)
print max(var)

note the last two statements. They will show (print) the range of your opacity values.
Btw, I think the range should be between 0 (no image is seen) and 1 (image is fully seen).

Best,
Natalia

Yes, works like a charm. Thanks a lot!

Best,
-Mia