Skip to content
dotsX_JQK_170320.m 17.9 KiB
Newer Older
Julian Kosciessa's avatar
Julian Kosciessa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
%_______________________________________________________________________
%
% core function for drawing random dots motion on presentation screen
%_______________________________________________________________________
%
function [frames,rseed,start_time,end_time,response,response_time] = dotsX_JQK_170320(screenInfo,dotInfo,targets,indCond)
% DOTSX display dots or targets on screen
%
% [frames,rseed,start_time,end_time,response,response_time] = dotsX(screenInfo,dotInfo,targets)
%
% For information on minimum fields of screenInfo and dotInfo arguments, see
% also openExperiment and createDotInfo. The input argument - "targets" is not
% necessary unless showing targets with the dots. Since rex only likes integers, 
% almost everything is in visual degrees * 10.
%
%   dotInfo.numDotField     number of dot patches that will be shown on screen
%   dotInfo.coh             vertical vectors, dots coherence (0...999) for each 
%                           dot patch
%   dotInfo.speed           vertical vectors, dots speed (10th deg/sec) for each 
%                           dot patch
%   dotInfo.dir             vertical vectors, dots direction (degrees) for each 
%                           dot patch
%   dotInfo.dotSize         size of dots in pixels, same for all patches
%   dotInfo.movingDotColor  color of dots in RGB for moving dots, same for all patches
%   dotInfo.randomDotColor  color of dots in RGB for random dots, same for all patches
%   dotInfo.maxDotsPerFrame determined by testing video card
%   dotInfo.apXYD           x, y coordinates, and diameter of aperture(s) in 
%                           visual degrees          
%   dotInfo.maxDotTime      optional to set maximum duration (sec). If not provided, 
%                           dot presentation is terminated only by user response
%   dotInfo.trialtype       1 fixed duration, 2 reaction time
%   dotInfo.keys            a set of keyboard buttons that can terminate the 
%                           presentation of dots (optional)
%   dotInfo.mouse           a set of mouse buttons that can terminate the 
%                           presentation of dots (optional)
%
%   screenInfo.curWindow    window pointer on which to plot dots
%   screenInfo.center       center of the screen in pixels
%   screenInfo.ppd          pixels per visual degree
%   screenInfo.monRefresh   monitor refresh value
%   screenInfo.dontclear    If set to 1, flip will not clear the framebuffer 
%                           after Flip - this allows incremental drawing of 
%                           stimuli. Needs to be zero for dots to be erased.
%   screenInfo.rseed        random # seed, can be empty set[] 
%
%   targets.rects           dimensions for drawOval
%   targets.colors          color of targets
%   targets.show            optional, if only showing certain targets but don't
%                           want to change targets structure (index number of 
%                           targets) to be shown during dots
%
% Algorithm:
%   All calculations take place within a square aperture in which the dots are 
% shown. The dots are constructed in 3 sets that are plotted in sequence.  For 
% each set, the probability that a dot is replotted in motion -- as opposed to 
% randomly replaced -- is given by the dotInfo.coh value. This routine generates 
% a set of dots as an (ndots,2) matrix of locations, and then plots them.  In 
% plotting the next set of dots (e.g., set 2), it prepends the preceding set 
% (e.g., set 1).
%

% created by MKMK July 2006, based on ShadlenDots by MNS, JIG and others

% Structures are not altered in this function, so should not have memory
% problems from matlab creating new structures.

% CURRENTLY THERE IS AN ALMOST ONE SECOND DELAY FROM THE TIME DOTSX IS
% CALLED UNTIL THE DOTS START ON THE SCREEN! THIS IS BECAUSE OF PRIORITY.
% NEED TO EVALUATE WHETHER PRIORITY IS REALLY NECESSARY.

if nargin < 3
    targets = [];
    showtar = [];
else
    if isfield(targets,'show')
        showtar = targets.show;
    else
        showtar = 1:size(targets.rects,1);
    end
end

curWindow = screenInfo.curWindow;
white = [255 255 255];

if isfield(dotInfo,'movingDotColor')
  movingDotColor = dotInfo.movingDotColor;
else
  movingDotColor = white;
end

if isfield(dotInfo,'randomDotColor')
  randomDotColor = dotInfo.randomDotColor;
else
  randomDotColor = white;
end

rseed = screenInfo.rseed;

% Query the frame duration
ifi = Screen('GetFlipInterval', curWindow);

% This only matters if using mouse. If dotInfo.mouse doesn't exist, this is
% never checked.
if dotInfo.trialtype(2) == 2
    waitpress = 1; % 1 means wait for a mouse press
else
    waitpress = 0; % 0 means wait for release
end

% In order to find out if using keypress or mouse, all trials should have spacekey
% for abort, unless its a demo. Spacekey means end experiment after this trial - 
% sends abort message to experiment.

