forked from bobvo23/flicker_stimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
square_flicker.m
119 lines (100 loc) · 3.42 KB
/
square_flicker.m
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
function square_flicker()
%%Initiate frequency
% Frames Period Freq. Simulated signal. 0 light. 1 dark
% [#] [ms] [Hz] [-]
% 3.0 50 .00 20.00 011
% 4.0 66.67 15.00 0011
% 5.0 83.33 12.00 00111
% 6.0 100.00 10.00 000111
% 7.0 116.67 8.57 0001111
% 8.0 133.33 7.50 00001111
% 9.0 150.00 6.66 0000 11111
%According to the paper 1 is blackbox, 0 is white
seven_five = [0 0 0 0 1 1 1 1];
ten = [0 0 0 1 1 1];
six_six = [0 0 0 0 1 1 1 1 1]; %[0 0 1 1 1];
eight_fiveseven = [0 0 0 1 1 1 1];%l
% initiate freq table
freq{1} = six_six; %4/8
freq{2} = seven_five; %3/6
freq{3} = eight_fiveseven; %3/5
freq{4} = ten; %4/7
%%Generate display matrixes for movies
% Find LCM of freq matrix to create equal matrixes for all freqs
lcmFreq = lcms([length(freq{1}),length(freq{2}),length(freq{3}),length(freq{4})]);
%Generate full movie matrix of frequency
for i=1:4
freqCombine(i,:) = repmat(freq{i},1,lcmFreq/length(freq{i}));
end
%Revert value because in Matlab 255 is white and 0 is black
freqCombine = 1 - freqCombine;
maxduration = 1000;
if nargin < 2
frequency = 5;
end
try
myScreen = max(Screen('Screens'));
%Priority(0);
%myScreen = 2;
[win,winRect] = Screen(myScreen,'OpenWindow',[],[0 0 900 900]);
[width, height] = RectSize(winRect);
% Background color dark green, just to make sure
Screen('FillRect',win,[0 127 0]);
%%Make movie
targetWidth = 100;
targetHeight = 100;
% make textures clipped to screen size
% Draw texture to screen: Draw 16 states or texture depens on the value of
screenMatrix = flickerTexture(width, height, targetWidth, targetHeight);
for i =1:16
texture(i) = Screen('MakeTexture', win, uint8(screenMatrix{i})*255);
end
% Define refresh rate.
ifi = Screen('GetFlipInterval', win);
%Run in this duration
deadline = GetSecs + maxduration;
% Preview texture briefly before flickering
% n.b. here we draw to back buffer
Screen('DrawTexture',win,texture(16));
VBLTimestamp = Screen('Flip', win, ifi);
WaitSecs(2);
% loop swapping buffers, checking keyboard, and checking time
% param 2 denotes "dont clear buffer on flip", i.e., we alternate
% our buffers cum textures
indexflip = 1;
% textureValue =0;
halfifi = 0.5*ifi;
vbl =0;
%% Start looping movie
Priority(1);
while (~KbCheck) && (GetSecs < deadline)
% Drawing
%Compute texture value based on display value from freq long matrixes
textureValue = freqCombine(:, indexflip) .* [1; 2; 4; 8];
textureValue = textureValue(4)+textureValue(3)+textureValue(2)+ textureValue(1) +1;
%Draw it on the back buffer
Screen('DrawTexture',win,texture(textureValue));
%Display current index
%Screen('DrawText', win, num2str(indexflip),400,400, 255);
%Tell PTB no more drawing commands will be issued until the next flip
Screen('DrawingFinished', win);
% Fliping
%Screen('Flip', win, vbl + halfifi);
%Flip ASAP
Screen('Flip', win);
indexflip = indexflip+1;
%Reset index at the end of freq matrix
if indexflip > lcmFreq
indexflip = 1;
%disp('over');
end
end
Priority(0);
frame_duration = Screen('GetFlipInterval', win);
Screen('CloseAll');
Screen('Close');
catch
Screen('CloseAll');
Screen('Close');
psychrethrow(psychlasterror);
end