【 MATLAB 】filter 函数介绍(一维数字滤波器)
了解这个函数,先看看这个基础知识:【 MATLAB 】Rational Transfer Function(有理传递函数)
filter
1-D digital filter
Syntax
y = filter(b,a,x)
y = filter(b,a,x,zi)
y = filter(b,a,x,zi,dim)
[y,zf] = filter(___)
Description
使用由分子和分母系数 b 和 a 定义的有理传递函数对输入数据 x 进行滤波。y
= filter(b
,a
,x
)
If a(1)
is not equal to 1
, then filter
normalizes the filter coefficients by a(1)
. Therefore, a(1)
must be nonzero.
-
If
x
is a vector, thenfilter
returns the filtered data as a vector of the same size asx
. -
If
x
is a matrix, thenfilter
acts along the first dimension and returns the filtered data for each column. -
If
x
is a multidimensional array, thenfilter
acts along the first array dimension whose size does not equal 1.
针对这条语法举个例子:
Moving-Average Filter
移动平均滤波器是用于平滑噪声数据的常用方法。 此示例使用过滤器函数计算沿数据向量的平均值。
Create a 1-by-100 row vector of sinusoidal data that is corrupted by random noise.
t = linspace(-pi,pi,100); rng default %initialize random number generator x = sin(t) + 0.25*rand(size(t));
A moving-average filter slides a window of length windowSize along the data, computing averages of the data contained in each window. The following difference equation defines a moving-average filter of a vector :
For a window size of 5, compute the numerator and denominator coefficients for the rational transfer function.
对于窗口大小为5,计算有理传递函数的分子和分母系数。
windowSize = 5; b = (1/windowSize)*ones(1,windowSize); a = 1;
Find the moving average of the data and plot it against the original data.
找到数据的移动平均值并根据原始数据绘制它。
y = filter(b,a,x); plot(t,x) hold on plot(t,y) legend('Input Data','Filtered Data')
uses initial conditions y
= filter(b
,a
,x
,zi
)zi
for the filter delays. The length of zi
must equal max(length(a),length(b))-1
.
举例说明:
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,
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])
ans = logical 1
acts along dimension y
= filter(b
,a
,x
,zi
,dim
)dim
. For example, if x
is a matrix, then filter(b,a,x,zi,2)
returns the filtered data for each row.
举例:
This example filters a matrix of data with the following rational transfer function.
Create a 2-by-15 matrix of random input data.
rng default %initialize random number generator x = rand(2,15);
Define the numerator and denominator coefficients for the rational transfer function.
b = 1; a = [1 -0.2];
Apply the transfer function along the second dimension of x
and return the 1-D digital filter of each row. Plot the first row of original data against the filtered data.
y = filter(b,a,x,[],2); t = 0:length(x)-1; %index vector plot(t,x(1,:)) hold on plot(t,y(1,:)) legend('Input Data','Filtered Data') title('First Row')
Plot the second row of input data against the filtered data.
figure plot(t,x(2,:)) hold on plot(t,y(2,:)) legend('Input Data','Filtered Data') title('Second Row')
[
also returns the final conditions y
,zf
] = filter(___)zf
of the filter delays, using any of the previous syntaxes.
这个形式的例子同:
Output Arguments
y
— Filtered data
vector | matrix | multidimensional array
Filtered data, returned as a vector, matrix, or multidimensional array of the same size as the input data, x
.
If x
is of type single
, then filter
natively computes in single precision, and y
is also of type single
. Otherwise, y
is returned as type double
.
Data Types: double
| single
zf
— Final conditions for filter delays
vector | matrix | multidimensional array
Final conditions for filter delays, returned as a vector, matrix, or multidimensional array.
-
If
x
is a vector, thenzf
is a column vector of lengthmax(length(a),length(b))-1
. -
If
x
is a matrix or multidimensional array, thenzf
is an array of column vectors of lengthmax(length(a),length(b))-1
, such that the number of columns inzf
is equivalent to the number of columns inx
. For example, consider usingfilter
along the second dimension (dim = 2
) of a 3-by-4-by-5 arrayx
. The arrayzf
has size [max(length(a),length(b))-1
]-by-3-by-5.
Data Types: double
| single