keys = [];
abort = [];
if isfield(dotInfo, 'keyLeft')
    keys = [dotInfo.keyLeft dotInfo.keyRight];
elseif isfield(dotInfo, 'keySpace')
    abort = nan;
end

% mouse
if isfield(dotInfo, 'mouse')
    mouse = dotInfo.mouse;
else
    mouse = [];
end

start_time = NaN;
end_time= NaN;
response = {NaN, NaN, NaN};
response_time = NaN;

if isfield(targets,'select')
    h = targets.select(:,1);
    k = targets.select(:,2);
    r = targets.select(:,3);
end

% Create the aperture square
%apRect = floor(createTRect(dotInfo.apXYD, screenInfo));

apD = dotInfo.apXYD(:,3); % diameter of aperture
center = repmat(screenInfo.center,size(dotInfo.apXYD(:,1)));

% Change x,y coordinates to pixels (y is inverted - pos on bottom, neg. on top)
center = [center(:,1) + dotInfo.apXYD(:,1)/10*screenInfo.ppd center(:,2) - ...
    dotInfo.apXYD(:,2)/10*screenInfo.ppd]; % where you want the center of the aperture
center(:,3) = dotInfo.apXYD(:,3)/2/10*screenInfo.ppd; % add diameter
d_ppd = floor(apD/10 * screenInfo.ppd);	% size of aperture in pixels
dotSize = dotInfo.dotSize; % probably better to leave this in pixels, but not sure

% ndots is the number of dots shown per video frame. Dots will be placed in a 
% square of the size of aperture.
% - Size of aperture = Apd*Apd/100  sq deg
% - Number of dots per video frame = 16.7 dots per sq deg/sec,
% When rounding up, do not exceed the number of dots that can be plotted in a 
% video frame (dotInfo.maxDotsPerFrame). maxDotsPerFrame was originally in 
% setupScreen as a field in screenInfo, but makes more sense in createDotInfo as 
% a field in dotInfo.
ndots = min(dotInfo.maxDotsPerFrame, ...
    ceil(16.7 * apD .* apD * 0.01 / screenInfo.monRefresh));

% determine update frequency of the two contents

checkFlipTimeSecs = 1/dotInfo.Hz_BG;
checkFlipTimeFrames = round(checkFlipTimeSecs / ifi);
frameCounter = 0;

RDMFlipTimeSecs = 1/dotInfo.Hz_RDM;
RDMFlipTimeFrames = round(RDMFlipTimeSecs / ifi);

%% get display size (for BG dots)

sizeX = screenInfo.screenRect(3);
sizeY = screenInfo.screenRect(4);

dontclear = screenInfo.dontclear;

% The main loop
frames = 0;
priorityLevel = MaxPriority(curWindow,'KbCheck');
Priority(priorityLevel);

% Make sure the fixation still on
for i = showtar
    Screen('FillOval',screenInfo.curWindow,targets.colors(i,:),targets.rects(i,:));
end

Screen('DrawingFinished',curWindow,dontclear);

%% get starting parameters

currentSegment = 1;
coh = dotInfo.coh{indCond}(currentSegment)./1000;
direction = dotInfo.dir{indCond}(currentSegment);
time = dotInfo.presTime{indCond}(currentSegment);
Contrast = dotInfo.Contrast(indCond);

% How dots are presented: 1st group of dots are shown in the first frame, a 2nd 
% group are shown in the second frame, a 3rd group shown in the third frame.
% Then in the next (4th) frame, some percentage of the dots from the 1st frame 
% are replotted according to the speed/direction and coherence. Similarly, the 
% same is done for the 2nd group, etc.

% initialize dot fields
for df = 1 : dotInfo.numDotField
    % dxdy is an N x 2 matrix that gives jumpsize in units on 0..1
    %   deg/sec * ap-unit/deg * sec/jump = ap-unit/jump
    dxdy{df} = repmat((dotInfo.speed(df)/10) * (10/apD(df)) * ...
        (3/screenInfo.monRefresh) * [cos(pi*direction(df)/180.0), ...
        -sin(pi*direction(df)/180.0)], ndots(df),1);    
    ss{df} = rand(ndots(df)*3, 2); % array of dot positions raw [x,y]
    % Divide dots into three sets
    Ls{df} = cumsum(ones(ndots(df),3)) + repmat([0 ndots(df) ndots(df)*2], ... 
        ndots(df), 1);
    loopi(df) = 1; % loops through the three sets of dots
end

%% set up loop and start it (all RDM changes within block)

TimeStamping = [];
StartTime = GetSecs();
BlockOnset = StartTime;

