欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一阶RC低通滤波

程序员文章站 2024-03-25 21:49:52
...

计算流程

一阶RC低通滤波

代码

import numpy as np
from matplotlib import pyplot as plt
from matplotlib.pylab import mpl
zhfont = mpl.font_manager.FontProperties(fname='C:\\Windows\\Fonts\\simsun.ttc')
fontcn = {'fontproperties': zhfont, 'size': 20, 'color': 'red'}
mpl.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文
mpl.rcParams['axes.unicode_minus'] = False  # 显示负号
#     入口:NEW_DATA 新采样值,OLD_DATA 上次滤波结果,k滤波系数
#     出口:本次滤波结果

def filter_1(NEW_DATA,OLD_DATA, k):
    if NEW_DATA < OLD_DATA:
        result = OLD_DATA - abs(OLD_DATA-NEW_DATA) * k
    elif NEW_DATA > OLD_DATA:
        result = OLD_DATA + abs(OLD_DATA - NEW_DATA) * k
    else:
        result = OLD_DATA
    return result


if __name__ == "__main__":
    R = 1000
    C = 4.7
    tao = R*C
    t = 1800
    k = t / tao
    q2 = np.zeros((1, 500))
    for i in range(3, 200):
        q2[0, i] = 20 + 3*np.sin(0.3*i)
    for i in range(200, 500):
        q2[0, i] = 25 + 3*np.sin(0.3*i)
    q2[0, 0] = 0
    q2[0, 1] = 5
    q2[0, 2] = 11
    record = []
    OLD_DATA = 0
    for i in range(1, 500):
        OLD_DATA = filter_1(q2[0, i], OLD_DATA, k)
        record.append(OLD_DATA)
    record.insert(0, 0)
    _x = range(1, 501)
    _x1 = np.zeros((1, 500))
    for i in range(500):
        _x1[0, i] = i+1
    plt.figure(figsize=(40,24), dpi=200)
    plt.plot(list(_x), record, "o:", markersize=1)
    plt.plot(_x1, q2, "*:", markersize=1)
    plt.xlabel('采样次数', fontcn)
    plt.ylabel('数值', fontcn)
    plt.tick_params(labelsize=20)
    plt.legend(['滤波效果', '采样值'])
    plt.savefig('一阶RC低通滤波.jpg')
    plt.show()

滤波效果

一阶RC低通滤波

相关标签: 信号处理 python