~ubuntu-branches/ubuntu/trusty/psychtoolbox-3/trusty-proposed

« back to all changes in this revision

Viewing changes to Psychtoolbox/PsychDemos/ECVP2013/HelloGaborArrayDemo.m

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-11-19 23:34:50 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20131119233450-f7nf92vb8qavjmk8
Tags: 3.0.11.20131017.dfsg1-3
Upload to unsable since fresh glew has arrived to sid!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
% GaborArrayDemo
 
2
 
 
3
% Written by Peter Scarfe
 
4
 
 
5
PsychDefaultSetup(2);
 
6
 
 
7
% Screen Number
 
8
screenNumber = max(Screen('Screens'));
 
9
 
 
10
% Define black, white and grey
 
11
white = WhiteIndex(screenNumber);
 
12
grey = GrayIndex(screenNumber);
 
13
black = BlackIndex(screenNumber);
 
14
 
 
15
% Open the screen
 
16
PsychImaging('PrepareConfiguration');
 
17
PsychImaging('AddTask', 'General', 'FloatingPoint32Bit');
 
18
[window, windowRect] = PsychImaging('OpenWindow', screenNumber, grey);
 
19
 
 
20
% Query the frame duration
 
21
ifi = Screen('GetFlipInterval', window);
 
22
 
 
23
% Maximum priority level
 
24
topPriorityLevel = MaxPriority(window);
 
25
 
 
26
% Get the centre coordinate of the window
 
27
[xCenter, yCenter] = RectCenter(windowRect);
 
28
 
 
29
%--------------------
 
30
% Gabor information
 
31
%--------------------
 
32
 
 
33
% Dimensions
 
34
gaborDimPix = 55;
 
35
 
 
36
% Sigma of Gaussian
 
37
sigma = gaborDimPix / 6;
 
38
 
 
39
% Obvious Parameters
 
40
orientation = 90;
 
41
contrast = 0.5;
 
42
aspectRatio = 1.0;
 
43
 
 
44
% Spatial Frequency (Cycles Per Pixel)
 
45
% One Cycle = Grey-Black-Grey-White-Grey i.e. One Black and One White Lobe
 
46
numCycles = 3;
 
47
freq = numCycles / gaborDimPix;
 
48
 
 
49
% Build a procedural gabor texture
 
50
gabortex = CreateProceduralGabor(window, gaborDimPix, gaborDimPix, [], [0.5 0.5 0.5 0.0], 1, 0.5);
 
51
 
 
52
% Positions of the Gabors
 
53
dim = 8;
 
54
[x, y] = meshgrid(-dim:dim, -dim:dim);
 
55
 
 
56
% Calculate the distance in "Gabor numbers" of each gabor from the center
 
57
% of the array
 
58
dist = sqrt(x.^2 + y.^2);
 
59
 
 
60
% Cut out an inner annulus
 
61
innerDist = 3.5;
 
62
x(dist <= innerDist) = nan;
 
63
y(dist <= innerDist) = nan;
 
64
 
 
65
% Cut out an outer annulus
 
66
outerDist = 10;
 
67
x(dist >= outerDist) = nan;
 
68
y(dist >= outerDist) = nan;
 
69
 
 
70
% Select only the finite values
 
71
x = x(isfinite(x));
 
72
y = y(isfinite(y));
 
73
 
 
74
% Center the annulus coordinates in the centre of the screen
 
75
xPos = x .* gaborDimPix + xCenter;
 
76
yPos = y .* gaborDimPix + yCenter;
 
77
 
 
78
% Count how many Gabors there are
 
79
nGabors = numel(xPos);
 
80
 
 
81
% Make the destination rectangles for all the Gabors in the array
 
82
baseRect = [0 0 gaborDimPix gaborDimPix];
 
83
allRects = nan(4, nGabors);
 
84
for i = 1:nGabors
 
85
    allRects(:, i) = CenterRectOnPointd(baseRect, xPos(i), yPos(i));
 
86
end
 
87
 
 
88
% Drift speed for the 2D global motion
 
89
degPerSec = 360 * 4;
 
90
degPerFrame =  degPerSec * ifi;
 
91
 
 
92
% Randomise the Gabor orientations and determine the drift speeds of each gabor.
 
93
% This is given by multiplying the global motion speed by the cosine
 
94
% difference between the global motion direction and the global motion.
 
95
% Here the global motion direction is 0. So it is just the cosine of the
 
96
% angle we use. We re-orientate the array when drawing
 
97
gaborAngles = rand(1, nGabors) .* 180 - 90;
 
98
degPerFrameGabors = cosd(gaborAngles) .* degPerFrame;
 
99
 
 
100
% Randomise the phase of the Gabors and make a properties matrix. We could
 
101
% if we want have each Gabor with different properties in all dimensions.
 
102
% Not just orientation and drift rate as we are doing here.
 
103
% This is the power of using procedural textures
 
104
phaseLine = rand(1, nGabors) .* 360;
 
105
propertiesMat = repmat([NaN, freq, sigma, contrast, aspectRatio, 0, 0, 0], nGabors, 1);
 
106
propertiesMat(:, 1) = phaseLine';
 
107
 
 
108
% Perform initial flip to gray background and sync us to the retrace:
 
109
vbl = Screen('Flip', window);
 
110
 
 
111
% Numer of frames to wait before re-drawing
 
112
waitframes = 1;
 
113
 
 
114
% Animation loop
 
115
while ~KbCheck
 
116
 
 
117
    % Set the right blend function for drawing the gabors
 
118
    Screen('BlendFunction', window, 'GL_ONE', 'GL_ZERO');
 
119
 
 
120
    % Batch draw all of the Gabors to screen
 
121
    Screen('DrawTextures', window, gabortex, [], allRects, gaborAngles - 90, [], [], [], [],...
 
122
        kPsychDontDoRotation, propertiesMat');
 
123
 
 
124
    % Change the blend function to draw an antialiased fixation point
 
125
    % in the centre of the array
 
126
    Screen('BlendFunction', window, 'GL_SRC_ALPHA', 'GL_ONE_MINUS_SRC_ALPHA');
 
127
 
 
128
    % Draw the fixation point
 
129
    Screen('DrawDots', window, [xCenter; yCenter], 10, black, [], 2);
 
130
 
 
131
 
 
132
    % Flip our drawing to the screen
 
133
    vbl = Screen('Flip', window, vbl + (waitframes - 0.5) * ifi);
 
134
 
 
135
    % Increment the phase of our Gabors
 
136
    phaseLine = phaseLine + degPerFrameGabors;
 
137
    propertiesMat(:, 1) = phaseLine';
 
138
 
 
139
end
 
140
 
 
141
% Wait for keyboard press
 
142
KbStrokeWait;
 
143
 
 
144
% Clean up
 
145
sca;