clear
clf

N=2^4;
h=[0:N-1];

Y=cos(50.342*h.*h+50);


subplot(4,2,1);
hold on
for(k=[1:N-1])
  plot([h(k+1),h(k+1)],[Y(k),Y(k+1)],"lineWidth",2);
  plot([h(k),h(k+1)],[Y(k),Y(k)],"lineWidth",2);
end
plot([N-1,N],[Y(N),Y(N)],"lineWidth",2);
axis([0,N,-1.1,1.1]);

M = 0.5*(Y(1:2:end)+Y(2:2:end));
D = 0.5*(Y(1:2:end)-Y(2:2:end));

subplot(4,2,3);
hold on
for(k=1:length(M)-1)
  kk=k-1;
  plot([h(2*(kk+1)+1),h(2*(kk+1)+1)],[M(k),M(k+1)],"lineWidth",2);
  plot([h(2*kk+1),h(2*(kk+1)+1)],[M(k),M(k)],"lineWidth",2);
end
plot([N-2,N],[M(end),M(end)],"lineWidth",2);
axis([0,N,-1.1,1.1]);

subplot(4,2,4);
hold on
for(k=1:length(M))
    kk=k-1;
    plot([h(2*kk+1),h(2*(kk)+1)],[0,D(k)],"lineWidth",2,"r");
    plot(h(2*kk+1),D(k),"markersize",2,"linewidth",2,"+r");
end
axis([-0.5,N,-1.1,1.1]);


M2 = 0.5*(M(1:2:end)+M(2:2:end));
D2 = 0.5*(M(1:2:end)-M(2:2:end));


subplot(4,2,5);
hold on
for(k=1:length(M2)-1)
  kk=k-1;
  plot([h(4*(kk+1)+1),h(4*(kk+1)+1)],[M2(k),M2(k+1)],"lineWidth",2);
  plot([h(4*kk+1),h(4*(kk+1)+1)],[M2(k),M2(k)],"lineWidth",2);
end
plot([N-4,N],[M2(end),M2(end)],"lineWidth",2);
axis([0,N,-1.1,1.1]);

subplot(4,2,6);
hold on
for(k=1:length(M2))
    kk=k-1;
    plot([h(4*kk+1),h(4*(kk)+1)],[0,D2(k)],"lineWidth",2,"r");
    plot(h(4*kk+1),D2(k),"markersize",2,"linewidth",2,"+r");
end
axis([-0.5,N,-1.1,1.1]);


M3 = 0.5*(M2(1:2:end)+M2(2:2:end));
D3 = 0.5*(M2(1:2:end)-M2(2:2:end));


subplot(4,2,7);
hold on
for(k=1:length(M3)-1)
  kk=k-1;
  plot([h(8*(kk+1)+1),h(8*(kk+1)+1)],[M3(k),M3(k+1)],"lineWidth",2);
  plot([h(8*kk+1),h(8*(kk+1)+1)],[M3(k),M3(k)],"lineWidth",2);
end
plot([N-8,N],[M3(end),M3(end)],"lineWidth",2);
axis([0,N,-1.1,1.1]);

subplot(4,2,8);
hold on
for(k=1:length(M3))
    kk=k-1;
    plot([h(8*kk+1),h(8*(kk)+1)],[0,D3(k)],"lineWidth",2,"r");
    plot(h(8*kk+1),D3(k),"markersize",2,"linewidth",2,"+r");
end
axis([-0.5,N,-1.1,1.1]);

%print -dfig "plot_multires.fig"
