python小波去噪实验
import matplotlib.pyplot as plt
import pywt
import pandas as pd
import numpy as np
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])
data_array = np.array(data0.stack())
data = data_array.tolist()
num = len(data)
print(num)
sampling_rate = 4000
t = np.arange(0, 1.0, 1.0/sampling_rate)
w = pywt.Wavelet('db8')
maxlev = pywt.dwt_max_level(len(data), w.dec_len)
print("maximum level is " + str(maxlev))
threshold = 0.2
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()
结果