function [x, A, B] = butterfiltfilt(x, fc, fs, order, type, direction)
if nargin<6 || isempty(direction)
if nargin<5 || isempty(type)
if nargin<4 || isempty(order)
if nargin<3 || isempty(fc)
if all(fc>=0) && all(fc<=1)
error('Sampling frequency not given');
if nargin<2; error('Insufficient number of inputs'); end
if ~isnumeric(x); error('Data must be numeric'); end;
if ~isnumeric(fc); error('Filter bounds must be numeric'); end
if ~isnumeric(fs) || ~isscalar(fs); error('Sampling frequency must be a scalar'); end
if ~isnumeric(order) || ~isscalar(order); error('Order must be a scalar'); end
if ~ischar(type); error('Type must be a string'); end
if ~ischar(direction); error('Direction must be a string'); end
if strcmp(direction,'both')
error('Filter order must be even if filter is bidirectional');
if numel(fc)~=1; error('Too many bounds given'); end;
if numel(fc)~=1; error('Too many bounds given'); end;
error('Type %s not found',type);
error('Filter requires two cutoff frequencies');
elseif strcmp(type,'stop')
[B,A] = butter(order, fc, 'stop');
elseif (fc(1)==0) && (isinf(fc(2)))
[B,A] = butter(order, fc(2), 'low');
elseif isinf(fc(2)) || fc(2)==1
[B,A] = butter(order, fc(1), 'high');
[B,A] = butter(order, fc);
x = FilterM(B, A, x, [], 'reverse');
x(end:-1:1,:) = filter(B, A, x(end:-1:1,:));
error('Unrecognised filter direction: %s',direction);