
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
运行结果:

如果以60HZ 为截止频率,
lowpass(x,60,1000)

若把Steepness,设置为0.999
lowpass(x,60,1000,'Steepness',0.999);

发现邻近60Hz,出现了一个假的peak,建议不要把stepness 调的太接近1。
注意,从设定的频率上限,信号大小并不是直接变0,而是有一段转折的区间 w。

其中,

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

优点:简单,方便,直接;
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

3. fdatool——Filter Designer App
请键入
filterDesigner 或者 fdatool。将会出现这个界面。

主要修改,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')
运行结果:

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

参考文献:
Lowpass-filter signals - MATLAB lowpasswww.mathworks.com
