-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.m
74 lines (64 loc) · 2.5 KB
/
application.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
function application(hdr, buffhost, buffport, calibrationDataPath)
buffer('con',[],buffhost,buffport);
load(calibrationDataPath,'collectedData','collectedEvents','electrodes','reference');
% train the classifier
[modelsProb, classValuesProb] = train_classification_CSP_prob(collectedData, collectedEvents);
lastPredictions = zeros(1,11); %starts with a majotity of baselines
currCommand = mode(lastPredictions);
message = '';
segmLen = round(0.25*hdr.fSample);%250ms
stat = buffer('wait_dat',[1 Inf 1000],buffhost,buffport);
curr = stat.nSamples;
myFig = figure;
tic();
while isvalid(myFig)
status = buffer('wait_dat',[curr+segmLen+1 Inf 4000],buffhost,buffport); %waits until the right amount of data is arrived (or timeout)
if status.nSamples < curr+segmLen
fprintf('Buffer stall detected...\n');
pause(1);
curr=status.nSamples;
continue;
elseif status.nSamples > curr+1*hdr.fSample % missed some samples of data (missing less then 1s is still reasonable)s
fprintf('Warning: Can''t keep up with the data!\n%d Dropped samples...\n',status.nSamples-segmLen-1-curr);
curr=status.nSamples - segmLen-1; % jump to the current time
end
d = buffer('get_dat',[curr curr+segmLen-1],buffhost,buffport);
newDat = d.buf(electrodes,:);
curr = curr+segmLen;
%Application goes HERE
pause(0.01);
if ~isempty(reference)
refSig = d.buf(reference,:);
newDat = newDat - refSig;
end
fprintf('Class: ');
predictedProb = apply_classification_CSP_prob(newDat, modelsProb, classValuesProb, 0.23);
lastPredictions(1:end-1) = lastPredictions(2:end);
lastPredictions(end) = predictedProb;
newCommand = mode(lastPredictions);
switch newCommand %uses the most frequent class predicted in the last period (basically it is a low pass filter)
case 0
message = 'stop';
fprintf('none');
case 1
message = 'left';
fprintf('left');
case 2
message = 'right';
fprintf('right');
case 3
message = 'forward';
fprintf('forward');
end
fprintf('\n');
figure(myFig);
histogram(lastPredictions,[-0.5 0.5 1.5 2.5 3.5]);
ylim([0 length(lastPredictions)]);
%%% send message to Unity %%%
if (newCommand ~= currCommand || toc() > 30) && toc() > 3%newCommand ~= 0 &&
tic();
currCommand = newCommand;
SendMessageFromMatlab(message);
end
%Application ends HERE
end