matlab filter函数_MATLAB 低通滤波器 low pass filter

4c429e6dca78589fdc69f2f671262c7c.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

运行结果:

d8aa0a726041108b1fd3a84242792a94.png

如果以60HZ 为截止频率,

lowpass(x,60,1000)

5828e9e99c5c4f86b5c0914a5ec3bb8e.png

若把Steepness,设置为0.999

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

c4875f484df1e6e34a5a8f2255bb772c.png

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

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

388fc17f98a80dbb9db93b3e7592a782.png

其中,

cd1543e344358bbdaf01d2ec50dce559.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')

ec9607a8dbf5651ceb3faba27a89c94a.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

1c8a1ce05a5b3d145a3a88d70150d200.png

3. fdatool——Filter Designer App

请键入

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

07508bfe88f160937410b16aac2fc50b.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')

运行结果:

6488cf53b4b6897f4d7e84a0fecd74a3.png

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

0a57c47bd33c0e92a6e0ab5d13f91db6.png

参考文献:

Lowpass-filter signals - MATLAB lowpass​www.mathworks.com
0d443484d54c11d896e19f36f4c24d41.png
Designing Low Pass FIR Filters​www.mathworks.com
620ad4c99983207f267345f8db067010.png
Designing Digital Filters with MATLAB Video​www.mathworks.com https://ww2.mathworks.cn/help/signal/ug/getting-started-with-filter-designer.html#btlstex-1​ww2.mathworks.cn
  • 3
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值