function D = SplineLevel( m, x, b1, b2, type )
% SplineLevel generates a B-spline dictionary depending on given parameters
%
% Usage:    D = SplineLevel( m, x, b1, b2, type );
%
% Inputs:
%   m       order of splines
%   x       discrete variable
%   b1      coarser partition of sp, points sp(1)+k*b1, k=0
%   b2      finer partition of sp, points sp(1)+k*b2, k=0
%   type    either ESEP or EPKB
%
% Output:
%   D       dictionary of spline functions
%
% References:
%    M. Andrle and L. Rebollo-Neira, "Cardinal B-spline dictionaries on a compact
%    interval", Applied and Computational Harmonic Analysis, Vol(18,3), 336-346 (2005).

% See   http://www.ncrg.aston.ac.uk/Projects/HNLApprox/
%       http://www.nonlinear-approx.info/

% initilization 
L=length(x);     %number of points
lb=x(1);ub=x(L); %borders of interval
int=ub-lb;       %length of interval
delta=x(2)-x(1); %discretization step delta
start=lb-m*b1+b2;
N2=int/b2-1;
nn=m*(b1/b2)+N2; %number of function
t=lb:b1:lb+m*b1; %knot sequence
w=TSpline(x,t,m);%function prototype
D=zeros(L,nn);
switch type
 case 'ESEP'
  index=1;
  for ni=start:b2:start+(nn-1)*b2,
    posun=round((ni-lb)/delta);
    if posun >=0  
      D(1+posun:L,index)=w(1:L-posun);
    else
      D(1:L+posun,index)=w(1-posun:L);
    end   
    index=index+1;
  end	
 case 'EPKB'
  %boundary functions
  t=0:b1:m*b1;
  for ni=0:m*(b1/b2)-2
    t1=max(lb,t+start+ni*b2);
    t2=min(ub,t+ub-(ni+1)*b2);
    D(:,ni+1)=TSpline(x,t1,m);
    D(:,nn-ni)=TSpline(x,t2,m);
  end 
  %just for case that there are no boundary functions
  if isempty(ni) ni=-1; end;
  %inner functions
  for k=lb:b2:ub-m*b1
    posun=round((k-lb)/delta); ni=ni+1;
    D(1+posun:L,ni+1)=w(1:L-posun);
  end  
 otherwise 
  error('Unknown type of basis, use ESEP or EPKB');
end


%Copyright (C) 2006 Miroslav ANDRLE and Laura REBOLLO-NEIRA
%
%This program is free software; you can redistribute it and/or modify it under the terms 
%of the GNU General Public License as published by the Free Software Foundation; either 
%version 2 of the License, or (at your option) any later version.
%
%This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
%without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
%See the GNU General Public License for more details.
%
%You should have received a copy of the GNU General Public License along with this program;
%if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
%Boston, MA  02110-1301, USA.
