1

currently i am building a stimuli in Psychopy for my thesis in my Uni, the 4 stimuli needs to flicker at 8.57 Hz,10 Hz ,12Hz and 15 Hz using the monitor refresh rate, my question is, can psychopy do that? and also what functions do i use for flickering at psychopy? what i have done so far is creating the shapes that needs to be flickered. the flickering effects must be dependent to frequency and phase

    from psychopy import visual, event, core,gui ,data,  logging #import c
    from numpy import sin, pi

    cyclepersecond=8.57142
    #Penentuan pergeseran fasa efek grating 
    phase=0
    phase1=0
    phase2=0
    phase3=0
    phase4=0
    #Penentuan Frekuensi temporal dari penampilan efek grating
    #phaseincrement = (cyclepersecond*360)
    #gratingtex=visual.GratingStim(win, res(1), res(2), [0 0 0 0])

    win = visual.Window([1366,768], color='black', units='pix',fullscr=False,) #membuat Window
    #pembuatan stimulus
    kotak1=visual.Rect(win, width= 150,height =150,pos=(10,250),)
    segitiga1=visual.Polygon(win, edges=3, radius=40,pos=(10,250))
    #segitiga1.setColor([-1,1,-1], colorSpace='rgb')
    kotak2=visual.Rect(win, width= 150, height=150,pos=(400,-200))
    segitiga2=visual.Polygon(win, edges=3, ori=90,radius=40, pos=(400,-200))
    kotak3=visual.Rect(win,width=150, height= 150, pos=(-400,-200))
    segitiga3=visual.Polygon(win,edges=3,radius=40,ori=-90,pos=(-400,-200))
    kotak4=visual.Rect(win,width= 150, height=150,pos=(0,-200))
    lingkaran1=visual.Circle(win,radius=40, edges=32,pos=(0,-200))

    trialClock=core.Clock()
    t=0
    while True:
         t=trialClock.getTime()
         kotak1.contrast=sin(t*pi*2)
         kotak1.draw()
         segitiga1.draw()
         kotak2.draw()
         segitiga2.draw()
         kotak3.draw()
         segitiga3.draw()
         kotak4.draw()
         lingkaran1.draw()
         win.flip()
         #core.wait(5.0)


    for key in event.getKeys():
      if key in ['escape','q']:
         core.quit()

I kinda know from psychtoolbox in matlab you can use the flip for flickering effects, but i don't really know how to do that in psychopy

aldo027
  • 13
  • 4

2 Answers2

3

@A_S00 posted a good conceptual answer. Just to be very specific, assuming that you use a 60 Hz monitor:

  1. 8.57 Hz corresponds to 60/8.57 = 7 frames on and 7 frames off.
  2. 10 Hz --> 6 frames
  3. 12 Hz --> 5 frames
  4. 15 Hz --> 4 frames

... here assuming that the frequency is frequency of SHIFTS (8.57 shifts per seconds) rather than CYCLES (8.57 on and offs per second) since the latter would be impossible on a 60 Hz monitor, because you should then change the image between frame 3 and 4.

So for e.g. 12 Hz you would do:

flicker_frequency = 12
current_frame = 0
while True:
     # When to draw stimuli
     if current_frame % (2*flicker_frequency) < flicker_frequency:
         kotak1.contrast = sin(t*pi*2)
         kotak1.draw()
         segitiga1.draw()
         kotak2.draw()
         segitiga2.draw()
         kotak3.draw()
         segitiga3.draw()
         kotak4.draw()
         lingkaran1.draw()

     # Show whatever has been drawn. Sometimes the stimuli, other times a blank screen. flip() waits for the next monitor update so the loop is time-locked to the screen here.
     win.flip()
     current_frame += 1  # increment by 1.
Jonas Lindeløv
  • 5,442
  • 6
  • 31
  • 54
  • Thanks for the great answer. fyi I had to wrap `2*flicker_frequency` in parentheses to make this work. – jkr Jun 22 '17 at 15:59
2

PsychoPy doesn't appear to have a built-in function for flickering. Its creator, Jon Pierce, recommends implementing flickering by:

  • Looping over frames (using the variable frameN)
  • Using the modulo operator (%) to derive a boolean value from the frame number, corresponding to the frequency you want (so that it goes from 1 on one block of frames, then 0 on the next block of frames, then back to 1, etc.)
  • On each frame, setting the opacity of your image to that boolean value (1 = fully opaque, 0 = transparent)

So, if your monitor is 60Hz, ((frameN % 12) >= 6) will be 1 for 6 frames (or 1/10th of a second), then 0 for the next 6 frames, then 1 again, then 0 again, and so on. That'll get you your 10Hz flicker; you'll have to use different divisors in your modulo function for each flicker frequency you want to implement.

A_S00
  • 225
  • 2
  • 15