function Cw=components(A, rank, plotoption)
% Redundant(stationary) Wavelet Transform according to
%  (Holschenider & Tchamitchian, 1990; Shensa, 1992)
% recommendation size >= 512, m <= 5

h0 = 1./16.;
h1 = 1./4.;
h2 = 3./8.;
h3 = 1./4.;
h4 = 1./16.;

M = 5;  %number of components
if plotoption=='p'
figure;
subplot(2,1,1);
plot(A,'r');
title('Selected Model for Cough');
ylabel('normalized sales');
xlabel('dates');
end;


extn = 0;
  j=1;

  for i = 1:1:M-1      
      extn = extn + j*2;
      j = j*2;
  end
  
  A = arpred(A,extn,rank);
  [row col] = size(A);  
  sz = row - extn;
  B=A;
  C=[];
  temp_old = A;
  j=1;
  for i = 1:1:M-1  

    extn = extn-j*2;
    temp(1:row) =0;
    for k = 1:1:sz+extn
        if k-2*j <=0 
            ind1 = 1;
        else
            ind1=k-2*j;
        end
        if k-j <=0 
            ind2 = 1;
        else
            ind2=k-j;
        end
        newf = h0 * temp_old(ind1) + h1 * temp_old(ind2) + h2 * temp_old(k) + h3 * temp_old(k+j) + h4 * temp_old(k+2*j);
        temp(k) = newf;
    end
    C=[C temp_old(1:sz)-temp(1:sz).'];
    temp_old = temp.';
    j = j*2;
 %  if plotoption == 'p'
 %    subplot(M+1,1,i+1);
 %    plot(C(:,i));
 %    title(['Component ',int2str(i)]);
 %    ylabel('amplitude');
   end;
  end
  C = [C temp(1:sz).'];
 
  if plotoption == 'p'
      subplot(2,1,2);
      plotComponents(C);    
%    subplot(3,1,3);
%    plot(C(:,M));
%    title('Residual');
%    xlabel('day number');
%    ylabel('amplitude');
  end;
if (A(1:sz) ~= sum(C,2))
      A(1:sz) - sum(C,2)
      'Calculated Incorrectly!'
      figure;
      plot(A(1:sz));
      hold on
      plot(sum(C,2),'r');
      hold off
  end
  Cw = C;
  hold off;

  