function [a,b]=haar_dec(level1_coefs,t0,t1)
% HAAR_DEC decomposes data using the Haar scaling function and
% wavelet. The input consists of the level j+1 scaling
% coefficients, and the output are the level j approximation and
% detail functions. Plots are given if no output is specified.
%
% Francis J. Narcowich, Mar.27, 2003.
%

close
s=level1_coefs;
n=length(s);
%Pad the end with 0 if there are an odd no. of coefs.
if 2*floor(n/2)<n 
  s(n+1)=0;
end
if nargin==1
    t=linspace(0,1,length(s)+1);
else
    t=linspace(t0,t1,length(s)+1);
end

% Create an array that will contain the f0.
a=zeros(size(s));
a(1:2:end)=0.5*(s(1:2:end)+s(2:2:end));
a(2:2:end)=a(1:2:end);

% Create an array that will contain the w0.
b=zeros(size(s));
b(1:2:end)=0.5*(s(1:2:end)-s(2:2:end));
b(2:2:end)=-b(1:2:end);
if nargout==0,
  subplot(3,1,1), stairs(t,[s,s(end)],'y-'), grid on, title('Original Signal')
  subplot(3,1,2), stairs(t,[a,a(end)],'b'), grid on
  subplot(3,1,3), stairs(t,[b,b(end)],'r'), grid on
  whitebg(gcf)
end


