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, coefficients 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;

  annotation('textbox',[0.45, 0.78, 0.1 0.1],'String',' sin(t)',...
      'FitBoxToText','on')
subplot(4,1,1)
plot(t,0.5*sin(pi*t).*(1+sign((1-t).*t)),'-k'), ...
    axis([0 1  -0.1 1.1])
 annotation('textbox',[0.45, 0.56, 0.1 0.1],'String',' f_j',...
      'FitBoxToText','on')
subplot(4,1,2)
stairs(t,y,'-ro'), axis([0 1 -0.1 1.1])
subplot(4,1,3)
stairs(t(1:end-1),f,'-p'), axis([0 1 -0.1 1.1])
subplot(4,1,4)
stairs(t(1:end-1),w,'-bo'), axis([0 1 -0.1 .25])
%t=linspace(0,1,500);

function y=sinc(t)
y=ones(size(t));
nn=find(t~=0);
y(nn)=sin(pi*t(nn))./(pi*t(nn));
