# Create exact 40hz flickering square on psychotoolbox

**URL:** <https://psychtoolbox.discourse.group/t/create-exact-40hz-flickering-square-on-psychotoolbox/4291>\
**Category:** Programming Help\
**Created:** [March 4, 2022, 4:11pm UTC](https://psychtoolbox.discourse.group/t/create-exact-40hz-flickering-square-on-psychotoolbox/4291 "2022-03-04T16:11:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![arminpan](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@arminpan](https://psychtoolbox.discourse.group/u/arminpan)\
**Post date:** [March 4, 2022, 4:11pm UTC](https://psychtoolbox.discourse.group/t/create-exact-40hz-flickering-square-on-psychotoolbox/4291/1 "2022-03-04T16:11:15Z")

</div>

Hello everyone, I`ve 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 won`t 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 = 2_CenX;  
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)_p1_0.5  
Screen(‘FillRect’,wPtr,color\_black, pos1);  
elseif i \<= (turn1+1)_p1_0.5 && i \> (turn1)_p1_0.5  
Screen(‘FillRect’,wPtr,color\_white, pos1);  
if i == (turn1+1)_p1_0.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

---

<div class="post-metadata">

**Author:** ![dcnieho](https://avatars.discourse-cdn.com/v4/letter/d/779978/32.png) [@dcnieho](https://psychtoolbox.discourse.group/u/dcnieho)\
**Post date:** [March 4, 2022, 7:03pm UTC](https://psychtoolbox.discourse.group/t/create-exact-40hz-flickering-square-on-psychotoolbox/4291/2 "2022-03-04T19:03:03Z")

</div>

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`
