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)
![](https://i-blog.csdnimg.cn/blog_migrate/424e4f81fc97f4ac1914eb8b89d65660.png)