-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataProcessing.m
151 lines (107 loc) · 4.5 KB
/
dataProcessing.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
%% Dataprocessing script
% This script processes the raw data.
% Inputs:
% cfT = cutoff frqueqency for torque
% cfV = cutoff frqueqency for velocity
% cfA = cutoff frqueqency for acceleration
% orderT = order of Butterworth filter for torque
% orderV = order of Butterworth filter for velocity
% orderA = order of Butterworth filter for acceleration
% cutTimeBefore = remove the time before this value
% cutTimeAfter = remove the time after this value
% pointNumber = approximate number of wished data points
% time = raw time data
% pos = raw position data
% vel = raw velocity data
% torque = raw torque data
% Outputs:
% i = actual number of data points
% t = processed time data
% q = processed position data
% qdot = processed velocity data
% qddot = processed acceleration data
% torque = processed torque data
% Author: Daniel Haugk, 2024, University of Michigan
function [i,t,q,qdot,qddot,torq] = dataProcessing(time,pos,vel,torque,cfT,cfV,cfA,cutTimeBefore,cutTimeAfter,orderT,orderV,orderA,pointNumber)
%empty data for usage of variables
t = [];
q = [];
%clip time after/before cutTime seconds
clipAfter = cutTimeAfter;
clipBefore = cutTimeBefore;
% number of joints
nj = length(pos(:,1));
if clipBefore == 0
% case if time starts at 0
% remove time after "cutTimeAfter" seconds
time(time>clipAfter) = [];
% number of datapoints
i = length(time)-1;
% downsample factor. Approximates the number of points someone wants to keep.
ds = ceil(i/pointNumber);
% time between 2 measurements. 1/dt is the frequency.
dt = time(end)-time(end-1);
% approximate acceleration and filter it.
qddots = centralDifference(vel,dt,cfA,orderA);
% set the initial acceleration to 0.
qddots(:,1) = 0;
% measurement frequency in Hz
fs = 1/dt;
% low-pass filter coefficients for torque (T) and velocity (V)
[b, a] = butter(orderT, cfT/(fs/2), 'low');
[b2, a2] = butter(orderV, cfV/(fs/2), 'low');
% apply the filter using filtfilt to achieve zero-phase filtering
for k = 1:nj
torque(k,:) = filtfilt(b, a, torque(k,:));
vel(k,:) = filtfilt(b2, a2, vel(k,:));
end
% downsample the data by the factor ds.
for k = 1:nj
torq(k,:) = downsample(torque(k,1:i),ds);
q(k,:) = downsample(pos(k,1:i),ds);
qdot(k,:) = downsample(vel(k,1:i),ds);
qddot(k,:) = downsample(qddots(k,1:i),ds);
t = downsample(time(1:i),ds);
end
else
% case if cutTimeBefore > 0
% remove time after "cutTimeAfter" seconds
time(time>clipAfter) = [];
% number of points left after first removal
i = length(time);
% remove time before "cutTimeBefore" seconds
time(time<clipBefore) = [];
% number of points left after second removal
n = length(time);
% index of the first element after removing both time instances
j = i - n;
% downsample factor. Approximates the number of points someone wants to keep.
ds = ceil(n/pointNumber);
% time between 2 measurements. 1/dt is the frequency.
dt = time(end)-time(end-1);
% approximate acceleration and filter it.
qddots = centralDifference(vel,dt,cfA,orderA);
% set the initial acceleration to 0.
qddots(:,1) = 0;
% measurement frequency in Hz
fs = 1/dt;
% low-pass filter coefficients for torque (T) and velocity (V)
[b, a] = butter(orderT, cfT/(fs/2), 'low');
[b2, a2] = butter(orderV, cfV/(fs/2), 'low');
% apply the filter using filtfilt to achieve zero-phase filtering
for k = 1:nj
torque(k,:) = filtfilt(b, a, torque(k,:));
vel(k,:) = filtfilt(b2, a2, vel(k,:));
end
% downsample the data by the factor ds
for k = 1:nj
torq(k,:) = downsample(torque(k,j:i-1),ds);
q(k,:) = downsample(pos(k,j:i-1),ds);
qdot(k,:) = downsample(vel(k,j:i-1),ds);
qddot(k,:) = downsample(qddots(k,j:i-1),ds);
t = downsample(time(1:n),ds);
end
end
% number of downsampled datapoints
i = length(t);
end