close all;
clear;
sca;
PsychDefaultSetup(2);
rng('shuffle')
screenNumber = max(Screen('Screens'));
white = WhiteIndex(screenNumber);
grey = white / 2;
black = BlackIndex(screenNumber);
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey, [], 32, 2,...
[], [], kPsychNeed32BPCFloat);
Screen('Flip', window);
ifi = Screen('GetFlipInterval', window);
topPriorityLevel = MaxPriority(window);
Priority(topPriorityLevel);
[xCenter, yCenter] = RectCenter(windowRect);
gaborDimPix = 55;
sigma = gaborDimPix / 6;
orientation = 90;
contrast = 0.5;
aspectRatio = 1.0;
numCycles = 3;
freq = numCycles / gaborDimPix;
gabortex = CreateProceduralGabor(window, gaborDimPix, gaborDimPix,...
[], [0.5 0.5 0.5 0.0], 1, 0.5);
dim = 8;
[x, y] = meshgrid(-dim:dim, -dim:dim);
dist = sqrt(x.^2 + y.^2);
innerDist = 3.5;
x(dist <= innerDist) = nan;
y(dist <= innerDist) = nan;
outerDist = 10;
x(dist >= outerDist) = nan;
y(dist >= outerDist) = nan;
x = x(isfinite(x));
y = y(isfinite(y));
xPos = x .* gaborDimPix + xCenter;
yPos = y .* gaborDimPix + yCenter;
nGabors = numel(xPos);
baseRect = [0 0 gaborDimPix gaborDimPix];
allRects = nan(4, nGabors);
for i = 1:nGabors
allRects(:, i) = CenterRectOnPointd(baseRect, xPos(i), yPos(i));
end
degPerSec = 360 * 4;
degPerFrame = degPerSec * ifi;
gaborAngles = rand(1, nGabors) .* 180 - 90;
degPerFrameGabors = cosd(gaborAngles) .* degPerFrame;
phaseLine = rand(1, nGabors) .* 360;
propertiesMat = repmat([NaN, freq, sigma, contrast, aspectRatio, 0, 0, 0],...
nGabors, 1);
propertiesMat(:, 1) = phaseLine';
vbl = Screen('Flip', window);
waitframes = 1;
while ~KbCheck
Screen('BlendFunction', window, 'GL_ONE', 'GL_ZERO');
Screen('DrawTextures', window, gabortex, [], allRects, gaborAngles - 90,...
[], [], [], [], kPsychDontDoRotation, propertiesMat');
Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
Screen('DrawDots', window, [xCenter; yCenter], 10, black, [], 2);
vbl = Screen('Flip', window, vbl + (waitframes - 0.5) * ifi);
phaseLine = phaseLine + degPerFrameGabors;
propertiesMat(:, 1) = phaseLine';
end
sca;