-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathimEpochFeedbackStimulus.m
More file actions
107 lines (95 loc) · 4.13 KB
/
Copy pathimEpochFeedbackStimulus.m
File metadata and controls
107 lines (95 loc) · 4.13 KB
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
% make the target sequence
tgtSeq=mkStimSeqRand(nSymbs,nSeq);
% make the stimulus display
fig=figure(2);
clf;
set(fig,'Name','Imagined Movement','color',[0 0 0],'menubar','none','toolbar','none','doublebuffer','on');
ax=axes('position',[0.025 0.025 .95 .95],'units','normalized','visible','off','box','off',...
'xtick',[],'xticklabelmode','manual','ytick',[],'yticklabelmode','manual',...
'color',[0 0 0],'DrawMode','fast','nextplot','replacechildren',...
'xlim',[-1.5 1.5],'ylim',[-1.5 1.5],'Ydir','normal');
stimPos=[]; h=[];
stimRadius=.5;
theta=linspace(0,pi,nSymbs); stimPos=[cos(theta);sin(theta)];
for hi=1:nSymbs;
h(hi)=rectangle('curvature',[1 1],'position',[stimPos(:,hi)-stimRadius/2;stimRadius*[1;1]],...
'facecolor',bgColor);
end;
% add symbol for the center of the screen
stimPos(:,nSymbs+1)=[0 0];
h(nSymbs+1)=rectangle('curvature',[1 1],'position',[stimPos(:,end)-stimRadius/4;stimRadius/2*[1;1]],...
'facecolor',bgColor);
set(gca,'visible','off');
% play the stimulus
set(h(:),'facecolor',bgColor);
% Wait for key-press to being stimuli
t=text(mean(get(ax,'xlim')),mean(get(ax,'ylim')),imInstruct,'HorizontalAlignment','center','color',[0 1 0],...
'fontunits','pixel','FontSize',.05*wSize(4));
% wait for button press to continue
drawnow;
waitforbuttonpress;
delete(t);
drawnow;
sendEvent('stimulus.testing','start');
% initialize the state so don't miss classifier prediction events
status=buffer('wait_dat',[-1 -1 -1],buffhost,buffport); state =[status.nsamples status.nevents 0];
endTesting=false; dvs=[];
for si=1:nSeq;
if ( ~ishandle(fig) || endTesting ) break; end;
sleepSec(intertrialDuration);
% show the screen to alert the subject to trial start
set(h(:),'faceColor',bgColor);
set(h(end),'facecolor',fixColor); % red fixation indicates trial about to start/baseline
drawnow;% expose; % N.B. needs a full drawnow for some reason
sendEvent('stimulus.baseline','start');
sleepSec(baselineDuration);
sendEvent('stimulus.baseline','end');
% show the target
fprintf('%d) tgt=%d : ',si,find(tgtSeq(:,si)>0));
set(h(tgtSeq(:,si)>0),'facecolor',tgtColor);
set(h(tgtSeq(:,si)<=0),'facecolor',bgColor);
set(h(end),'facecolor',tgtColor); % green fixation indicates trial running
drawnow;% expose; % N.B. needs a full drawnow for some reason
sendEvent('stimulus.target',find(tgtSeq(:,si)>0));
sendEvent('stimulus.trial','start');
sleepSec(trialDuration);
% wait for classifier prediction event
if( verb>0 ) fprintf(1,'Waiting for predictions\n'); end;
[devents,state,nevents,nsamples]=buffer_newevents(buffhost,buffport,state,'classifier.prediction',[],trlen_ms);
% do something with the prediction (if there is one), i.e. give feedback
if( isempty(devents) ) % extract the decision value
fprintf(1,'Error! no predictions, continuing');
else
dv = devents(end).value;
if ( numel(dv)==1 )
if ( dv>0 && dv<=nSymbs && isinteger(dv) ) % dvicted symbol, convert to dv equivalent
tmp=dv; dv=zeros(nSymbs,1); dv(tmp)=1;
else % binary problem, convert to per-class
dv=[dv -dv];
end
end
% give the feedback on the predicted class
prob=1./(1+exp(-dv)); prob=prob./sum(prob);
if ( verb>=0 )
fprintf('dv:');fprintf('%5.4f ',dv);fprintf('\t\tProb:');fprintf('%5.4f ',prob);fprintf('\n');
end;
[ans,predTgt]=max(dv); % prediction is max classifier output
set(h(:),'facecolor',bgColor);
set(h(predTgt),'facecolor',fbColor);
drawnow;
sendEvent('stimulus.predTgt',predTgt);
end % if classifier prediction
sleepSec(feedbackDuration);
% reset the cue and fixation point to indicate trial has finished
set(h(:),'facecolor',bgColor);
% also reset the position of the fixation point
drawnow;
sendEvent('stimulus.trial','end');
end % loop over sequences in the experiment
% end training marker
sendEvent('stimulus.testing','end');
if ( ishandle(fig) )
text(mean(get(ax,'xlim')),mean(get(ax,'ylim')),{'That ends the training phase.','Thanks for your patience'},'HorizontalAlignment','center',...
'color',[0 1 0],'fontunits','pixel','FontSize',.05*wSize(4));
pause(3);
end