function S = ansbbfil(Input, n, C)
%S = Analysis_subband_filter(Input, n, C)
%   Returns the 32 subband samples S(i) defined in [1, pp. 67,78]
%   n is the index in Input where the 32 `new'samples are located.
%   C is the analysis window defined in [1, pp.68--69].
%
%   See also Analysis_window
   
%-------------------------------------------------------------------------------
common;

nmax = length(Input);

% Check input parameters
if (n + 31 > nmax | n < 1)
   error('Unexpected analysis index.');
end

% Build an input vector X of 512 elements. The most recent sample
% is at position 512 while the oldest element is at position 1.
% Padd with zeroes if the input signal does not exist.
%  ...........................................................
%         |         480 samples        |  32 samples   |
%         n-480                         n              n+31
X = Input(max(1, n - 480):n + 31); % / 32768
X = X(:);
X = [zeros(512 - length(X), 1); X];

% Window vector X by vector C. This produces the Z buffer.
Z = X .* C;

% Partial calculation: 64 Yi coefficients
Y = zeros(1, 64);
for i = 1 : 64,
   for j = 0 : 7,
      Y(i) = Y(i) + Z(i + 64 * j);
   end
end

% Calculate the analysis filter bank coefficients
for i = 0 : 31,
   for k = 0 : 63,
      M(i + 1, k + 1) = cos((2 * i + 1) * (k - 16) * pi / 64);
   end
end

% Calculate the 32 subband samples Si
S = zeros(1, 32);
for i = 1 : 32,
   for k = 1 : 64,
      S(i) = S(i) + M(i, k) * Y(k);
   end
end
