Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
38 changes: 27 additions & 11 deletions gaitanalysis/octave/2d_inverse_dynamics/myfiltfilt.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (yd(i)-yd(i-1))/h;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ydd(i). I'll push the fix.

end

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

This file was deleted.