(python实现)FIR滤波器和IIR滤波器设计

FIR Filter

python 复制代码
def design_fir_filter(Fs, pass_freq, stop_freq, pass_attenuation, stop_attenuation, density_factor, filter_type):
    nyquist_freq = Fs * 0.5
    # filter_order2 = int(density_factor * nyquist_freq / (np.min(np.diff(stop_freq))))

    if (filter_type == 'low_pass'):
        if isinstance(pass_freq, list) or isinstance(stop_freq, list):
            print('pass_freq or stop_freq length not 1 !')
            return False
        filter_order = density_factor
        # filter_order = int(density_factor * nyquist_freq / (pass_freq - stop_freq))
        filter_coeff = signal.remez(filter_order + 1, [0, pass_freq, pass_freq + stop_freq, nyquist_freq],
                                [1, 0], [pass_attenuation, stop_attenuation], fs=Fs)
    elif (filter_type == 'high_pass'):
        if isinstance(pass_freq, list) or isinstance(stop_freq, list):
            print('pass_freq or stop_freq length not 1 !')
            return False
        filter_order = density_factor
        filter_coeff = signal.remez(filter_order + 1, [0, pass_freq - stop_freq, pass_freq , nyquist_freq],
                                [0, 1], [stop_attenuation, pass_attenuation], fs=Fs)
    else:
        filter_order = density_factor
        filter_coeff = signal.remez(filter_order + 1, [0, pass_freq[0]- stop_freq[0], pass_freq[0], pass_freq[1], pass_freq[1] + stop_freq[1], nyquist_freq],
                                [0, 1, 0], [stop_attenuation, pass_attenuation, stop_attenuation], fs=Fs)
    plot_response(filter_coeff, Fs, filter_type)
    return filter_coeff

def apply_fir_filter(filter_coeff, input_signal, num=None):
    if num is None:
        num = input_signal.shape[0]

    filtered_signals = np.zeros_like(input_signal[:num, :])

    for i in range(num):
        filtered_signals[i, :] = signal.lfilter(filter_coeff, 1, input_signal[i, :])

    return filtered_signals

IIR Filter

python 复制代码
def design_iir_butterworth_filter(pass_freq, stop_freq, gpass, gstop, fs_in=None, design_type='sos', filter_type='butterworth'):
    if design_type is 'sos':
        sos = signal.iirdesign(pass_freq, stop_freq, gpass, gstop, analog=False, ftype='butter', output='sos', fs=None)
        filter_coeff = sos
    else:
        b, a = signal.iirdesign(pass_freq, stop_freq, gpass, gstop, analog=False, ftype='butter', output='ba', fs=None)
        filter_coeff = [b, a]
    plot_response(filter_coeff, fs_in, filter_type)
    return filter_coeff

def apply_iir_filter(filter_coeff, input_signal, design_type='sos', num=None):
    if num is None:
        num = input_signal.shape[0]

    filtered_signals = np.zeros_like(input_signal[:num, :])
    if design_type is 'sos':
        for i in range(num):
            filtered_signals[i, :] = signal.sosfilt(filter_coeff, input_signal[i, :])
    else:
        for i in range(num):
            filtered_signals[i, :] = signal.lfilter(filter_coeff[0], filter_coeff[1], input_signal[i, :])

    return filtered_signals
相关推荐
起名字真南14 分钟前
【OJ题解】C++实现字符串大数相乘:无BigInteger库的字符串乘积解决方案
开发语言·c++·leetcode
tyler_download26 分钟前
golang 实现比特币内核:实现基于椭圆曲线的数字签名和验证
开发语言·数据库·golang
小小小~26 分钟前
qt5将程序打包并使用
开发语言·qt
hlsd#27 分钟前
go mod 依赖管理
开发语言·后端·golang
哇咔咔哇咔27 分钟前
【科普】conda、virtualenv, venv分别是什么?它们之间有什么区别?
python·conda·virtualenv
小春学渗透28 分钟前
Day107:代码审计-PHP模型开发篇&MVC层&RCE执行&文件对比法&1day分析&0day验证
开发语言·安全·web安全·php·mvc
杜杜的man31 分钟前
【go从零单排】迭代器(Iterators)
开发语言·算法·golang
亦世凡华、31 分钟前
【启程Golang之旅】从零开始构建可扩展的微服务架构
开发语言·经验分享·后端·golang
测试界的酸菜鱼1 小时前
C# NUnit 框架:高效使用指南
开发语言·c#·log4j
GDAL1 小时前
lua入门教程 :模块和包
开发语言·junit·lua