%% Filter Data in Sections
% Use initial and final conditions for filter delays to filter data in sections, especially if memory limitations are a consideration.
% Generate a large random data sequence and split it into two segments, x1 and x2.

x = randn(10000,1);

x1 = x(1:5000);
x2 = x(5001:end);

% The whole sequence, x, is the vertical concatenation of x1 and x2.
%
% Define the numerator and denominator coefficients for the rational transfer function,
%  H(z) = \frac{b(1)+b(2)z^{-1}}{a(1)+a(2)z^{-1}} =
% \frac{2+3z^{-1}}{1+0.2z^{-1}}.

b = [2,3];
a = [1,0.2];

%
% Filter the subsequences x1 and x2 one at a time. Output the final conditions from filtering x1 to store the internal status of the filter at the end of the first segment.

[y1,zf] = filter(b,a,x1);

%
% Use the final conditions from filtering x1 as initial conditions to filter the second segment, x2.

y2 = filter(b,a,x2,zf);

% y1 is the filtered data from x1, and y2 is the filtered data from x2. The entire filtered sequence is the vertical concatenation of y1 and y2.
%
% Filter the entire sequence simultaneously for comparison.

y = filter(b,a,x);

isequal(y,[y1;y2])

% Copyright 2015 The MathWorks, Inc.