且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MATLAB 中的高通滤波

更新时间:2022-10-15 15:54:32

可以使用多种过滤器,过滤器的实际选择将取决于您要实现的目标.既然您提到了 Butterworth、Chebyschev 和 Elliptical 滤波器,我假设您正在寻找一般的 IIR 滤波器.

***是开始阅读不同过滤器及其作用的好地方.例如,的维基文章,尤其是关于稳定性的部分.

为了进一步说明我的观点,请考虑以下带通滤波器.

fpass=[0.05 0.2];%# 通带fstop=[0.045 0.205];%# 降低到一半功率的频率Rpass=1;%# 阻带内最大允许纹波 (dB)Astop=40;%# min 40dB 衰减n=cheb2ord(fpass,fstop,Rpass,Astop);%# 计算达到这些设计要求的最小滤波器阶数[b,a]=cheby2(n,Astop,fstop);

现在,如果您使用 zplane(b,a) 查看零极点图,您会看到有几个极点 (x) 位于单位圆,这使得这种方法不稳定.

这从频率响应完全失控的事实中可以明显看出.使用 freqz(b,a) 得到以下内容

要根据您的确切设计要求获得更稳定的滤波器,您需要在 MATLAB 中使用 z-p-k 方法而不是 b-a 使用二阶滤波器.以下是与上述相同的过滤器的方法:

[z,p,k]=cheby2(n,Astop,fstop);[s,g]=zp2sos(z,p,k);%# 创建二阶截面hd=dfilt.df2sos(s,g);%#创建一个dfilt对象.

现在如果你看这个滤波器的特性,你会发现所有的极点都在单位圆内(因此稳定)并且符合设计要求

butterellip 的方法类似,具有等效的 buttordellipord.MATLAB 文档也有关于设计滤波器的很好的例子.您可以在这些示例的基础上,根据自己的需要设计过滤器.

要对您的数据使用过滤器,您可以执行 filter(b,a,data)filter(Hd,data) 取决于您最终使用的过滤器用.如果您想要零相位失真,请使用 filtfilt.但是,这不接受 dfilt 对象.因此,要使用 Hd 进行零相位滤波器,请使用 filtfilthd Mathworks 文件交换站点上提供的文件

编辑

这是对@DarenW 评论的回应.平滑和过滤是两种不同的操作,虽然它们在某些方面是相似的(移动平均是一种低通滤波器),但您不能简单地用一个替换另一个,除非您可以确定它不会是关注具体应用.

例如,在0-25kHz的线性chirp信号上实现Daren的建议,在100kHz采样,这是用高斯滤波器平滑后的频谱

当然,接近 10Hz 的漂移几乎为零.但是,该操作完全改变了原始信号中频率分量的性质.这种差异的产生是因为他们完全忽略了平滑操作的滚降(见红线),并假设它将为零.如果这是真的,那么减法就会奏效.但可惜,事实并非如此,这就是为什么存在关于设计过滤器的整个领域的原因.

Does anyone know how to use filters in MATLAB? I am not an aficionado, so I'm not concerned with roll-off characteristics etc — I have a 1 dimensional signal vector x sampled at 100 kHz, and I want to perform a high pass filtering on it (say, rejecting anything below 10Hz) to remove the baseline drift.

There are Butterworth, Elliptical, and Chebychev filters described in the help, but no simple explanation as to how to implement.

There are several filters that can be used, and the actual choice of the filter will depend on what you're trying to achieve. Since you mentioned Butterworth, Chebyschev and Elliptical filters, I'm assuming you're looking for IIR filters in general.

Wikipedia is a good place to start reading up on the different filters and what they do. For example, Butterworth is maximally flat in the passband and the response rolls off in the stop band. In Chebyschev, you have a smooth response in either the passband (type 2) or the stop band (type 1) and larger, irregular ripples in the other and lastly, in Elliptical filters, there's ripples in both the bands. The following image is taken from wikipedia.

So in all three cases, you have to trade something for something else. In Butterworth, you get no ripples, but the frequency response roll off is slower. In the above figure, it takes from 0.4 to about 0.55 to get to half power. In Chebyschev, you get steeper roll off, but you have to allow for irregular and larger ripples in one of the bands, and in Elliptical, you get near-instant cut off, but you have ripples in both bands.

The choice of filter will depend entirely on your application. Are you trying to get a clean signal with little to no losses? Then you need something that gives you a smooth response in the passband (Butterworth/Cheby2). Are you trying to kill frequencies in the stopband, and you won't mind a minor loss in the response in the passband? Then you will need something that's smooth in the stop band (Cheby1). Do you need extremely sharp cut-off corners, i.e., anything a little beyond the passband is detrimental to your analysis? If so, you should use Elliptical filters.

The thing to remember about IIR filters is that they've got poles. Unlike FIR filters where you can increase the order of the filter with the only ramification being the filter delay, increasing the order of IIR filters will make the filter unstable. By unstable, I mean you will have poles that lie outside the unit circle. To see why this is so, you can read the wiki articles on IIR filters, especially the part on stability.

To further illustrate my point, consider the following band pass filter.

fpass=[0.05 0.2];%# passband
fstop=[0.045 0.205]; %# frequency where it rolls off to half power
Rpass=1;%# max permissible ripples in stopband (dB)
Astop=40;%# min 40dB attenuation
n=cheb2ord(fpass,fstop,Rpass,Astop);%# calculate minimum filter order to achieve these design requirements

[b,a]=cheby2(n,Astop,fstop);

Now if you look at the zero-pole diagram using zplane(b,a), you'll see that there are several poles (x) lying outside the unit circle, which makes this approach unstable.

and this is evident from the fact that the frequency response is all haywire. Use freqz(b,a) to get the following

To get a more stable filter with your exact design requirements, you'll need to use second order filters using the z-p-k method instead of b-a, in MATLAB. Here's how for the same filter as above:

[z,p,k]=cheby2(n,Astop,fstop);
[s,g]=zp2sos(z,p,k);%# create second order sections
Hd=dfilt.df2sos(s,g);%# create a dfilt object.

Now if you look at the characteristics of this filter, you'll see that all the poles lie inside the unit circle (hence stable) and matches the design requirements

The approach is similar for butter and ellip, with equivalent buttord and ellipord. The MATLAB documentation also has good examples on designing filters. You can build upon these examples and mine to design a filter according to what you want.

To use the filter on your data, you can either do filter(b,a,data) or filter(Hd,data) depending on what filter you eventually use. If you want zero phase distortion, use filtfilt. However, this does not accept dfilt objects. So to zero-phase filter with Hd, use the filtfilthd file available on the Mathworks file exchange site

EDIT

This is in response to @DarenW's comment. Smoothing and filtering are two different operations, and although they're similar in some regards (moving average is a low pass filter), you can't simply substitute one for the other unless it you can be sure that it won't be of concern in the specific application.

For example, implementing Daren's suggestion on a linear chirp signal from 0-25kHz, sampled at 100kHz, this the frequency spectrum after smoothing with a Gaussian filter

Sure, the drift close to 10Hz is almost nil. However, the operation has completely changed the nature of the frequency components in the original signal. This discrepancy comes about because they completely ignored the roll-off of the smoothing operation (see red line), and assumed that it would be flat zero. If that were true, then the subtraction would've worked. But alas, that is not the case, which is why an entire field on designing filters exists.