python小波去噪实验
作者:互联网
python小波去噪实验
import matplotlib.pyplot as plt
import pywt
import pandas as pd
import numpy as np
#读取csv文件转换为列表序列
path = "data_ce(轴承)/48k_Drive_End_B007_0_122_1.csv"
pathi = "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('shipintu/'+pathi, encoding='gbk')
结果
标签:plt,python,小波,len,coeffs,实验,print,pywt,data 来源: https://blog.csdn.net/qq_62022086/article/details/123222053