-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcentralDifference.m
More file actions
59 lines (47 loc) · 1.68 KB
/
centralDifference.m
File metadata and controls
59 lines (47 loc) · 1.68 KB
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
%% 4th Order Central difference
% Author: Daniel Haugk, 2024, University of Michigan
function dydt = centralDifference(y,dt,cfA,orderA)
% coefficients for 4th order central difference
CentCoeff = [1/12 -2/3 0 2/3 -1/12]';
% coefficients for 4th order forward difference
ForwCoeff = [-25/12 4 -3 4/3 -1/4]';
% coefficients for 4th order backward difference
BackwCoeff = flip(-ForwCoeff);
% number of datapoints
n = length(y(1,:));
% number of joints
nj = length(y(:,1));
% measurement frequency in Hz
fs = 1/dt;
% Design the filter
[b, a] = butter(orderA, cfA/(fs/2), 'low');
% approximation for each row from 1 to nq, of y seperately
for j = 1:nj
% Apply the filter using filtfilt to achieve zero-phase filtering
yfilt = filtfilt(b, a, y(j,:));
for i=1:n
switch i
case 1
% use FORWARD difference here for the first point
y_ = yfilt(i:i+4);
dydt(j,i) = y_*ForwCoeff/dt;
case 2
% use FORWARD difference here for the second point
y_ = yfilt(i:i+4);
dydt(j,i) = y_*ForwCoeff/dt;
case n-1
% use BACKWARD difference here for the second last point
y_ = yfilt(i-4:i);
dydt(j,i) = y_*BackwCoeff/dt;
case n
% use BACKWARD difference here for the last point
y_ = yfilt(i-4:i);
dydt(j,i) = y_*BackwCoeff/dt;
otherwise
% use CENTRAL difference for every other point
y_ = yfilt(i-2:i+2);
dydt(j,i) = y_*CentCoeff/dt;
end
end
end
end