function y = convfilt(FIR,x)
%                    FAST 'FIR' CONVOLUTION FILTERING
% -----------------------------------------------------------------------------
% CALLING: y = convfilt(FIR,x)
% E.g., the typical usage is with the fast stepwise polynomial regression
%       (see 'polyfilt' for more details):
%          y = convfilt(polyfilt(d,m),x)
% INPUT:
% x  ... is an n x 1 vector of data to be filtered
% FIR... m x m matrix (m<=n, m=2*q+1 odd) defining the smoothing operator
%        y = FIR*x
%        FIR(k,:) ... FIR for computing
%                     y(k)       for 1<=k<=q    from x(1:m)     [left boundary] 
%                     y(q+1:n-q) for k=q+1      from x          [middle part]
%                     y(n-m+k)   for q+2<=k<=m  from x(n-q+1:m) [right boundary] 
%        m<n => FIR is moving across the data vector
% OUTPUT:
% y ...  is an n x 1 vector of smoothed (filtered) data

% (C) 1996, V. Vesely, Masaryk University of Brno (Czech Republic)
%

error(nargchk(2,2,nargin));
n = length(x);
x= reshape(x,n,1);
[m,mm] = size(FIR);
q = fix(m/2);
if m~=mm | m~=2*q+1, error('FIR must be a square matrix of odd size'); end
if n<m, error('Filter length ''m'' exceeds the data size'); end
y = conv(x,FIR(q+1,m:-1:1));
y = [FIR(1:q,:)*x(1:m); y(m:n); FIR(q+2:m,:)*x(n-m+1:n)];
