forked from Lif3line/Myo-MATLAB-Interface
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrealTimeFeatureExtractOptimised.m
110 lines (90 loc) · 3.32 KB
/
realTimeFeatureExtractOptimised.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
%% Pseudo-real-time EMG data extraction from Myo to MATLAB
% Extracts EMG data from a MYO to MATLAB via C++
% Requires MYO Connect to be running
% Runs a C++ executable in a command prompt
clear; clc; close all % House keep
%% Genmeral Setup
lineLength = 54;
numChannels = 8;
sampMax = 4000;
windowLength = 40;
curPosition = 0;
emgData = NaN([sampMax numChannels]); % Matrix for EMG data
mavData = NaN([(sampMax - windowLength) numChannels]); % Matrix for MAV feature
fileNameEMG = 'emg.txt'; % File for data transmission; again stop judging ^^
cmdWindowName = 'EMG Gather';
FileEMG = fopen(fileNameEMG,'w'); % Reset file
fclose(FileEMG);
system(['start /realtime "' cmdWindowName '" getMyoEmg.exe & exit &']) % Start (non-blocking) C thread
figure(1) % Do after cmd call to bring to foreground
set(gcf,'currentchar',']') % Used for exit on button press
%% Pause for handshake with myo connect and for data collection to begin
pause(1);
%% Pseudo-realtime extraction
% Get first timestamp from file (and check data gathering is working)
FileEMG = fopen(fileNameEMG,'r');
fseek(FileEMG,curPosition,-1);
fileDataRaw = fgetl(FileEMG);
if fileDataRaw == -1
system(['taskkill /f /fi "WindowTitle eq ' cmdWindowName '" /T & exit'])
close(gcf)
disp('Data acquisition not active')
return;
end
fileDataStrArray = strsplit(fileDataRaw,',');
startTime = str2double(fileDataStrArray(end));
fclose(FileEMG);
curSample = 1;
lastSample = 1;
while get(gcf,'currentchar') == ']' % While no button has been pressed
FileEMG = fopen(fileNameEMG,'r');
fseek(FileEMG,curPosition,-1);
fileDataRaw = ' ';
while ischar(fileDataRaw) % Extract new data from file
fileDataRaw = fgetl(FileEMG);
if numel(fileDataRaw) ~= lineLength % Break if last line incomplete (and seek back to start of that line
fseek(FileEMG,-numel(fileDataRaw) ,0);
break;
end
fileDataStrArray = strsplit(fileDataRaw,',');
curTime = str2double(fileDataStrArray(end));
emgData(curSample,:) = str2double(fileDataStrArray(1:numChannels));
if curSample >= windowLength % MAV feature extraction
mavData(curSample - windowLength + 1,:) = mean(abs(emgData(curSample - windowLength + 1:curSample,:)));
end
curSample = curSample + 1;
end
curPosition = ftell(FileEMG);
fclose(FileEMG);
if curSample - lastSample == 0 % Don't waste time drawing if no new data
continue;
else
lastSample = curSample;
end
% Plots
subplot(2,1,1)
plot(emgData)
ylim([-128 127])
xlim([1 sampMax])
title(['Sample frequency: ' num2str(curSample/(curTime - startTime))])
xlabel('Samples')
ylabel('Amplitude')
subplot(2,1,2)
plot(mavData)
ylim([0 127])
xlim([1 sampMax])
title([num2str(windowLength) ' Sample Window MAV'])
xlabel('Press any key to quit..')
ylabel('Amplitude')
drawnow
if curSample > sampMax % Clear arrays when large
curSample = 1;
lastSample = 1;
emgData = NaN([sampMax numChannels]);
mavData = NaN([(sampMax - windowLength) numChannels]);
startTime = curTime;
end
end
%% CLean up - target specific window made for this script
system(['taskkill /f /fi "WindowTitle eq ' cmdWindowName '" /T & exit'])
close(gcf)