Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed ups to the Octave inverse dynamics. #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions gaitanalysis/octave/2d_inverse_dynamics/myfiltfilt.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function [y, yd, ydd] = myfiltfilt(t, x, f0);
function [y, yd, ydd] = myfiltfilt(t, x, f0)

% performs low pass filtering and differentiation
% method is the same as used in HBM but bidirectional to eliminate lag
Expand All @@ -9,28 +9,44 @@
% f0 (scalar) Corner frequency
%
% Outputs
% y (Nsamples x 1) Filtered signal
% yd (Nsamples x 1) First derivative
% ydd (Nsamples x 1) Second derivative
% y (Nsamples x Nchannels) Filtered signal
% yd (Nsamples x Nchannels) First derivative
% ydd (Nsamples x Nchannels) Second derivative

C = 0.802; % correction factor for dual pass 2nd order filter (Winter book)
y = rtfilter_batch(-flipud(t), flipud(x), f0/C); % filter backwards in time
[y, yd, ydd] = rtfilter_batch(t, flipud(y), f0/C); % filter forward in time
C = 0.802; % correction factor for dual pass 2nd order filter (Winter book)
y = filter_batch(-flipud(t), flipud(x), f0/C); % filter backwards in time
[y, yd, ydd] = filter_batch(t, flipud(y), f0/C); % filter forward in time

end
%===================================================================================
function [y, yd, ydd] = rtfilter_batch(t,x,f0)
function [y, yd, ydd] = filter_batch(t,x,f0)
% filters a time series and also returns derivatives
% uses real-time second order Butterworth filter (rtfilter.m)

% some constants we will need
a = (2*pi*f0)^2;
b = sqrt(2)*(2*pi*f0);

% allocate memory for the results
n = size(x,1);
y = zeros(size(x));
yd = zeros(size(x));
ydd = zeros(size(x));

for i = 1:n
[y(i,:), yd(i,:), ydd(i,:)] = rtfilter(t(i),x(i,:),f0);
% Integrate the filter state equation using the midpoint Euler method with step h
% initial conditions are y=0 and yd=0
for i = 2:n
h = t(i)-t(i-1); % time step
denom = 4 + 2*h*b + h^2*a;
A = (4 + 2*h*b - h^2*a)/denom;
B = 4*h/denom;
C = -4*h*a/denom;
D = (4 - 2*h*b - h^2*a)/denom;
E = 2*h^2*a/denom;
F = 4*h*a/denom;
y(i) = A*y(i-1) + B*yd(i-1) + E*(x(i)+x(i-1))/2;
yd(i) = C*y(i-1) + D*yd(i-1) + F*(x(i)+x(i-1))/2;
ydd(i) = (yd(i)-yd(i-1))/h;
end

end
%===================================================================================
%===================================================================================
52 changes: 0 additions & 52 deletions gaitanalysis/octave/2d_inverse_dynamics/rtfilter.m

This file was deleted.