filter函数的用法matlab_MATLAB 低通滤波器 low pass filter

本文介绍了MATLAB中设计和使用低通滤波器的方法,包括使用`lowpass`函数、直接设计FIR滤波器参数以及通过`fdatool`进行滤波器设计。强调了`lowpass`函数的使用注意事项,如stepness值的选择,以及在`fdatool`中调整Fs、Fpass和Fstop参数的影响。提供了实例和滤波器设计的实践应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

384c157809d2a97c0abe895f2b544c7c.png

1. lowpass 函数

注意,只有2018年之后的matlab才有lowpass, bandpass 函数。

lowpass(x,fpass,fs) % x 一维信号,fpass:截止频率,fs:采样频率。

例子:

两个频率的信号,一个是50HZ, 振幅为1, 一个是250HZ,振幅为2

fs = 1e3;                  %sampling rate per second
t = 0:1/fs:1;              % time series
x = [1 2]*sin(2*pi*[50 250]'.*t) + randn(size(t))/10; %两个频率的信号,一个是50HZ, 一个是250HZ.

做lowpass, 以150为截止频率

lowpass(x,150,fs)          %以150为频率上限,做lowpass

运行结果:

8c23903915d385196fc6a336eba1bc2f.png

如果以60HZ 为截止频率,

lowpass(x,60,1000)

53ea7b566b9e0e66fed6a8e7295f5de0.png

若把Steepness,设置为0.999

lowpass(x,60,1000,'Steepness',0.999);

a49597b161ed3193bddee0e663c8dbdc.png

发现邻近60Hz,出现了一个假的peak,建议不要把stepness 调的太接近1。

注意,从设定的频率上限,信号大小并不是直接变0,而是有一段转折的区间 w。

beaa6fa0ea31114541b43653b9eb9697.png

其中,

a3bdf49b70c05eaa26c3315a4dc42744.png

s 称为 stepness,默认值为0.85,可以调整。

fs = 1000;
x = randn(20000,1);

[y1,d1] = lowpass(x,200,fs,'ImpulseResponse','iir','Steepness',0.5);
[y2,d2] = lowpass(x,200,fs,'ImpulseResponse','iir','Steepness',0.8);
[y3,d3] = lowpass(x,200,fs,'ImpulseResponse','iir','Steepness',0.95);

pspectrum([y1 y2 y3],fs)
legend('Steepness = 0.5','Steepness = 0.8','Steepness = 0.95')

0eddd3044d8c41a5d9336b405164a19f.png

优点:简单,方便,直接;

2. 设计 低通滤波器(Designing Low Pass FIR Filters)

matlab还支持直接设计低通滤波器的参数,需要用到两个参数firceqripandfirgr

N 表示transition region 的sharp程度,数字越大,越sharp;

Fp:通带截止频率

Fs:信号采样频率

Rp:通带波纹ripple

Rst: 阻带衰减

N   = 100;        % FIR filter order
Fp  = 20e3;       % 20 kHz passband-edge frequency
Fs  = 96e3;       % 96 kHz sampling frequency
Rp  = 0.00057565; % Corresponds to 0.01 dB peak-to-peak ripple
Rst = 1e-4;       % Corresponds to 80 dB stopband attenuation

eqnum = firceqrip(N,Fp/(Fs/2),[Rp Rst],'passedge'); % eqnum = vec of coeffs
fvtool(eqnum,'Fs',Fs,'Color','White') % Visualize filter

df6297eb1199f290b99ebc667e142545.png

3. fdatool——Filter Designer App

请键入

filterDesigner 或者 fdatool。将会出现这个界面。

77f7c116641359574be42a05c9076416.png

主要修改,Fs (sampling frequency ), Fpass, Fstop

设置好之后,保存到m文件。

调用方法:

 filter(filter_test4, x);

注意不要把Fpass, Fstop调的太接近,否则过滤后的信号,将会有一段很接近0的区域,越接近,这段区域越长。

我的filter_test4.m

function Hd = filter_test4
%FILTER_TEST5 Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.7 and DSP System Toolbox 9.9.
% Generated on: 31-May-2020 12:50:15
% Equiripple Lowpass filter designed using the FIRPM function.
% All frequency values are in Hz.
Fs = 1000;  % Sampling Frequency
Fpass = 60;              % Passband Frequency
Fstop = 100;              % Stopband Frequency
Dpass = 0.057501127785;  % Passband Ripple
Dstop = 0.0001;          % Stopband Attenuation
dens  = 20;              % Density Factor
% Calculate the order from the parameters using FIRPMORD.
[N, Fo, Ao, W] = firpmord([Fpass, Fstop]/(Fs/2), [1 0], [Dpass, Dstop]);
% Calculate the coefficients using the FIRPM function.
b  = firpm(N, Fo, Ao, W, {dens});
Hd = dfilt.dffir(b);
% [EOF]

函数

fs = 1e3;                  %sampling rate per second 
t = 0:1/fs:1;              % time series 
x = [1 2]*sin(2*pi*[50 250]'.*t) + randn(size(t))/10; %两个频率的信号,一个是50HZ, 一个是250HZ.
x1= filter(filter_test4, x);
figure;
subplot(2,1,1);
plot(x,'DisplayName','Original signals');
hold on;
plot(x1,'DisplayName','Original signals');
legend;
subplot(2,1,2); % power spectrum
powerspectrum(x);%自己写的powerspectrum
hold on;
powerspectrum(x1);
legend('Original signals','Filtered signals')

运行结果:

f46ad75a968895cfe30236e8a8232bad.png

如果Fstop = 65; ,则过滤后的信号,接近0的初始段将会变长,

d5cb2f429379421981d1daac24a94871.png

参考文献:

Lowpass-filter signals - MATLAB lowpass​www.mathworks.com
6330bd6ffd44d607c4ff14ff3f418aba.png
Designing Low Pass FIR Filters​www.mathworks.com
2f6115b7f4aef0ebeb6fea428e4f5b1f.png
Designing Digital Filters with MATLAB Video​www.mathworks.comhttps://ww2.mathworks.cn/help/signal/ug/getting-started-with-filter-designer.html#btlstex-1​ww2.mathworks.cn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值