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
.
Examples
Filter the random series through the lag operator polynomial to produce the series , where
Create 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 . 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 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 through the lag operator polynomial to produce the series , where
Provide a presample to initialize polynomial.
Create 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 , 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 as a presample, which is insufficient to initialize the polynomial.
Provide the first five observations of 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 through the multivariate lag operator polynomial to produce the series , where
Create 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 . 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
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
.
Value | Description |
---|---|
1-by-numObs numeric row vector | numObs observations from one variable to filter through
the input 1-D lag operator polynomial A . |
numObs -by-1 numeric column vector | numObs observations from one variable to filter through
the input 1-D lag operator polynomial A |
numObs -by-numDims numeric
matrix | numObs 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.
Value | Support | Description |
---|---|---|
numeric scalar | All lag operator polynomials | All required presample observations for all time series have value
X0 . |
numPresampleObs -length numeric vector | 1-D polynomials only | When Otherwise,
You can supply a vector of any orientation. |
numPresampleObs -by-numDims numeric
matrix | numDims -D polynomials only | When Otherwise,
Each
column of |
Regardless of input type, the last observation is the latest.
Data Types: double
Output Arguments
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(
is the filtered value at
sample time j
)times(
.j
)
If X
is a numeric matrix, filter
returns Y
as a numeric matrix.
Y(
is
the filtered value at sample time j
,k
)times(
for the variable j
)k
The number of observations in Y
depends on whether you specify at
least the required number of presample observations P
:
If you specify
numPresampleObs
≥P
presample observations in the inputX0
,Y
contains the same number of observations asX
.Otherwise,
Y
hasP
–numPresampleObs
fewer rows thanX
.
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(
for
t
)
> 0 is a linear combination of
t
X(
for times
t
)
=
t
,
t
– 1,
t
–
2,...,t
– t
P
.
More About
A lag operator polynomial filter is a linear dynamic function of a time series that produces a time series.
Consider the lag operator polynomial A(L) and time series xt, where L is the backshift operator such that Lqxt = xt–q. The lag operator polynomial filter is simply the polynomial applied to the time series A(L)xt, which produces the processed time series yt.
For example, let A(L) = 1 + 0.9L – 0.25L2. The filter applied to xt is
If xt = {1,2,3,4,5}, yt is the series represented in this table.
t | xt | Filter | yt |
---|---|---|---|
–2 | 1 | Presample | N/A |
–1 | 2 | Presample | N/A |
0 | 3 | 3 + 0.9(2) – 0.25(1) | 4.55 |
1 | 4 | 4 + 0.9(3) – 0.25(2) | 6.2 |
2 | 5 | 5 + 0.9(4) – 0.25(3) | 7.85 |
yt = {4.55,6.2,7.85}.
Version History
Introduced in R2010a
See Also
Objects
Functions
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)