ezfilt

UPDATE: ezfilt now uses multiple regression based on a selection of cosines. The old version, the running average, runs into the usual filter problem: what do you do about the beginning and end of the signal? Now I detrend using a sequence of cosines with periods of multiples of twice the available signal duration.

function [slow, fast] = ezfilt(signal, Fs, cutFreq)

% function [slow, fast] = ezfilt(signal, Fs, cutFreq)

fcrit = cutFreq;

% Create X
X = [];
t = 1:length(signal);
t = (t - 0) ./ Fs;
T = t(end);
f0 = 0;
while 1 == 1,
    cos0 = cos(2 * pi * f0 * t);
    X = [X cos0(:)];
    f0 = f0 + 0.5 / T;
    if f0 > fcrit,
        break;
    end;
end;

% Fit
b = inv(X' * X) * X' * signal(:);
slow = X * b;
fast = signal(:) - slow;