-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main_BOLDThermometer.m
148 lines (106 loc) · 4.15 KB
/
Main_BOLDThermometer.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
%% NEUROFEEDBACK THERMOMETER
% DESCRIPTIVE TEXT
% EXPLAIN HOW FEEDBACK IS CALCULATED
% TODO
%% CHECKLIST
% * NeuroElf
% * TBV Network Plugin v1.71
% * Pre-Load ROI
% * AutoHide Windows TaskBar
%% Start Clean
clear, clc;
close all;
%% Configuration
addpath('utils')
addpath('functions')
addpath('tbvinterface')
% ===== Select IP =================
configs.TBV_IP = '192.168.238.189'; % MRI Signal-PC
%configs.TBV_IP = 'localhost'; % Computer with TBV IPk
% ===== Select TR and wait time ===
TR = 1; %in seconds
wait_time = 0.05; %in seconds
% ====== Select PRT path ==========
prtPath = 'prt';
prtName = 'Task01_Run01.prt';
% ===== Select NF parameters =====
selected_roi = 1;
maxPSC = 2.5;
ignore = 5;
updateThreshold = 3; %Only updates after having this number of points after shiftBegin
% ===== Turn On/Off Feedback
FEEDBACK = true;
%% Open TBV Connection
configs.TBV_PORT = 55555;
tbvNetInt = TBVNetworkInterface( TBVclient( configs.TBV_IP, configs.TBV_PORT ) );
tbvNetInt.createConnection();
%% THERMOMETER Display Configuration
% Thermometer size and range
sz = [700 170];
Trange = [0 1 1];
[ hAx ] = startThermometer( sz , Trange );
%% WAIT FOR DATA
[ n_rois , currentTime , expectedTime ] = waitForData( tbvNetInt , wait_time );
%% INITIALIZE Variables and NF Parameters
shiftBegin = 6 / TR;
shiftEnd = shiftBegin / 3;
last = ignore + 1;
time = 0;
counter = 0;
maxcounter = 100;
ROImeans = zeros(expectedTime,n_rois);
SignalVar = zeros(expectedTime,1);
Baseline = zeros(expectedTime,1);
BaselineIndexes = zeros(expectedTime,1);
BaselineIndexesUpdate = zeros(expectedTime,1);
% Read PRT file
[ cond_names , intervalsPRT , intervals , baseCondIndex ] = readProtocol( prtPath , prtName, expectedTime , TR );
for i = 1:size(intervalsPRT.(cond_names{baseCondIndex}),1)-1
BaselineIndexes(intervalsPRT.(cond_names{baseCondIndex})(i,1)+shiftBegin:intervalsPRT.(cond_names{baseCondIndex})(i,2)+shiftEnd) = 1;
BaselineIndexesUpdate(intervalsPRT.(cond_names{baseCondIndex})(i,1)+shiftBegin+shiftEnd+updateThreshold:intervalsPRT.(cond_names{baseCondIndex})(i,2)+shiftEnd) = 1;
end
BaselineIndexes(1:ignore) = 0;
BaselineIndexesUpdate(1:ignore+shiftBegin+updateThreshold) = 0;
blockDur = intervalsPRT.(cond_names{baseCondIndex})(1,2)-intervalsPRT.(cond_names{baseCondIndex})(1,1)+1;
%% TIME Iteration
while time < expectedTime
if time == currentTime
if counter == maxcounter
fprintf('ERROR: No new point received after %i seconds.\n',wait_time*maxcounter);
beep, close all;
break;
end
pause(wait_time)
counter = counter + 1;
else
%---Get Mean ROI
ROImeans(time+1,:) = getMeanROI(n_rois,tbvNetInt,time);
if time+1 > ignore %Ignore first 5 data points
temp = find(BaselineIndexes(1:time+1) == 1);
if BaselineIndexesUpdate(time+1)
Baseline(time+1) = mean( ROImeans( last-shiftBegin+1 : time+1 , selected_roi ) );
else
last = time+1;
if time+1 == ignore + 1
Baseline(time+1) = ROImeans( time , selected_roi );
else
Baseline(time+1) = Baseline(time);
end
end
SignalVar(time+1) = calcSignalVar( Baseline(time+1) , ROImeans(time+1,selected_roi) , maxPSC );
end
% Thermometer Title - Condition Name
if intervals(time+1) == 1; fontS = 25; else fontS = 30; end;
title(cond_names{intervals(time+1)},'FontSize',fontS,'Color','w')
if FEEDBACK % False for Train and Transfer runs
if time+1 > blockDur && sum(BaselineIndexesUpdate(1:time+1)) > 0 %Suppress Feedback during first Baseline (& Imag) block
measure = str2num(sprintf('%.1f',SignalVar(time+1))); %Value rounded to 0.x (round() does not exist in 2014b)
thermometer(hAx,measure);
end
end
fprintf('Time %d\n',time+1);
time = time + 1;
counter = 0;
end
currentTime = tbvNetInt.tGetCurrentTimePoint;
end