%
% fit data at given wavenet level of resolution
%
%   [ f, c ] = levelfit( x, w, l, m )
%
%     f  = fitted function (2^l+1 points)
%     c  = fitted coefficients (2^(l-m+1) values)
%     x  = data to be fitted (2^l+1 points, 
%                              NaN if missing)
%     w  = wavelet type (see function wavefunc)
%     l  = resolution
%     m  = wavenet level (0..l)
%
function [ f, c ] = levelfit( x, w, l, m )
%
% generate regression matrix
a = [];
xx = [0:2^l];
for k = 0:2^m:2^l,
 a = [ a wavefunc( xx, w, l, m, k ) ];
end;
%
b = x(:);
%
% regress
if cond(a) < 1000000,
  c = a \ b;
else
  disp( [ 'WARNING - Level ' num2str(m) ...
          ' poorly conditioned' ] );
  [ row col ] = size( a );
  c = zeros( row, 1 );
end;
%
% calculate function
f = a * c;
%
