forked from yeatmanlab/speech_contrasts_public
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCategorization_Practice.m
438 lines (319 loc) · 13.6 KB
/
Categorization_Practice.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
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
% This script presents the categorization task practice.
function Categorization_Practice(varargin)
% Pass in the parameters
if nargin < 1
SubjectCode = 'nnn';
test_cue = 'Sa_Sha';
stim_path = './Stimuli';
results_path = './Results';
single_interval = 1;
else
SubjectCode = varargin{1};
test_cue = varargin{2};
stim_path = varargin{3};
results_path = varargin{4};
single_interval = varargin{5};
end
% Call some default settings
PsychDefaultSetup(2);
InitializePsychSound
% Disable keys except the arrows
RestrictKeysForKbCheck([115, 117])
% Get the screen numbers
screens = Screen('Screens');
% Select the external screen if it is present, else revert to native screen
screenNumber = max(screens);
% Define colors
background = [83, 58, 113] ./ 255;
% Open an on screen window and color it
[window, ~] = PsychImaging('OpenWindow', screenNumber, background);
% Get the size of the onscreen window in pixels
[screenXpixels, screenYpixels] = Screen('WindowSize', window);
%% Load in sounds
stim_list_dir = './Stim_List';
stim_list_file_base = ['stimlist_' test_cue '.txt'];
list_base = read_list([stim_list_dir '/' stim_list_file_base]);
% get endpoints
% Which continua?
if strcmp(test_cue, 'Ba_Da')
end_pt_1_label = 'Ba';
end_pt_2_label = 'Da';
elseif strcmp(test_cue, 'Sa_Sha')
end_pt_1_label = 'Sha';
end_pt_2_label = 'Sa';
end
% first extreme
play_file = [stim_path '/' list_base{1}];
[audio, freq] = audioread(play_file);
cont_end_1 = [audio';audio'];
% second extreme
play_file = [stim_path '/' list_base{7}];
[audio, freq] = audioread(play_file);
cont_end_2 = [audio';audio'];
% Set interstumulus interval
ISI = 0.4;
ITI = 0.5; % time between trials
num_trials_each = 4; % How many times each stimulus will be presented
shuffled_list = permute_list([list_base(1) list_base(7)], num_trials_each);
%% Load in images
imageLocation = './Images';
if strcmp(test_cue, 'Ba_Da')
end_pt_1_image_name = 'sheep_1.png';
end_pt_2_image_name = 'sheep_2.png';
end_pt_1_image_name_glow = 'sheep_1_glow.png';
end_pt_2_image_name_glow = 'sheep_2_glow.png';
elseif strcmp(test_cue, 'Sa_Sha')
end_pt_1_image_name = 'snake_1.png';
end_pt_2_image_name = 'snake_2.png';
end_pt_1_image_name_glow = 'snake_1_glow.png';
end_pt_2_image_name_glow = 'snake_2_glow.png';
end
image_1 = imread([imageLocation '/' end_pt_1_image_name], 'BackgroundColor', background);
image_1_glow = imread([imageLocation '/' end_pt_1_image_name_glow], 'BackgroundColor', background);
image_2 = imread([imageLocation '/' end_pt_2_image_name], 'BackgroundColor', background);
image_2_glow = imread([imageLocation '/' end_pt_2_image_name_glow], 'BackgroundColor', background);
% Get size of images
[s11, s21, ~] = size(image_1);
[s12, s22, ~] = size(image_2);
aspect_ratio_1 = s21/s11;
aspect_ratio_2 = s22/s12;
imageHeights = 500;
imageWidth1 = imageHeights .* aspect_ratio_1;
imageWidth2 = imageHeights .* aspect_ratio_2;
imageTexture1 = Screen('MakeTexture', window, image_1);
imageTexture2 = Screen('MakeTexture', window, image_2);
imageTexture1_glow = Screen('MakeTexture', window, image_1_glow);
imageTexture2_glow = Screen('MakeTexture', window, image_2_glow);
% Get the feedback images
correct_image_name = 'correct.png';
incorrect_image_name = 'incorrect.png';
correct_image = imread([imageLocation '/' correct_image_name], 'BackgroundColor', background);
incorrect_image = imread([imageLocation '/' incorrect_image_name], 'BackgroundColor', background);
correctTexture = Screen('MakeTexture', window, correct_image);
incorrectTexture = Screen('MakeTexture', window, incorrect_image);
[s1, s2, ~] = size(correctTexture);
aspect_ratio = s2/s1;
feedback_Height = 500;
feedback_Width = feedback_Height .* aspect_ratio;
% make the destination rectangles for our image
dstRects = zeros(4, 2);
theRect1 = [0 0 imageWidth1 imageHeights];
theRect2 = [0 0 imageWidth2 imageHeights];
corRect = [0 0 feedback_Width feedback_Height];
dstRects(:,1) = CenterRectOnPointd(theRect1, screenXpixels/4, screenYpixels/2);
dstRects(:,2) = CenterRectOnPointd(theRect2, screenXpixels*(3/4), screenYpixels/2);
dstRects(:,3) = CenterRectOnPointd(corRect, screenXpixels*0.5, screenYpixels*0.5);
%% Set up where to save results
repeat_number = 1;
results_file_base = [SubjectCode '_categorization_practice_' test_cue '_' num2str(repeat_number) '.txt'];
results_file = [results_path '/' results_file_base];
% Check if this file already exists
while exist(results_file) == 2
%update the repeat number and then the file name
repeat_number = repeat_number + 1;
results_file_base = [SubjectCode '_categorization_practice_' test_cue '_' num2str(repeat_number) '.txt'];
results_file = [results_path '/' results_file_base];
end
output_pointer = fopen(results_file, 'w');
data_header_row = 'trial,stimulus,sound1,sound2,selection,RT';
%timestamp = datestr(datetime('now'));
timestamp = fix(clock);
fprintf(output_pointer, '%d-%d-%d,%d:%d:%d\n', timestamp(1),timestamp(2),timestamp(3),timestamp(4),timestamp(5),timestamp(6));
%fprintf(output_pointer, '%s\n', timestamp);
fprintf(output_pointer, '%s\n',data_header_row);
fclose(output_pointer);
%% Load up all the sounds in a buffer
for i = 1:length(shuffled_list)
% Select at random a sound from the continuum
play_file = [stim_path '/' shuffled_list{i}];
[audio, freq] = audioread(play_file);
test_wavedata{i} = [audio'; audio'];
% Want to know the stimulus step, for plotting our psychometric at the end
tmp_str = strsplit(shuffled_list{i}, {'_','.'});
stimulus_step(i) = str2double(tmp_str{4});
end
%% Make a vector to store the percent classified as end_pt_1 for plotting at the end
% How many steps are in the continuum?
num_steps_in_continuum = length(list_base);
psychometric = zeros(1, num_steps_in_continuum);
%% Open the default audio device
pahandle = PsychPortAudio('Open', [],[],0,freq,2);
%% ************************MAIN EXPERIMENT LOOP****************************
if single_interval == 0
for j = 1:length(shuffled_list)
%% Starting screen
% Draw two animals
Screen('DrawTextures', window, imageTexture1, [], dstRects(:,1));
Screen('DrawTextures', window, imageTexture2, [], dstRects(:,2))
% Flip to the screen
Screen('Flip', window);
%% Play the first end of the continuum
% Color the correct button
% Flip window and start playing sound
use_end = cont_end_1;
choice1 = end_pt_1_label;
Screen('DrawTextures', window, imageTexture1_glow, [], dstRects(:,1));
Screen('DrawTextures', window, imageTexture2, [], dstRects(:,2))
sound1_start = Screen('Flip', window);
PsychPortAudio('FillBuffer', pahandle, use_end);
PsychPortAudio('Start', pahandle, 1,[]);
PsychPortAudio('Stop', pahandle, 1);
WaitSecs(ISI);
%% Play the second end of the continuum
use_end = cont_end_2;
choice2 = end_pt_2_label;
Screen('DrawTextures', window, imageTexture1, [], dstRects(:,1));
Screen('DrawTextures', window, imageTexture2_glow, [], dstRects(:,2))
sound2_start = Screen('Flip', window);
PsychPortAudio('FillBuffer', pahandle, use_end);
PsychPortAudio('Start', pahandle, 1,[]);
PsychPortAudio('Stop', pahandle, 1);
WaitSecs(ISI);
%% Return to a neutral screen and play the third sound
% Draw two animals
Screen('DrawTextures', window, imageTexture1, [], dstRects(:,1));
Screen('DrawTextures', window, imageTexture2, [], dstRects(:,2))
sound3_start = Screen('Flip', window);
PsychPortAudio('FillBuffer', pahandle, test_wavedata{j});
PsychPortAudio('Start', pahandle, 1,[]);
PsychPortAudio('Stop', pahandle, 1);
reps_start_time = GetSecs;
keyPress = 0;
% Wait for a keystroke to terminate
while keyPress ==0
[keyPress, secs, keyCode] = KbCheck();
end
Response_time = secs - reps_start_time;
% Categorize as choice 1 or choice 2
kbNameResult = KbName(keyCode);
disp(kbNameResult)
if strcmp(kbNameResult,'DownArrow')
selection = end_pt_1_label;
elseif strcmp(kbNameResult,'RightArrow')
selection = end_pt_2_label;
psychometric(stimulus_step(j)) = psychometric(stimulus_step(j))+1;
else
selection = 'NA';
end
% Give feedback to the participant- correct or incorrect?
if strcmp(selection, end_pt_1_label) && stimulus_step(j) == 1
Screen('DrawTextures', window, correctTexture, [], dstRects(:,3));
elseif strcmp(selection, end_pt_2_label) && stimulus_step(j) == 7
Screen('DrawTextures', window, correctTexture, [], dstRects(:,3));
else
Screen('DrawTextures', window, incorrectTexture, [], dstRects(:,3));
end
Screen('Flip', window)
% Write to file
output_pointer = fopen(results_file, 'a');
fprintf(output_pointer, '%d,%s,%s,%s,%s,%d\n',...
j, ... %d
shuffled_list{j}, ... %s
choice1, ... %s
choice2, .... %s,
selection, ... %s
Response_time); %d
% Increment the psychometric function by
WaitSecs(ITI)
end
else
%% First, play through the stimuli a few times.
% Draw two animals
Screen('DrawTextures', window, imageTexture1, [], dstRects(:,1));
Screen('DrawTextures', window, imageTexture2, [], dstRects(:,2))
% Flip to the screen
Screen('Flip', window);
%% Play the first end of the continuum
% Color the correct button
% Flip window and start playing sound
use_end = cont_end_1;
Screen('DrawTextures', window, imageTexture1_glow, [], dstRects(:,1));
Screen('DrawTextures', window, imageTexture2, [], dstRects(:,2));
sound1_start = Screen('Flip', window);
PsychPortAudio('FillBuffer', pahandle, use_end);
PsychPortAudio('Start', pahandle, 1,[]);
PsychPortAudio('Stop', pahandle, 1);
WaitSecs(0.5);
PsychPortAudio('FillBuffer', pahandle, use_end);
PsychPortAudio('Start', pahandle, 1,[]);
PsychPortAudio('Stop', pahandle, 1);
WaitSecs(0.5);
PsychPortAudio('FillBuffer', pahandle, use_end);
PsychPortAudio('Start', pahandle, 1,[]);
PsychPortAudio('Stop', pahandle, 1);
WaitSecs(1);
%% Play the second end of the continuum
use_end = cont_end_2;
Screen('DrawTextures', window, imageTexture1, [], dstRects(:,1));
Screen('DrawTextures', window, imageTexture2_glow, [], dstRects(:,2))
sound2_start = Screen('Flip', window);
PsychPortAudio('FillBuffer', pahandle, use_end);
PsychPortAudio('Start', pahandle, 1,[]);
PsychPortAudio('Stop', pahandle, 1);
WaitSecs(0.5);
PsychPortAudio('FillBuffer', pahandle, use_end);
PsychPortAudio('Start', pahandle, 1,[]);
PsychPortAudio('Stop', pahandle, 1);
WaitSecs(0.5);
PsychPortAudio('FillBuffer', pahandle, use_end);
PsychPortAudio('Start', pahandle, 1,[]);
PsychPortAudio('Stop', pahandle, 1);
%% Return to a neutral screen and play the third sound
% Draw two animals
Screen('DrawTextures', window, imageTexture1, [], dstRects(:,1));
Screen('DrawTextures', window, imageTexture2, [], dstRects(:,2))
WaitSecs(2);
%% Now go into the loop
for j = 1:length(shuffled_list)
% Draw two animals
Screen('DrawTextures', window, imageTexture1, [], dstRects(:,1));
Screen('DrawTextures', window, imageTexture2, [], dstRects(:,2));
% Flip to the screen
Screen('Flip', window);
PsychPortAudio('FillBuffer', pahandle, test_wavedata{j});
PsychPortAudio('Start', pahandle, 1,[]);
PsychPortAudio('Stop', pahandle, 1);
reps_start_time = GetSecs;
keyPress = 0;
% Wait for a keystroke to terminate
while keyPress ==0
[keyPress, secs, keyCode] = KbCheck();
end
Response_time = secs - reps_start_time;
% Categorize as choice 1 or choice 2
kbNameResult = KbName(keyCode);
disp(kbNameResult)
if strcmp(kbNameResult,'DownArrow')
selection = end_pt_1_label;
elseif strcmp(kbNameResult,'RightArrow')
selection = end_pt_2_label;
psychometric(stimulus_step(j)) = psychometric(stimulus_step(j))+1;
else
selection = 'NA';
end
% Give feedback to the participant- correct or incorrect?
if strcmp(selection, end_pt_1_label) && stimulus_step(j) == 1
Screen('DrawTextures', window, correctTexture, [], dstRects(:,3));
elseif strcmp(selection, end_pt_2_label) && stimulus_step(j) == 7
Screen('DrawTextures', window, correctTexture, [], dstRects(:,3));
else
Screen('DrawTextures', window, incorrectTexture, [], dstRects(:,3));
end
Screen('Flip', window)
% Write to file
output_pointer = fopen(results_file, 'a');
fprintf(output_pointer, '%d,%s,%s,%s,%s,%d\n',...
j, ... %d
shuffled_list{j}, ... %s
'NA', ... %s
'NA', .... %s,
selection, ... %s
Response_time); %d
% Increment the psychometric function by
WaitSecs(ITI);
end
end
% Clear the screen
sca
PsychPortAudio('Close');
end