[python scipy] 记录一次scipy信号处理 低通滤波

该文章通过Python的scipy库演示了如何对信号进行低通滤波处理,主要步骤包括读取CSV数据,计算平均值,确定采样率,设置截止频率,应用butterworth滤波器,以及可视化原始和过滤后的信号。
摘要由CSDN通过智能技术生成

scipy信号滤波,讲解详细!https://blog.csdn.net/weixin_41521681/article/details/108262389

import matplotlib.pyplot as plt#import module to graph
import pandas as pd#import module to read the files
import numpy as np#import module to calculate
from scipy import signal#import module to fliter the data


list_data = ["80+1.csv", "80+2.csv", "80+3.csv", "120+1.csv", "120+2.csv", "120+3.csv", "160+1.csv", "160+2.csv", "160+3.csv"]
list_i = [1, 2, 3, 4, 5, 6, 7, 8, 9]

for o, i in zip(list_data, list_i):  # import the loop

    data = pd.read_csv(o)  # read the data
    y = data.to_numpy()  # turn data into numpy array
    sampling_rate = 5000  # sample rate is 5000Hz

    y_av = np.average(y)
    y_subtracted = y-y_av

    sampling_time = 0.1
    start_value = int(np.argmax(y_subtracted))
    end_value = int(start_value + sampling_time*sampling_rate)
    y_inrange = y_subtracted[start_value:end_value]

    x = np.linspace(0, len(y_subtracted)/sampling_rate, len(y_subtracted))
    x1 = np.linspace(start_value, end_value, len(y_inrange))

    # 计算wn
    cutoff_frequency = 80.0
    normalised_cutoff = (2.0*cutoff_frequency / sampling_rate)

    # 进行低通滤波
    b, a = signal.butter(4, normalised_cutoff, btype='low')
    y_filtered = signal.filtfilt(b, a, y_inrange.flatten())

    # d[i+1]>d[i] 的序号
    y_filtered_sign = np.argwhere(np.diff(np.sign(y_filtered))).flatten()
    
    fig, ax1 = plt.subplots()
    ax1.set_ylabel("Amplitude")
    ax1.set_xlabel("Time (s)")
    plt2 = ax1.plot(x1, y_inrange, color='green')
    plt1 = ax1.scatter(x1, y_inrange, s=1, color='red')

    # 显示
    plt4 = ax1.plot(x1[y_filtered_sign],
                    y_inrange[y_filtered_sign],
                    color='blue')
    plt.show()

    fig.savefig("figure{}.png".format(i), dpi=300)  # save the graphs
    c = np.argwhere(np.diff(np.sign(y), n=1) != 0)

结果显示

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放飞自我的Coder

你的鼓励很棒棒哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值