Main Content

filter

Apply lag operator polynomial to filter time series

Description

The filter function filters an input time series through a lag operator polynomial, which is specified by a LagOp model object (see Lag Operator Polynomial Filter). To filter data through a rational transfer function, see filter.

Y = filter(A,X) returns the filtered time series Y resulting from applying the LagOp lag operator polynomial A to the time series X.

example

Y = filter(A,X,Initial=X0) applies the presample time series data X0 to initialize the filter.

example

[Y,times] = filter(___) also returns time indices associated with the input data.

example

Examples

collapse all

Filter the random series xt through the lag operator polynomial A(L) to produce the series yt=A(L)xt, where

A(L)=1-0.6L+0.08L2+0.2L4.

Create A(L) by using the LagOp function. Specify the lag terms by setting the Lags name-value argument.

A = LagOp({1 -0.6 0.08 0.2},Lags=[0 1 2 4]);

A is a LagOp lag operator polynomial object.

Generate 10 values of random standard Gaussian noise representing an arbitrary time series, and then filter it through A(L). Return observation times relative to the input series.

rng(1,"twister")    % For reproducibility
x = randn(10,A.Dimension);
[y,t] = filter(A,x);
[t x(t) y]
ans = 6×3

    4.0000   -1.1096   -0.3703
    5.0000   -0.8456    0.0821
    6.0000   -0.5727   -0.4344
    7.0000   -0.5587    0.2459
    8.0000    0.1784   -0.5177
    9.0000   -0.1969    0.6043

filter requires at least four observations to initialize the model. By default, filter assumes the input series is ordered by increasing sampling time and initilaizes the model by using the first four observations (times 0 through 3).

The output y(1) = -0.3703, which is y4=A(L)x4=x(5) - 0.6*x(4) + 0.08*x(3) + 0.2*x(1). Subsequent values of y follow a similar pattern.

Cross check the value of y(1).

x(5) - 0.6*x(4) + 0.08*x(3) + 0.2*x(1)
ans = 
-0.3703

Filter the random series xt through the lag operator polynomial A(L) to produce the series yt=A(L)xt, where

A(L)=1-0.6L+0.08L2+0.2L4. Provide a presample to initialize polynomial.

Create A(L) by using the LagOp function. Generate 10 values of random standard Gaussian noise representing an arbitrary time series.

A = LagOp({1 -0.6 0.08 0.2},Lags=[0 1 2 4]);

rng(1,"twister")    % For reproducibility
x = randn(10,A.Dimension);

Remove all required presample observations from the beginning of xt, and then pass the reduced-length series and presample to filter. Filter the entire time series using the default presample and compare the results.

p = A.Degree;        % Required number of presample observations
x0 = x(1:p);         % Presample
x1 = x((p + 1):end);   % Input data relative to outputs

[y1,t1] = filter(A,x); 
[y2,t2] = filter(A,x1,Initial=x0); 
[t1 t2 y1 y2 x1]
ans = 6×5

    4.0000         0   -0.3703   -0.3703   -0.8456
    5.0000    1.0000    0.0821    0.0821   -0.5727
    6.0000    2.0000   -0.4344   -0.4344   -0.5587
    7.0000    3.0000    0.2459    0.2459    0.1784
    8.0000    4.0000   -0.5177   -0.5177   -0.1969
    9.0000    5.0000    0.6043    0.6043    0.5864

The in-sample input and output series are the same, but the sample times are different. The sampling times of the in-sample series start at 0 when you specify the required number of presample observations.

Filter the series again, but perform the following actions:

  • Provide the first two observations of xt as a presample, which is insufficient to initialize the polynomial.

  • Provide the first five observations of xt as a presample, which is more observations than is required to initialize the polynomial.

% Insufficient presample
x0_3 = x(1:(p - 1));
x1_3 = x(p:end);
[y3,t3] = filter(A,x1_3,Initial=x0_3);

% More than required presample
x0_5 = x(1:(p + 1));
x1_5 = x((p + 2):end);
[y5,t5] = filter(A,x1_5,Initial=x0_5);

[t3 y3]
ans = 6×2

    1.0000   -0.3703
    2.0000    0.0821
    3.0000   -0.4344
    4.0000    0.2459
    5.0000   -0.5177
    6.0000    0.6043

[t5 y5]
ans = 5×2

         0    0.0821
    1.0000   -0.4344
    2.0000    0.2459
    3.0000   -0.5177
    4.0000    0.6043

For the results from specifying the insufficient presample, filter removes x1_3(1) from the in-sample data because it is required to initialize the polynomial for filtering; x1_3(1) is the final observation in the presample. Therefore, the filtered observations are the same as y1 and y2. However, filter returns the times relative to the specified in-sample data x1_3 without those times the function removes to apply to the presample. Because x1_3(1) has the baseline time of 0 and filter removed it, y3(1) has time t3(1) = 1.

