# 1. 绘制原始图形曲线
from pandas import read_csv
import pandas as pd #导入pandas
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline
from scipy import interpolate
dataframe = read_csv('2010 - 1.csv', usecols=[1], engine='python', skipfooter=0)
y = dataframe.values
length = len(y)
# 2. 数据减少到1/3
x_new = [i for i in range(1,length+1,3)] #将数据缩为1/3
y_smooth = y[x_new]
print("y_smooth的后5个数据是\n",y_smooth[-5:])##查看数据框的尾部数据
# 2. 使用插值法,将缩减到减少到1/3的数据扩充到原有的数量
x_new_1 = np.linspace(1, length, length)
tck = interpolate.splrep(x_new, y_smooth,k=3) #样本点导入,生成参数
y_bspline = interpolate.splev(x_new_1, tck) #根据原有长度和样条参数,生成插值
# 3. 数据存储
y_bspline= pd.DataFrame(y_bspline)
y_bspline=y_bspline.values
print(y_bspline.shape)
new=np.hstack((y,y_bspline))
columns=["实际","处理" ]
df1 = pd.DataFrame(new,columns = columns )
#df1 = pd.DataFrame({'a_name':y,'b_name':y1})
df1.to_csv(r'2.csv',columns=columns )#存储到CSV中
plt.figure(figsize=(8,6))
plt.plot(y,'red',label='orginal data', marker='.')
plt.plot(y_bspline,'green',label='processed data',marker='.')
plt.title('Dada compare')
plt.legend()
plt.show()
三次样条去噪
最新推荐文章于 2021-05-14 15:52:18 发布