Create exact 40hz flickering square on psychotoolbox

Hello everyone, Ive written a code to make a flickering square with a specific frequency but the problem is I can just get the frequencies that are dividable to my screen frame rate. I can use the floor for the division but in that case I wont have an exact frequency of 40 for example since 144/40 is not an integer. (duty cycle is 50%)

Here`s my code:

clear all
clc
%tic
Screen(‘Preference’, ‘SkipSyncTests’, 1);
screenNum=0;
flipSpd=0;
[wPtr,rect]=Screen(‘OpenWindow’,screenNum); % wPtr - designate the window, rect - screen size
monitorFlipInterval=Screen(‘GetFlipInterval’, wPtr); % Refresh time [s]
frame_rate = (1/monitorFlipInterval); % Frame rate [Hz]

%%%%%%%%%%%%%%%% Setteing Frequencies of the Square %%%%%%%%%%%%%%%%%%%%%
% change "fi"s to change the freq of stimulus
f1 = 2;

p1 = floor(frame_rate/(f1));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
color_white = [255 255 255]; % Defining white color
color_black = [0 0 0]; % Defining black color

CenX = rect(3)/2; % Center of width screen
CenY = rect(4)/2; % Center of height screen

% Squares coordinates and length
Dis = 2CenX;
SqS = 2
CenY;
X1=CenX;
Y1=CenY;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Position of the square %%%%%%%%%%%%%%%%%%%%%
pos1=[CenX-Dis CenY-SqS CenX+Dis CenY+SqS]; % up left
% pos2=[CenX-X2 CenY-Y2-SqS CenX-X2+Dis CenY-Y2]; % bottom left
% pos3=[CenX-X3-Dis CenY-Y3 CenX-X3 CenY-Y3+SqS]; % up right
% pos4=[CenX-X4-Dis CenY-Y4-SqS CenX-X4 CenY-Y4]; % up bottem

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
black=BlackIndex(wPtr); % Find the color lookup table values (black)
white=WhiteIndex(wPtr); % Find the color lookup table values (white)

%% -----------------------------------------------------------------------
Screen(‘FillRect’,wPtr,black); % Fills a rectangle the size of the screen to be black
Screen(wPtr, ‘Flip’); % Flip the screen from offscreen to onscreen
HideCursor;

Screen(‘FillRect’,wPtr,black); % Blacks the screen
vbl=Screen(wPtr, ‘Flip’); % Collect the time for the first flip with vbl

turn1 = 1;
flag = 0;
tic

for i=1:600 % “time of run”
% Generate the flickering squares
if i <= (turn1)p10.5
Screen(‘FillRect’,wPtr,color_black, pos1);
elseif i <= (turn1+1)p10.5 && i > (turn1)p10.5
Screen(‘FillRect’,wPtr,color_white, pos1);
if i == (turn1+1)p10.5
turn1 = turn1 + 2;
flag = 1;
end
end
Screen(wPtr, ‘Flip’);
end

Screen(‘FillRect’,wPtr,black); % Blacks the screen
vbl=Screen(wPtr, ‘Flip’, vbl+(flipSpd*monitorFlipInterval));
Screen(‘CloseAll’);

Anyone has any idea how to fix this or give a suggestion to make flickering squares with exact frequency?

Many thanks

Run your monitor at 120 Hz refresh rate if possible.
Or use VRR support with AMD graphics on Linux, if your monitor is “AMD FreeSync” / “DisplayPort adaptive sync” compatible via DisplayPort. Cfe. VRRTest and help VRRSupport

1 Like