For the results from specifying the presample with more than enough observations, filter applies only the latest required observations as a presample, which is x0_5(2) through x0_5(5); filter does not use the value of x0_5(1) in any calculation. The in-sample filtered observations are the same as the final 5 filtered observations from the previous call of filter. The output times start at the baseline 0 because x0_5 contains at least the required number of presample observations.

Filter the random series Xt through the multivariate lag operator polynomial B(L) to produce the series Yt=B(L)Xt, where

B(L)=[0.50001000-0.5]+[10.250.1-0.51-0.50.15-0.21]L4.

Create B(L) by using the LagOp function. Specify the lag terms by setting the Lags name-value argument.

Phi0 = [0.5 0   0; ... 
        0   1   0; ...
        0   0   -0.5];

Phi4 = [1       0.25    0.1; ...
        -0.5    1       -0.5; ...
        0.15    -0.2    1];

Phi = {Phi0 Phi4};
lags = [0 4];

B = LagOp(Phi,Lags=lags)
B = 
    3-D Lag Operator Polynomial:
    -----------------------------
        Coefficients: [Lag-Indexed Cell Array with 2 Non-Zero Coefficients]
                Lags: [0 4]
              Degree: 4
           Dimension: 3

Generate 10 values of random 3-D standard Gaussian noise representing an arbitrary 3-D time series, and then filter it through B(L). Return observation times relative to the input series.

rng(1,"twister")    % For reproducibility
X = randn(10,B.Dimension);
[Y,t] = filter(B,X);
InSampleX = X(t,:); 
table(t,InSampleX,Y)
ans=6×3 table
    t               InSampleX                               Y                
    _    ________________________________    ________________________________

    4     -1.1096     0.87587      1.7737     -1.3123    -0.63259     0.73043
    5    -0.84555    -0.24279     -1.8651      1.1553    0.074722      1.1463
    6    -0.57266     0.16681     -1.0511      -1.237     -3.9862      2.1781
    7    -0.55868     -1.9654    -0.41738    -0.62409    -0.72622     0.73096
    8     0.17838     -1.2701      1.4022     -1.1912      2.2877     -1.2595
    9    -0.19686      1.1752     -1.3677    -0.34285      3.0079     -1.0241

Input Arguments

collapse all

Lag operator polynomial, specified as a LagOp object.

Time series data to filter through lag operator polynomial A, specified as a value in this table. numDims is the number of dimensions (variables) of the polynomial, A.Dimension.

ValueDescription
1-by-numObs numeric row vectornumObs observations from one variable to filter through the input 1-D lag operator polynomial A.
numObs-by-1 numeric column vectornumObs observations from one variable to filter through the input 1-D lag operator polynomial A
numObs-by-numDims numeric matrixnumObs observations from numDims variables to filter through the input numDims-D lag operator polynomial A

Regardless of input type, the last observation is the latest.

X is one sample path of time series data. To filter multiple sample paths, use a for loop.

Data Types: double

Presample time series data to initialize the lag operator polynomial, specified as a value in this table. The lag operator polynomial A requires P = A.Degree presample observations to be initialized.

ValueSupportDescription
numeric scalarAll lag operator polynomialsAll required presample observations for all time series have value X0.
numPresampleObs-length numeric vector1-D polynomials only

When numPresampleObsP, filter uses the latest specified observations to initialize the polynomial.

Otherwise, filter prepends X with X0, for example, fullData = [X0; X]. Then, filter uses the first P observations in the resulting full data as the presample (fullData(1:P)), and it filters the remaining observations (fullData((P+1):end)).

You can supply a vector of any orientation.

numPresampleObs-by-numDims numeric matrixnumDims-D polynomials only

When numPresampleObsP, filter uses the latest specified observations (rows) to initialize the polynomial.

Otherwise, filter prepends X with X0, for example, FullData = [X0; X]. Then, filter uses the first P observations in the resulting full data as the presample (FullData(1:P,:)), and it filters the remaining observations (FullData((P+1):end,:)).

Each column of X0 initializes the corresponding variable in the polynomial.

Regardless of input type, the last observation is the latest.

Data Types: double

Output Arguments

collapse all

Filtered input time series, returned as a numeric vector or matrix.

If X is a numeric vector, filter returns Y as a numeric vector. The orientation of X and Y are the same. Regardless of orientation, Y(j) is the filtered value at sample time times(j).

If X is a numeric matrix, filter returns Y as a numeric matrix. Y(j,k) is the filtered value at sample time times(j) for the variable k

The number of observations in Y depends on whether you specify at least the required number of presample observations P:

  • If you specify numPresampleObsP presample observations in the input X0, Y contains the same number of observations as X.

  • Otherwise, Y has PnumPresampleObs fewer rows than X.

Relative time indices, returned as a vector of integers with the same length as Y. Times are relative to, or as an offset from, observations times 0, 1, 2,...,numObs – 1 for the input series X.

For a polynomial of degree P, Y(0) is a linear combination of X0 or X for times t = 0, –1, –2,...,–P (presample data). Y(t) for t > 0 is a linear combination of X(t) for times t = t, t – 1, t – 2,...,tP.

More About

collapse all

Version History

Introduced in R2010a