python小波去噪实验

import matplotlib.pyplot as plt
import pywt
import pandas as pd
import numpy as np

#读取csv文件转换为列表序列
path = "48k_Drive_End_B007_0_122_1.csv"
data0 = pd.read_csv(path, usecols=[1]) #读取'Column2'列的数据
data_array = np.array(data0.stack())  # 首先将pandas读取的数据转化为array
data = data_array.tolist()  # 然后转化为list形式
num = len(data)
print(num)
sampling_rate = 4000  #采样频率
t = np.arange(0, 1.0, 1.0/sampling_rate)

# Create wavelet object and define parameters
w = pywt.Wavelet('db8')  # 选用Daubechies8小波
maxlev = pywt.dwt_max_level(len(data), w.dec_len)
print("maximum level is " + str(maxlev))
threshold = 0.2  # Threshold for filtering

# Decompose into wavelet components, to the level selected:
coeffs = pywt.wavedec(data, 'db8', level=maxlev)  # 将信号进行小波分解

plt.figure()
for i in range(1, len(coeffs)):
    coeffs[i] = pywt.threshold(coeffs[i], threshold*max(coeffs[i]))  # 将噪声滤波

datarec = pywt.waverec(coeffs, 'db8')  # 将信号进行小波重构

mintime = 0
maxtime = mintime + len(data) + 1

# 画图
plt.figure()
# 第一幅图
plt.subplot(2, 1, 1)
plt.plot(t, data[mintime:maxtime])
plt.xlabel('time (s)')
plt.ylabel('microvolts (uV)')
plt.title("Raw signal")
# 第二幅图
plt.subplot(2, 1, 2)
plt.plot(t, datarec[mintime:maxtime-1])
plt.xlabel('time (s)')
plt.ylabel('microvolts (uV)')
plt.title("De-noised signal using wavelet techniques")

plt.tight_layout()
plt.show()

# print(data)
# print("----------------------------------------------------------")
# print(datarec)
# print(len(datarec))

# 将处理后的数据存入CSV文件
# name = ['columns']
# test = pd.DataFrame(columns=name, data=datarec)  # 数据有三列,列名分别为one,two,three
# print(test)
# test.to_csv("122_1.csv", encoding='gbk')

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值