大家好,欢迎来到IT知识分享网。
滤波器性能评估
- 幅频响应:查看滤波器的幅频响应曲线,比较通带平坦度和阻带衰减。
- 相频响应:观察相频响应,以确保相位响应平滑,尤其在需要线性相位的应用中。
- 均方误差(MSE):计算原始信号与滤波后信号之间的均方误差,数值越小表示滤波器性能越好。
- 信噪比(SNR):计算滤波后信号的信噪比,信噪比越高表示滤波效果越好。
设计低通、高通、带通和带阻滤波器
凯撒窗为例设计fir滤波器,示例代码显示滤波器的幅频响应、相频响应、均方误差和信噪比,可根据这几个性能参数来优化性能。(tip:最近学习相关知识,记录学习笔记,如有不同想法,欢迎探讨)
%% Clear clc; clear; close all; % 创建一个测试信号,包含50Hz、100hz、150hz和200Hz的成分 fs = 1000; t = 0:1/fs:1-1/fs; % 时间向量 raw = 0.5 * sin(2*pi*50*t) + sin(2*pi*100*t) + 2*sin(2*pi*150*t) + 1.5*sin(2*pi*200*t); % 合成信号 % 原信号绘制 index = 1; plot_fig(raw, index, raw, fs, '原信号') %% FIR滤波器--凯撒窗 % 低通滤波 fcuts = [100, 120]; mags = [1, 0]; devs = [0.01, 10^(-60 / 20)]; % 通带波纹和阻带衰减,阻带衰减将db转为线性比例,10^(-阻带衰减 / 20) [n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fs); hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale'); % noscale参数控制不对滤波器的幅度响应进行归一化。默认情况下,fir1函数会自动进行归一化 index = index + 1; figure (index); freqz(hh,1,1024,fs) title('低通滤波器') y = conv(raw,hh,"same"); index = index + 1; plot_fig(raw, index, y, fs, '凯撒窗低通滤波信号') % 高通滤波 fcuts = [120, 150]; mags = [0, 1]; devs = [0.01, 10^(-60 / 20)]; % 通带波纹和阻带衰减,阻带衰减需讲db转为线性比例,10^(-阻带衰减 / 20) [n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fs); hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale'); % noscale选项控制不对滤波器的幅度响应进行归一化。默认情况下,fir1函数会自动进行归一化 index = index + 1; figure (index); freqz(hh,1,1024,fs) title('高通滤波器') y = conv(raw,hh,"same"); index = index + 1; plot_fig(raw, index, y, fs, '凯撒窗高通滤波信号') % 带通滤波 fcuts = [75, 100, 150, 175]; mags = [0, 1, 0]; devs = [10^(-50 / 20), 0.01, 10^(-50 / 20)]; % 通带波纹和阻带衰减,阻带衰减需讲db转为线性比例,10^(-阻带衰减 / 20) [n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fs); hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale'); % noscale选项控制不对滤波器的幅度响应进行归一化。默认情况下,fir1函数会自动进行归一化 index = index + 1; figure (index); freqz(hh,1,1024,fs) title('带通滤波器') y = conv(raw,hh,"same"); index = index + 1; plot_fig(raw, index, y, fs, '凯撒窗带通滤波信号') % 带阻滤波 fcuts = [95, 100, 150, 155]; mags = [1, 0, 1]; devs = [0.01, 10^(-50 / 20), 0.01]; % 通带波纹和阻带衰减,阻带衰减需讲db转为线性比例,10^(-阻带衰减 / 20) [n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fs); hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale'); % noscale选项控制不对滤波器的幅度响应进行归一化。默认情况下,fir1函数会自动进行归一化 index = index + 1; figure (index); freqz(hh,1,1024,fs) title('带阻滤波器') y = conv(raw,hh,"same"); index = index + 1; plot_fig(raw, index, y, fs, '凯撒窗带阻滤波信号') function [] = plot_fig(raw, index, x, Fs, title1) % 计算单边幅度谱 temp = abs(fft(x)); ly = length(temp); temp = temp(1:ly/2+1); temp(2:end-1) = 2 * temp(2:end-1); f = (0:ly/2)/ly*Fs; % 绘图 figure(index) subplot(211) plot(x) title(title1+"时域图") subplot(212) plot(f,temp) title(title1+"幅度谱") % 评估滤波器性能,计算均方误差 mse1 = mean((raw - x).^2); % 评估滤波器性能,计算信噪比 snr1 = snr(x, raw - x); fprintf('%s: MSE = %.4f, SNR = %.2f dB\n', title1, mse1, snr1); end
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/130033.html