%-----------------------------------------------------------------
%     *** 2.161 Signal Processing - Continuous and Discrete  ***
%                      Tutorial MATLAB Function
%
% firdf -   Demonstration FIR transposed direct form implementation
%
% Source:   Class handout:  Direct Form Digital Filter Structures
%
% Usage :   1) Initialization:
%             y = firtdf('initial', b)
%               where b are the numerator polynomial coefficients. 
%                Example: b= [1 2 3 4 5 4 3 2 1];
%                         y = iirdf1('initial',b);
%                Note: firtdf returns y = 0 for initialization
%           2) Filtering:
%              y_out = firtdf(f);
%                where f is a single input value, and y_out is the computed
%                output value.
%                Example: To compute the step response:
%                     for j=1:100
%                        y(j) = firtdf(1);
%                     end
%
% Version:  1.0
% Author:   D. Rowell   11/20/07
% -------------------------------------------------------------------------
%
function y_n = firtdf(f_n,B)
persistent register Bx  N
%
% The following is initialization, and is executed once
%
if (ischar(f_n) && strcmp(f_n,'initial'))
    N = length(B);
    Bx = B;
    register = zeros(1,N-1);
    y_n = 0;
    else
% Filtering:   (Note that a Transposed Direct Form II filter needs only a single
% register.)  Also note that this is not strictly a shift register.
    y_n = register(1) + Bx(1)*f_n;
    % Update for the next iteration
    for J = 1:N-2
        register(J) = register(J+1) + Bx(J+1)*f_n;
    end
    register(N-1) =  Bx(N)*f_n;
end
