方法1
可以通过Filter Builder交互界面设计,这里选择Lowpass。
指定参数,选择FIR滤波器,采样频率128Hz,通带截止频率15Hz,阻带截止频率16Hz。通带最大波纹0.1dB,阻带最小衰减60dB。这里选用Kaiser窗法设计(或等波纹法equiripple)。
可以生成对应的代码
function Hd = getFilter %GETFILTER Returns a discrete-time filter object. % MATLAB Code % Generated by MATLAB(R) 9.3 and DSP System Toolbox 9.5. % Generated on: 22-Jun-2020 15:08:24 Fpass = 15; % Passband Frequency Fstop = 16; % Stopband Frequency Apass = 0.1; % Passband Ripple (dB) Astop = 60; % Stopband Attenuation (dB) Fs = 128; % Sampling Frequency h = fdesign.lowpass('fp,fst,ap,ast', Fpass, Fstop, Apass, Astop, Fs); % 等波纹法 %Hd = design(h, 'equiripple', 'MinOrder', 'any', 'StopbandShape', 'flat'); % kaiser窗法 Hd = design(h, 'kaiserwin');
查看滤波器特性
Hlp = getFilter; freqz(Hlp) % fvtool(lpFilt)
通过filtfilt获得零相位延迟的滤波
y = filtfilt(Hlp.Numerator, 1, x); % filtfilt(b, 1, x)
滤波结果
方法2
也可以直接通过fdesign和design设计,跟前面是等价的。
Fpass = 15; % Passband Frequency Fstop = 16; % Stopband Frequency Apass = 0.1; % Passband Ripple (dB) Astop = 60; % Stopband Attenuation (dB) Fs = 128; % Sampling Frequency h = fdesign.lowpass('fp,fst,ap,ast', Fpass, Fstop, Apass, Astop, Fs); % 等波纹法 %Hd = design(h, 'equiripple', 'MinOrder', 'any', 'StopbandShape', 'flat'); % kaiser窗法 Hd = design(h, 'kaiserwin');
方法3
也可以通过designfilt设计数字滤波器,结果是一样的。
lpFilt = designfilt('lowpassfir','PassbandFrequency',15/64, ... 'StopbandFrequency',16/64,'PassbandRipple',0.1, ... 'StopbandAttenuation',60,'DesignMethod','kaiserwin'); y = filtfilt(lpFilt, x);
滤波结果