function [y,f,w]=haar_sine(j) 
% HAAR_SINE first projects onto V_j the function f(t), where f(t) =
% sin(pi*t) if 0<t<1, f(t) = 0 otherwise. It then decomposes f_j
% into the Haar approximation and wavelet parts f_{j-1} and
% g_{j-1}. Outputs are the level j and level j-1 approximation, y
% and f, coefficinets and the level j-1 wavelet coefficients, w. If
% no output is specified, a plot is returned. 
%
% Francis J. Narcowich, Mar.27, 2003.
%

t=-1:2^(-j):2;
y=zeros(size(t));
y(2^j+1:2*2^j)=sinc(2^(-j-1)).*sin(pi*t(2^j+1:2*2^j)+pi*2^(-j-1));
w_c=0.5*(y(1:2:end-1)-y(2:2:end)); %Wavelet coefficients
w=zeros(1,2*length(w_c));
w(1:2:end)=w_c; w(2:2:end)=-w_c;
f_c=0.5*(y(1:2:end-1)+y(2:2:end)); %Scaling coefficients
f=zeros(1,2*length(f_c));
f(1:2:end)=f_c; f(2:2:end)=f_c;
stairs(t,y,'r'), axis([-0.125 1.125 -0.1 1.1])
hold on
stairs(t(1:end-1),f,'g')
stairs(t(1:end-1),w,'b')
t=linspace(-1,2,500);
plot(t,0.5*sin(pi*t).*(1+sign((1-t).*t)),'-.k')
hold off
