function [fX,AA]=bkfilter_asy(y,pl,pu)
% GB 2013
% PURPOSE: computes asymmetric Baxter-King filter
% if useful, cite: 
% Buss, Ginters, 2011. "Asymmetric Baxter-King filter," 
% MPRA Paper 28176, 
% University Library of Munich, Germany. 
% ------------------------------------------------------------
% INPUT:
% * y = a real (nx1) vector
% * pl = minimum period of oscillation of desired component
% * pu = maximum period of oscillation of desired component
%   (2<=pl<pu<infinity).
% ------------------------------------------------------------
% OUPTUT:
% * fX = vector (nX1) containing filtered data
% * AA = filtering matrix
% ------------------------------------------------------------
% ------------------------------------------------------------

b = 2*pi/pl;
a = 2*pi/pu;

[T,nvars] = size(y);

if T<5 then
   warning('# of observations < 5');
end


if pu<=pl then
  error('pu must be larger than pl');
end

if pl<2 then
  warning('pl less than 2 , reset to 2');
  pl = 2;
end



%   compute """"ideal"""" Bs
j = 1:2*T;
B = [(b-a)/pi,(sin(j*b)-sin(j*a)) ./ (j*pi)]';

AA = zeros(T,T);

for i = 1:T
  AA(i,i:T) = B(1:T-i+1)';
end

AA(1,1) = AA(T,T);
%  Use symmetry to construct bottom 'half' of AA
v2 = triu(AA,1);
v2 = v2(:,end:-1:1);
AA = AA+v2(end:-1:1,:);
AA=AA-kron(sum(AA,2)./T,ones(1,T));

% compute filtered time series using selected filter matrix AA

fX = AA*y;
