function [f,g,h] = postfilter(f0,g0,h0,S)
% Adds an output filter to a linear-analytic system
%
% [F,G,H]=POSTFILTER(F0,G0,H0,S) adds the state-space filter (S.A,S.b,S.c)
% to the ouput
% literal symbolic linear-analytic state-space model 
% x'= f0(x) + g0(x)u
% y=h0*x

%   CBVK Toolbox
%   JHMG, PMSB 2016


% Filter in symbolic form
A = sym(S.A,'d');
b = sym(S.b,'d');
c = sym(S.c,'d');

% Filter, system and overall orders
N = size(A,1);
m = size(f0,1);
M = N+m;


% Symbolic state vector
x = sym(zeros(M,1));
for k=1:min(M,9)
    x(k) = sym(['x0' num2str(k)]);
end;    

% M<100
if M>9
  for k=10:M
    x(k)= sym(['x' num2str(k)]);
  end
end

f = sym(zeros(M,1));
g = sym(zeros(M,1));

% Linear analytic part
f(1:m) = f0;
g(1:m) = g0;

% Filter part
f(m+1:end) = A*x(m+1:end) + b*h0*x(1:m);
h=zeros(1,M);
h(m+1:end)=c;

