function D = DictSpline( m, L, sp, b1, b2, type )
% DictSpline generates dictionary of cardinal B-spline functions of order m
%           
% Usage:    D = DictSpline( m, L, sp, b1, b2, type );
%
% Inputs:
%   m       order of spline functions (m>=1), m=1 is a piecewise constant function
%   L       number of discrete points
%   sp      vector [sp(1),sp(2)] specifying the interval
%   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 cardinal B-spline functions of order 
%
% Remark:   This procedure uses the translation property of inner cardinal B-splines.
%           It calls the routine SplineLevel to do the job.

% See   http://www.ncrg.aston.ac.uk/Projects/HNLApprox/
%       http://www.nonlinear-approx.info/
  
tic;funname='DictSpline';
%simple tests
if nargin~=6, error('Need 6 input arguments.');end
if (m<1) | (m>16), error('Order of splines, m, should be from 1 to 16.');end

type=upper(type);
%reading of interval
lb=sp(1); ub=sp(2); 
%are the sequences nested?
if b1/b2~=round(b1/b2), error('Ratio b1/b2 must be an integer.');end  

x=linspace(lb,ub,L);  %discretization of the interval
int=ub-lb;            %length of the interval

%can be the interval equally spaced?
if (int/b2~=round(int/b2)) | (int/b1~=round(int/b1)) 
  fprintf('With b1=%g, b2=%g, interval [%g,%g] cannot be equally spaced.\n',b1,b2,lb,ub);
  error('Try another pair b1, b2.');
end  
%can I create all inner functions as translates of one of them?
pom=b2/(x(2)-x(1));
if pom~=round(pom)  
  fprintf('Using value L=%d this method cannot be accurate.\n',L);     
  error('Please choose L as L=int/b2*k+1 for k being an integer and int=sp(2)-sp(1).');
end
 
delka=m+1;      %support of spline
N2=int/b2-1;
n=m*(b1/b2)+N2; %number of functions

fprintf('\nInterval [%g,%g], devided into %d points, step=%g\n',lb,ub,L,x(2)-x(1));
fprintf('The smallest distance between two adjacent functions is b2=%g\n',b2);

%--------Main part--------------------
%creating of dictionary
D=zeros(L,n);
D(:,1:n)=SplineLevel(m,x,b1,b2,type);
%-------------------------------------

fprintf('B-splines of order %d, type %s, indexes %d :%d \n',m,type,1,n);
tt=toc;
fprintf('\n%s generated %d functions in %f seconds\n',funname,size(D,2),tt);
fprintf('   (It means %f functions per second)\n\n',size(D,2)/tt);


%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.
