forked from oliveirafhm/NCCSystem-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindowing.m
355 lines (302 loc) · 12 KB
/
windowing.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
% Signal windowing and filter (smooth and remove offset)
% Run after start.m
% PhD student - Fabio Henrique ([email protected]) - 30/01/2018
% Last modification: 05/04/2018
%% Load audio
[audioSignal, audioFS] = audioread('experiment beeps.mp3');
audioSignalLength = length(audioSignal);
timeAudio = (0:1/audioFS:audioSignalLength/audioFS)';
timeAudioLength = length(timeAudio);
% Fix different size vectors
if (audioSignalLength > timeAudioLength)
aDiff = audioSignalLength - timeAudioLength;
for i = 1:aDiff
timeAudio(timeAudioLength+i) = 0;
end
elseif (audioSignalLength < timeAudioLength)
aDiff = timeAudioLength - audioSignalLength;
for i = 1:aDiff
audioSignal(audioSignalLength+i) = 0;
end
end
% Play audio
%sound(audioSignal, audioFs);
%% Get audio onset
thresholdValue = 0.01; % y value
timeSkip = 0.6; % s
onsets = AudioOnset(audioSignal, audioFS, thresholdValue, timeSkip);
figure;
plot(timeAudio,audioSignal,timeAudio(onsets),audioSignal(onsets),'or');
title('Audio beeps','FontSize',18);
xlabel('Time (s)','FontSize',16);
ylabel('Normalized audio data','FontSize',16);
audioOnset = audioSignal(onsets);
timeOnsets = onsets/audioFS; % onset in seconds
close;
%% Show the oneside envelope alone
commandwindow;
envelopeSide = input('Choose the envelope side to proceed with the analysis (1 - lower and 2 - upper): ');
if envelopeSide == 2 % Upper
envIndex_ps1 = upperEnvIndex_ps1;
env_ps1 = upperEnv_ps1;
envIndex_ps2 = upperEnvIndex_ps2;
env_ps2 = upperEnv_ps2;
elseif envelopeSide == 1 % Lower
envIndex_ps1 = lowerEnvIndex_ps1;
env_ps1 = lowerEnv_ps1;
envIndex_ps2 = lowerEnvIndex_ps2;
env_ps2 = lowerEnv_ps2;
end
input('Do not close the first figure before windowing.[Enter to continue]\n');
fig1 = figure; % fig1: used to get points to sync with audio
% x-axis
h2(1) = subplot(2,1,1);
plot(time(envIndex_ps1),env_ps1,'r-');
title('The envelope of Sensor pair 1 (x-axis)','FontSize',18);
ylabel('Envelope of raw sensor output (v)','FontSize',16);
% y-axis
h2(2) = subplot(2,1,2);
plot(time(envIndex_ps2),env_ps2,'r-');
title('The envelope of Sensor pair 2 (y-axis)','FontSize',18);
ylabel('Envelope of raw sensor output (v)','FontSize',16);
xlabel('Time (s)','FontSize',16);
linkaxes(h2,'x');
%% Get points to sync with audio (should be executed only after x-axis envelope)
commandwindow;
chosenOnset = input('\nType the known onset: ');%3;
[xOnset, yOnset] = getpts(fig1);
plesseyTimeOnsets = ones(size(timeOnsets))*-1;
plesseyTimeOnsets(chosenOnset) = xOnset;
i = chosenOnset;
while i > 1
plesseyTimeOnsets(i-1) = plesseyTimeOnsets(i) - (timeOnsets(i) - timeOnsets(i-1));
i = i-1;
end
i = find(plesseyTimeOnsets == -1, 1);
while i <= length(timeOnsets)
plesseyTimeOnsets(i) = plesseyTimeOnsets(i-1) + (timeOnsets(i) - timeOnsets(i-1));
i = i+1;
end
ps1EnvTime = time(envIndex_ps1);
ps2EnvTime = time(envIndex_ps2);
% Delays the onsets, to better fit our experiment
% The delay was calculated based on audio beeps and human reaction time to
% sound
plesseyTimeOnsets = plesseyTimeOnsets - (2 + 0.2); % seconds
ps1Onsets = ones(size(plesseyTimeOnsets))*-1;
ps2Onsets = ones(size(plesseyTimeOnsets))*-1;
for i=1:length(ps1Onsets)
ps1Onsets(i) = find(ps1EnvTime < plesseyTimeOnsets(i),1,'last');
ps2Onsets(i) = find(ps2EnvTime < plesseyTimeOnsets(i),1,'last');
end
%% Highlights the windows from each movement type
% Audio onset index below the mean, used to exclude repeated movements
audioOnsetMean = find(audioOnset < mean(audioOnset));
ps1SelectedWindows = ps1Onsets(audioOnsetMean); %index of each window
ps2SelectedWindows = ps2Onsets(audioOnsetMean);
% Plot sensor pair 1 (x-axis) and 2 (y-axis) windowed envelope
figure;
h5(1) = subplot(2,1,1);
plot(ps1EnvTime(ps1SelectedWindows(1):ps1SelectedWindows(end)),...
env_ps1(ps1SelectedWindows(1):ps1SelectedWindows(end)));
line([ps1EnvTime(ps1SelectedWindows) ps1EnvTime(ps1SelectedWindows)],...
[min(env_ps1) max(env_ps1)], 'Color',[1 0 0], 'LineStyle', '-.');
title('Sensor pair 1 windowed envelope (x-axis)','FontSize',18);
ylabel('Envelope of raw sensor output (v)','FontSize',16);
h5(2) = subplot(2,1,2);
plot(ps2EnvTime(ps2SelectedWindows(1):ps2SelectedWindows(end)),...
env_ps2(ps2SelectedWindows(1):ps2SelectedWindows(end)));
line([ps2EnvTime(ps2SelectedWindows) ps2EnvTime(ps2SelectedWindows)],...
[min(env_ps2) max(env_ps2)], 'Color',[1 0 0], 'LineStyle', '-.');
title('Sensor pair 2 windowed envelope (y-axis)','FontSize',18);
ylabel('Envelope of raw sensor output (v)','FontSize',16);
xlabel('Time (s)','FontSize',16);
linkaxes(h5,'x');
%% Fix time and windows
ps1TimeFiltered = ps1EnvTime(ps1SelectedWindows(1):...
ps1SelectedWindows(end)) - ps1EnvTime(ps1SelectedWindows(1));
ps2TimeFiltered = ps2EnvTime(ps2SelectedWindows(1):...
ps2SelectedWindows(end)) - ps2EnvTime(ps2SelectedWindows(1));
timeFiltered = ps1TimeFiltered; %test
ps1WindowsFiltered = ps1SelectedWindows - ps1SelectedWindows(1)+1;
ps2WindowsFiltered = ps2SelectedWindows - ps2SelectedWindows(1)+1;
windowsFiltered = ps1WindowsFiltered; %test
% Value to be used in the sync process with other hardwares
initTimeTrash = ps1EnvTime(ps1SelectedWindows(1)-1); % In seconds
%% Signal filtering (remove offset and signal trash) - Plessey signal
plotFlag = 1;
% TODO: Fix smooth params to keep tremor signal
env_ps1_filtered = SmoothFilter(ps1TimeFiltered, ...
env_ps1(ps1SelectedWindows(1):ps1SelectedWindows(end)),plotFlag);
env_ps2_filtered = SmoothFilter(ps2TimeFiltered, ...
env_ps2(ps2SelectedWindows(1):ps2SelectedWindows(end)),plotFlag);
fileName
%% Load and prepare tremsen data ..
[tsFileName, tsPathName] = uigetfile('.txt', ...
'Select tremsen signal file');
tsFilePath = strcat(tsPathName, tsFileName);
tremsenData = importtremsenfile(tsFilePath);
commandwindow;
selectedCh = input('\nType used TremSen Channel (1, 2 or 3): ');
gyroYCh = [3,6,9];
gyroZCh = [4,7,10];
tsTime = tremsenData(:,1);
gyro3Y = tremsenData(:,gyroYCh(selectedCh));
gyro3Z = tremsenData(:,gyroZCh(selectedCh));
pulseA = tremsenData(:,40);
% pulseB = tremsenData(:,41);
tsFs = round(length(gyro3Y) / tsTime(end), 0);
commandwindow;
input('Do not close the following figure before select pulse.[Enter to continue]');
fig2 = figure;
h3(1) = subplot(2,1,1);
plot(tsTime,gyro3Z,tsTime,pulseA);
title('Raw gyro 3 (z-axis)','FontSize',18);
h3(2) = subplot(2,1,2);
plot(tsTime,gyro3Y,tsTime,pulseA);
title('Raw gyro 3 (y-axis)','FontSize',18);
xlabel('Time (s)','FontSize',16);
linkaxes(h3,'x');
%% Delete signal trash from selected sensor/axis of tremsen
[tsXOnset, tsYOnset] = getpts(fig2);
pulseFlag = tsXOnset;
tLag = pulseFlag + initTimeTrash;
gyro3Y = gyro3Y(tsTime > tLag);
gyro3Z = gyro3Z(tsTime > tLag);
tsTime = tsTime(tsTime > tLag);
tsTime = tsTime - tLag;
% endTime = timeFiltered(windowsFiltered(end));%Detected by means of the audio beeps
endTime = timeFiltered(end);
gyro3Y = gyro3Y(tsTime <= endTime);
gyro3Z = gyro3Z(tsTime <= endTime);
tsTime = tsTime(tsTime <= endTime);
figure;
h4(1) = subplot(2,1,1);
plot(tsTime,gyro3Z);
title('Cropped raw gyro 3 (z-axis)','FontSize',18);
h4(2) = subplot(2,1,2);
plot(tsTime,gyro3Y);
title('Cropped raw gyro 3 (y-axis)','FontSize',18);
xlabel('Time (s)','FontSize',16);
linkaxes(h4,'x');
% Fix window marks for tremsen
d = abs(bsxfun(@minus,tsTime,timeFiltered(windowsFiltered)'));
[~, tsWindows] = min(d);
% Estimate sample rate of tremsen
tsSampleRate = round(length(gyro3Y) / tsTime(end),0);
%% Signal filtering (remove offset and smooth signal) - Tremsen signal
plotFlag = 1;
gyro3Y_filtered = SmoothFilter(tsTime, gyro3Y, plotFlag);
gyro3Z_filtered = SmoothFilter(tsTime, gyro3Z, plotFlag);
%% Plot sensor pair 1 (x-axis) and 2 (y-axis) windowed envelope
% along with tremsen analyzed sensors/axis
figure;
% Plessey pair 1 (x-axis)
h6(1) = subplot(4,1,1);
plot(ps1TimeFiltered,env_ps1_filtered);
line([ps1TimeFiltered(ps1WindowsFiltered) ps1TimeFiltered(ps1WindowsFiltered)], ...
[min(env_ps1_filtered) max(env_ps1_filtered)], 'Color',[1 0 0], 'LineStyle', '-.');
title('Non-contact capacitive sensor (x-axis)','FontSize',20);
ylabel('Amplitude (V)','FontSize',20);
xlim([ps1TimeFiltered(1) ps1TimeFiltered(end)]);
% Tremsem G3.Z
h6(2) = subplot(4,1,2);
plot(tsTime,gyro3Z_filtered);
line([timeFiltered(windowsFiltered) timeFiltered(windowsFiltered)], ...
[min(gyro3Z_filtered) max(gyro3Z_filtered)], 'Color',[1 0 0], 'LineStyle', '-.');
title('Gyroscope (z-axis)','FontSize',20);
ylabel('\circ/s','FontSize',20);
% Plessey pair 2 (y-axis)
h6(3) = subplot(4,1,3);
plot(ps2TimeFiltered,env_ps2_filtered);
line([ps2TimeFiltered(ps2WindowsFiltered) ps2TimeFiltered(ps2WindowsFiltered)], ...
[min(env_ps2_filtered) max(env_ps2_filtered)], 'Color',[1 0 0], 'LineStyle', '-.');
title('Non-contact capacitive sensor (y-axis)','FontSize',20);
ylabel('Amplitude (V)','FontSize',20);
xlim([ps2TimeFiltered(1) ps2TimeFiltered(end)]);
% Tremsem G3.Y
h6(4) = subplot(4,1,4);
plot(tsTime,gyro3Y_filtered);
line([timeFiltered(windowsFiltered) timeFiltered(windowsFiltered)], ...
[min(gyro3Y_filtered) max(gyro3Y_filtered)], 'Color',[1 0 0], 'LineStyle', '-.');
title('Gyroscope (y-axis)','FontSize',20);
ylabel('\circ/s','FontSize',20);
xlabel('Time (s)','FontSize',20);
linkaxes(h6,'x');
%% Estimate peaks (for plessey and tremsen signal)
% nPeaksPerTask = [0 0 5 5 5 5 15 15 15 15];
% commandwindow;
% analysisWn = input('\n\nType the window number (task) for analysis (1-10): ');
% maxPeaks = nPeaksPerTask(analysisWn);
% nPeaksFlag = 0;
% peakProminence = 0;
% invertPlesseyFlag = 0;
% plotFlag = 1;
% sensorName = {'Plessey1','Gyro3Y','Gyro3Z'};
%
% % Peak params
% % min peak dist - same value for all sensors
% if maxPeaks == 5
% mpd = 1.2;
% elseif maxPeaks == 15
% mpd = 0.30;
% end
% mphP = 0.2; % min peak height
% mphGY = 0.15;
% mphGZ = 0.2;
%
% % Plessey
% window = windowsFiltered(analysisWn):windowsFiltered(analysisWn+1);
% wnPlesseySig = env_ps1_filtered(window);
% if max(wnPlesseySig) < abs(min(wnPlesseySig))*0.9 || invertPlesseyFlag
% wnPlesseySig = wnPlesseySig * -1;
% end
% % fprintf('\n-- %s --\n',sensorName{1});
% [pks{1}, locs{1}] = PeakFinder2(wnPlesseySig, meanEnvSampleRate,...
% mpd, mphP, maxPeaks, nPeaksFlag, peakProminence, plotFlag);
% if plotFlag, title([sensorName{1} ' signal peaks of task ' num2str(analysisWn)],'FontSize',18); end
%
% % Tremsen
% tsWindow = tsWindows(analysisWn):tsWindows(analysisWn+1);
%
% % fprintf('\n-- %s --\n',sensorName{2});
% [pks{2}, locs{2}] = PeakFinder2(gyro3Y_filtered(tsWindow), tsSampleRate,...
% mpd, mphGY, maxPeaks, nPeaksFlag, peakProminence, plotFlag);
% if plotFlag, title([sensorName{2} ' signal peaks of task ' num2str(analysisWn)],'FontSize',18); end
%
% % fprintf('\n-- %s --\n',sensorName{3});
% [pks{3}, locs{3}] = PeakFinder2(gyro3Z_filtered(tsWindow), tsSampleRate,...
% mpd, mphGZ, maxPeaks, nPeaksFlag, peakProminence, plotFlag);
% if plotFlag, title([sensorName{3} ' signal peaks of task ' num2str(analysisWn)],'FontSize',18); end
%
% % close(2:4)
%% Estimate features based on peaks
% printFlag = 1;
% fprintf('\nSource file: %s\n',fileName);
% for i = 1:length(sensorName)
% fprintf('\n-- Features of %s sensor of task %d --\n',sensorName{i}, analysisWn);
% [~,~,~,~] = PeakFeatures(pks{i}, locs{i}, printFlag);
% end
%% Save all figures
saveFigsScript;
close all;
%% Save all workspace data
matFileName = strsplit(fileName,'.');
matFileName = matFileName{1};
matFileName = [pathName matFileName '.mat'];
save(matFileName);
%% Save filtered signal to later feature analysis
% File name convention = name_subject_iteration
% fsFileName = input('Enter file name to save filtered signal and its windows: ', 's');
% % Example: FilteredSignal_1_1.mat
% eval([fsFileName '=' 'struct' ';']);
% eval([fsFileName '.' 'env_ps1_filtered' '=' 'env_ps1_filtered' ';']);
% eval([fsFileName '.' 'env_ps2_filtered' '=' 'env_ps2_filtered' ';']);
% eval([fsFileName '.' 'timeFiltered' '=' 'timeFiltered' ';']);
% eval([fsFileName '.' 'windowsFiltered' '=' 'windowsFiltered' ';']);
% eval([fsFileName '.' 'initTimeTrash' '=' 'initTimeTrash' ';']);
% save([fsFileName '.mat'], fsFileName);
%%
clear all;
clc;
%% Run featureAnalysis.m file to proceed