![4c429e6dca78589fdc69f2f671262c7c.png](https://i-blog.csdnimg.cn/blog_migrate/52aef4237fd0ad389bce425b5f52c645.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](https://i-blog.csdnimg.cn/blog_migrate/0d07a9ba9f4aab810800ed0857a42f10.jpeg)
如果以60HZ 为截止频率,
lowpass(x,60,1000)
![5828e9e99c5c4f86b5c0914a5ec3bb8e.png](https://i-blog.csdnimg.cn/blog_migrate/58bf4839881c0d132ef358b650be75d2.jpeg)
若把Steepness,设置为0.999
lowpass(x,60,1000,'Steepness',0.999);
![c4875f484df1e6e34a5a8f2255bb772c.png](https://i-blog.csdnimg.cn/blog_migrate/5f1da0209fc387ca61e050625f044acb.jpeg)
发现邻近60Hz,出现了一个假的peak,建议不要把stepness 调的太接近1。
注意,从设定的频率上限,信号大小并不是直接变0,而是有一段转折的区间 w。
![388fc17f98a80dbb9db93b3e7592a782.png](https://i-blog.csdnimg.cn/blog_migrate/6f68a16b7e7b3b065e800674d2cec0e7.png)
其中,
![cd1543e344358bbdaf01d2ec50dce559.png](https://i-blog.csdnimg.cn/blog_migrate/36b1647677a470ddd54555a6b775799f.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](https://i-blog.csdnimg.cn/blog_migrate/2ad4058117cda4ee985a4322a29da886.jpeg)
优点:简单,方便,直接;
2. 设计 低通滤波器(Designing Low Pass FIR Filters)
matlab还支持直接设计低通滤波器的参数,需要用到两个参数firceqrip
andfirgr
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](https://i-blog.csdnimg.cn/blog_migrate/0ab6cdd059abfb3e230309bb8c326965.jpeg)
3. fdatool——Filter Designer App
请键入
filterDesigner 或者 fdatool。将会出现这个界面。
![07508bfe88f160937410b16aac2fc50b.png](https://i-blog.csdnimg.cn/blog_migrate/cec8b6c0aff0ce005b984e30dd401034.jpeg)
主要修改,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](https://i-blog.csdnimg.cn/blog_migrate/6f77f0399077976aebba430f6a9d3118.jpeg)
如果Fstop = 65; ,则过滤后的信号,接近0的初始段将会变长,
![0a57c47bd33c0e92a6e0ab5d13f91db6.png](https://i-blog.csdnimg.cn/blog_migrate/5f24091330dae640e59fc3935efef3c8.jpeg)
参考文献:
Lowpass-filter signals - MATLAB lowpasswww.mathworks.com![0d443484d54c11d896e19f36f4c24d41.png](https://i-blog.csdnimg.cn/blog_migrate/1981986e1bd77382821cafb1bddb1a3a.jpeg)
![620ad4c99983207f267345f8db067010.png](https://i-blog.csdnimg.cn/blog_migrate/3780527caaeb887362d99f7e1a0e4989.png)