ExperimentProtocol = cell(0);
ExperimentProtocol = [ExperimentProtocol; {'BlockOnset'}, {StartTime}, {[]}, {[]}, {[]}, {coh}, {direction},{Contrast}]
ExperimentProtocol = [ExperimentProtocol; {'RDMUpdate'}, {StartTime}, {[]}, {[]}, {[]}, {coh}, {direction},{Contrast}]

% initiate response cue
KbQueueCreate; KbQueueStart;
while currentSegment <= numel(dotInfo.presTime{indCond})

    for df = 1:dotInfo.numDotField

        % update movement field with current direction
        dxdy{df} = repmat((dotInfo.speed(df)/10) * (10/apD(df)) * ...
        (3/screenInfo.monRefresh) * [cos(pi*direction(df)/180.0), ...
        -sin(pi*direction(df)/180.0)], ndots(df),1);    
        
        % ss is the matrix with 3 sets of dot positions, dots from the last 2 
        %   positions and current dot positions
        % Ls picks out the set (e.g., with 5 dots on the screen at a time, 1:5, 
        %   6:10, or 11:15)
        
        % Lthis has the dot positions from 3 frames ago, which is what is then
        Lthis{df}  = Ls{df}(:,loopi(df));
        
        % Moved in the current loop. This is a matrix of random numbers - starting 
        % positions of dots not moving coherently.
        this_s{df} = ss{df}(Lthis{df},:);
        
        % Update the loop pointer
        loopi(df) = loopi(df)+1;
        
        if loopi(df) == 4
            loopi(df) = 1;
        end
        
        % Compute new locations, how many dots move coherently
        L = rand(ndots(df),1) < coh(df);
        % Offset the selected dots
        this_s{df}(L,:) = bsxfun(@plus,this_s{df}(L,:),dxdy{df}(L,:));
        
        if sum(~L) > 0
            this_s{df}(~L,:) = rand(sum(~L),2);	% get new random locations for the rest
        end
        
        % Check to see if any positions are greater than 1 or less than 0 which 
        % is out of the square aperture, and replace with a dot along one of the
        % edges opposite from the direction of motion.
        N = sum((this_s{df} > 1 | this_s{df} < 0)')' ~= 0;
        
        if sum(N) > 0
            xdir = sin(pi*direction(df)/180.0);
            ydir = cos(pi*direction(df)/180.0);
            % Flip a weighted coin to see which edge to put the replaced dots
            if rand < abs(xdir)/(abs(xdir) + abs(ydir))
                this_s{df}(find(N==1),:) = [rand(sum(N),1),(xdir > 0)*ones(sum(N),1)];
            else
                this_s{df}(find(N==1),:) = [(ydir < 0)*ones(sum(N),1),rand(sum(N),1)];
            end
        end
        
        % Convert for plot
        this_x{df} = floor(d_ppd(df) * this_s{df});	% pix/ApUnit
        
        % It assumes that 0 is at the top left, but we want it to be in the 
        % center, so shift the dots up and left, which means adding half of the 
        % aperture size to both the x and y directions.
        dot_show{df} = (this_x{df} - d_ppd(df)/2)';
        
    end
    
    %% After all computations, flip to draw dots from the previous loop. 
    % For the first call, this doesn't draw anything.
    
    vbl = Screen('Flip', curWindow,0,dontclear);
    
    TimeStamping = [TimeStamping; vbl];
        
    %% update noise background
    
     if ~mod(frameCounter, checkFlipTimeFrames)
         tmp_BG = randperm(sizeX*sizeY,dotInfo.percentOfBG*(sizeX*sizeY));
         [pos(1,:), pos(2,:)] = ind2sub([sizeX, sizeY],tmp_BG);
         % remove dots within central aperture
         inCircle = sqrt((pos(1,:)-center(1,1)).^2 + (pos(2,:)-center(1,2)).^2) < center(df,3); % euclidean distance to center
         pos(:,inCircle) = NaN; clear inCircle;
         frameCounter = 0;
     end
    % Increment the counter
    frameCounter = frameCounter + 1;
	% Draw our texture to the screen
    Screen('DrawDots',curWindow,pos,1,Contrast.*randomDotColor);

    %% Draw random dots if it's time to update, although nothing is flipped yet    
    
    if ~mod(frameCounter, RDMFlipTimeFrames)
        for df = 1:dotInfo.numDotField
            % NaN out-of-circle dots                
            xyDis = dot_show{df};
            outCircle = sqrt(xyDis(1,:).^2 + xyDis(2,:).^2) + dotInfo.dotSize/2 > center(df,3);        
            dots2Display = dot_show{df};
            dots2Display(:,outCircle) = NaN;
            % Screen('DrawDots',curWindow,dots2Display,dotSize,movingDotColor,center(df,1:2));
            movingDots = dots2Display(:,L);
            randomDots = dots2Display(:,~L);
            if size(movingDots,2) > 0
              Screen('DrawDots',curWindow,movingDots,dotSize,movingDotColor,center(df,1:2));
            end
            if size(randomDots,2) > 0
              Screen('DrawDots',curWindow,randomDots,dotSize,randomDotColor,center(df,1:2));
            end
        end

        % Draw targets
        for i = showtar
            Screen('FillOval',screenInfo.curWindow,targets.colors(i,:),targets.rects(i,:));
        end
    end;
    
    %% check for responses
    
    [keyIsDown, firstPress, ~, ~, ~] = KbQueueCheck;
            
    % encode response
    % Note that encoding here might break the update frequency as
    % the computations take some time. They are necessary however
    % to give instant feedback. In any case, this should only
    % affect the cycle on which feedback is received.
    
    if keyIsDown == 1
        kp = find(firstPress);
        rt = firstPress(kp)-StartTime; % reference to onset of last interval
        
        % encode accuracy
        if kp == 80 && direction == 180 % response left
            tmp_response = 1;
            disp('Correct');
        elseif kp == 79 && direction == 0 % response right
            disp('Correct');
            tmp_response = 1;
        else
            tmp_response = 0;
            disp('Wrong');
        end;
        
        ExperimentProtocol = [ExperimentProtocol; {'Resp'}, {firstPress(kp)}, {rt}, {kp}, {tmp_response}, {coh}, {direction}, {Contrast}];
        
    end; % end of response encode loop
    
    %% Prepare next dots presentation
    
    % Tell PTB to get ready while doing computations for next dots presentation
    Screen('DrawingFinished',curWindow,dontclear);
    %Screen('BlendFunction', curWindow, GL_ONE, GL_ZERO);
    
    frames = frames + 1;
    %frameCounter = frameCounter + 1; % counter for checkerboards 
    
    if frames == 1     
        start_time = GetSecs;       
    end
       
    for df = 1 : dotInfo.numDotField
        % Update the dot position array for the next loop
        ss{df}(Lthis{df}, :) = this_s{df};
    end
    
    % Advance to next RDM state if time has passed
    if GetSecs()-StartTime >= dotInfo.presTime{indCond}(currentSegment)
        currentSegment = currentSegment + 1;
        coh = dotInfo.coh{indCond}(currentSegment)./1000;
        direction = dotInfo.dir{indCond}(currentSegment);
        time = dotInfo.presTime{indCond}(currentSegment);
        StartTime = GetSecs();
        ExperimentProtocol = [ExperimentProtocol; {'RDMUpdate'}, {StartTime}, {[]}, {[]}, {[]}, {coh}, {direction}, {Contrast}]
    end;
    
    % User may terminate the dots by pressing certain keyboard keys defined by
    % "keys". Pressing the escape key will exit the experiment
    if not(isempty(keys))
        [exitKeyPressed, ~, keyIsDown, secs, keyCode] = checkKeys(dotInfo);
        if keyIsDown
            % Exit experiment
            if exitKeyPressed
                response{3} = -1;
                return
            end
            % End trial, have response
            if numel(find(keyCode)) == 1 && any(keyCode(keys))
                response{3} = find(keyCode(keys));
                response_time = secs;
            end
        end
    end

    if ~isempty(mouse)
        [x,y,buttons] = GetMouse(curWindow);
        % check == 0 means exit dots, check == 1 means continuing showing dots
        check = 0;
        if buttons
            % mouse was pressed, if hold is on, and we know fixation position, 
            % make sure holding the correct place.
            if waitpress == 0
                if isfield(targets,'select')
                    check = checkPosition(x,y,h(1),k(1),r(1));
                end
            else
                % If hold is not on, and this is fixed duration, we don't care 
                % if the subject touches the screen. For reaction time, touching 
                % means exit dots.
                if dotInfo.trialtype(1) == 1
                    check = 1;
                end
            end   
        else
            % mouse was not pressed. If waiting for mouse press, continue paradigm
            if waitpress == 1
                check = 1;
            end
        end
        
        if ~check
            % For fixed duration, exiting early is always an error.
            if dotInfo.trialtype(1) == 1
                response{2} = 0;
            else
                % buttons is zero if we are doing reaction time where the subject 
                % has to hold during fixation, and releasing the mouse signifies 
                % end of the dots, otherwise should tell you the xy position. 
                % Eventually, I guess we should make it so we can use two mouse 
                % buttons as the answer.
                if buttons
                    response{2} = [x y];                    
                else
                    response{2} = 0;
                end
            end
            response_time = GetSecs;
        end
    end
end

% Present the last frame of dots
Screen('Flip',curWindow,0,dontclear);

% Erase the last frame of dots, but leave up fixation and targets (if targets 
% are up). Make sure the fixation still on.
showTargets(screenInfo,targets,showtar);

end_time = GetSecs;
Priority(0);