Randomize images presented together with a sound to measure reaction times
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi everyone, I created some code for an experiment to do through psychtoolbox. The intent of the code is to present images. During the slideshow of each image, low-pitched or high-pitched audio is presented. My intent is to measure the reaction times of responding to a question about sound quality, immediately after the presentation of image with sound.
How do I randomize the images, if each image must appear at least once with the low sound and once with the high sound?
Thanks
% Clear the workspace and the screen
sca;
close all;
clear;
% Here we call some default settings for setting up Psychtoolbox
PsychDefaultSetup(2);
Screen('Preference', 'SkipSyncTests', 1);
% Get the screen numbers. This gives us a number for each of the screens attached to our computer.
screens = Screen('Screens');
screenNumber = max(screens);
% Define black and white (white will be 1 and black 0).
white = WhiteIndex(screenNumber);
black = BlackIndex(screenNumber);
grey = white / 2;
% Open an on screen window using PsychImaging and color it grey.
[window, rect] = PsychImaging('OpenWindow', screenNumber, grey);
%%%%%%FIX%%%%%%
[X,Y] = RectCenter(rect); % X e Y = coordinate centro dello schermo
% creo stimoli visivi fuori dal loop
fixcross = [X-1,Y-15,X+1,Y+15; X-15,Y-1,X+15 ,Y+1]; % ad esempio la croce di fissazione
redcross = [255 0 0]; % e il suo colore, per esempio
%%%%%%%%%%%%%%%
%RANDOMIZING
rng('shuffle');
%%%%%%%%%%
% Impostazione dei parametri per i suoni
fs = 44100; % Frequenza di campionamento
duration = 1; % Durata del suono in secondi
freq_high = 1000; % Frequenza del suono acuto (in Hz)
freq_low = 300; % Frequenza del suono grave (in Hz)
% Generazione dei suoni
t = 0:1/fs:duration;
sound_high = sin(2*pi*freq_high*t);
sound_low = sin(2*pi*freq_low*t);
%impostazioni immagini
myFolder = 'C:\Users\\OneDrive\Desktop\MATLAB\\';
image_files = dir(fullfile(myFolder, '*.png'));
images = cell(size(image_files)); %{image_files.name};
% preparazione loop per la presentazione delle immagini e dei suoni
num_trials = 6;
num_images = length(image_files); %(images);
reaction_times = zeros(num_trials, num_images * 2); % Per salvare i tempi di reazione
responses = zeros(num_trials, num_images * 2); % Per salvare le risposte (1 = acuto, 0 = grave)
%istruzioni
tsize = 40; % text size
Screen('TextSize', window, tsize);
Screen('DrawText', window, 'ISTRUZIONI', 30 , 100, [255 0 0]);
Screen('DrawText', window, 'ciao', 30 , 200, [0 0 0]);
Screen('DrawText', window, 'ciao', 30 , 300, [0 0 0]);
Screen('DrawText', window, 'ciao', 30 , 400, [0 0 0]);
Screen('DrawText', window, 'ciao', 30 , 800, [0 0 0]);
Screen('DrawText', window, 'ciao', 30 , 900, [0 0 0]);
Screen('Flip',window);
Screen('FillRect', window, 127);
KbWait;
shuffled_indices = randperm(num_images);
shuffled_images = images(shuffled_indices);
%LOOP
for i = 1:1 %1:num_trials
%%%%%%%%%%%% RANDOMIZING
for h = shuffled_indices %1:num_images
Screen('FillRect', window, redcross, fixcross');
Screen('Flip', window, 127);
WaitSecs(0.5)
baseFileName = image_files(h).name;
fullFileName = fullfile(image_files(h).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imageDisplay = Screen('MakeTexture', window , imageArray);
Screen('DrawTexture', window, imageDisplay);
Screen('Flip',window);
%WaitSecs(0.2)
% Riproduzione del suono acuto
sound(sound_high, fs);
% Misurazione del tempo di reazione
start_time = GetSecs;
response = NaN;
while isnan(response)
[keyIsDown, secs, keyCode] = KbCheck;
if keyIsDown
if keyCode(KbName('RightArrow'))
response = 1; % Acuto
elseif keyCode(KbName('LeftArrow'))
response = 0; % Grave
end
reaction_times(i, (h-1)*2+1) = secs - start_time;
responses(i, (h-1)*2+1) = response;
end
end
end
for l = shuffled_indices %1:num_images
Screen('FillRect', window, redcross, fixcross');
Screen('Flip', window, 127);
WaitSecs(0.5)
baseFileName = image_files(l).name;
fullFileName = fullfile(image_files(l).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imageDisplay = Screen('MakeTexture', window , imageArray);
Screen('DrawTexture', window, imageDisplay);
Screen('Flip',window);
%WaitSecs(0.2)
% Riproduzione del suono acuto
sound(sound_low, fs);
% Misurazione del tempo di reazione
start_time = GetSecs;
response = NaN;
while isnan(response)
[keyIsDown, secs, keyCode] = KbCheck;
if keyIsDown
if keyCode(KbName('RightArrow'))
response = 1; % Acuto
elseif keyCode(KbName('LeftArrow'))
response = 0; % Grave
end
reaction_times(i, (l-1)*2+1) = secs - start_time;
responses(i, (l-1)*2+1) = response;
end
end
end
end
% Salvataggio dei dati
%save('reaction_times.mat', 'reaction_times', 'responses');
% Now we have drawn to the screen we wait for a keyboard button press (any key) to terminate.
KbStrokeWait;
% Clear the screen.
sca;
댓글 수: 0
채택된 답변
Michael VanMeter
2024년 6월 4일
Why not create a table that has all the required permutations of your trials and then randomize that. You can put your responses and reaction times right into the table as well.
T = table( ...
[image_files;image_files], ...
[ones([size(image_files,1) 1]) * sound_high ; ones([size(image_files,1) 1]) * sound_low], ...
NaN([size(image_files,1) * 2 1]), ...
NaN([size(image_files,1) * 2 1]), ...
'VariableNames',{'File','Fequency','Response','ReactionTime'});
shuffled_indices = randperm(height(T));
for i = shuffled_indices
Screen('FillRect', window, redcross, fixcross');
Screen('Flip', window, 127);
WaitSecs(0.5)
baseFileName = T.File(i).name;
fullFileName = fullfile(T.File(i).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
imageDisplay = Screen('MakeTexture', window , imageArray);
Screen('DrawTexture', window, imageDisplay);
Screen('Flip',window);
%WaitSecs(0.2)
% Riproduzione del suono acuto
sound(T.Fequency(i), fs);
% Misurazione del tempo di reazione
start_time = GetSecs;
while isnan(response)
[keyIsDown, secs, keyCode] = KbCheck;
if keyIsDown
if keyCode(KbName('RightArrow'))
response = 1; % Acuto
elseif keyCode(KbName('LeftArrow'))
response = 0; % Grave
end
T.ReactionTime(i) = secs - start_time;
T.Response(i) = response;
end
end
end
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!