赞
踩
线性插值是最简单的插值方法。它假设输入数据点之间的函数在每个点上是线性的。在找到输入点之间的位置后,线性插值函数会计算出新位置处的函数值。
‘slinear’插值方法使用线性插值的方式对原始数据进行平滑。在调用interp1d函数时,需要将参数kind设置成‘slinear’。
三次样条插值使用光滑的三次多项式来逼近输入数据点之间的函数。这个插值方法会生成一个三次样条函数,它在离散点之间具有三阶平滑性。使用三次样条插值方法时,在调用interp1d函数时,需要将参数kind设置成‘cubic’。示例代码如下所示:
- import numpy as np
- import matplotlib.pyplot as plt
- from scipy.interpolate import interp1d
-
- # 参数设置
- amplitude = 1.0 # 振幅
- frequency = 5.0 # 频率(周期为2*pi/frequency)
- sample_rate = 1000 # 采样率
- duration = 1.0 # 波形持续时间
-
- # 生成时间序列
- t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
-
- # 生成正弦波形
- signal = amplitude * np.sin(2 * np.pi * frequency * t)
-
- # 对正弦波进行离散采样
- sample_points = np.arange(0, len(t), step=50) # 每50个点采样一次
- sampled_signal = signal[sample_points]
-
- # 绘制原始波形
- plt.subplot(5, 1, 1) # 表示将整个图像窗口分为3行1列, 当前位置为1
- plt.plot(t, signal)
- plt.title('Original Sinusoidal Waveform')
- plt.xlabel('Time (s)')
- plt.ylabel('Amplitude')
- plt.grid(True)
-
- # 绘制采样后的波形和拟合曲线
- plt.subplot(5, 1, 2)
- plt.plot(sample_points / sample_rate, sampled_signal)
- plt.stem(sample_points / sample_rate, sampled_signal, markerfmt='ro', linefmt='r-', basefmt='k-')
- # plt.plot(t, fit_curve, 'g--', label='Fit Curve')
- plt.title('Sampled Sinusoidal Waveform with Fit Curve')
- plt.xlabel('Time (s)')
- plt.ylabel('Amplitude')
- plt.legend()
- plt.grid(True)
-
- # 绘制插值后的曲线
- # f = interp1d(sample_points / sample_rate,sampled_signal, kind='cubic')
- f = interp1d(sample_points / sample_rate,sampled_signal, kind='linear')
- # f = interp1d(sample_points / sample_rate,sampled_signal, kind='slinear')
- x_interp = np.linspace(min(sample_points / sample_rate), max(sample_points / sample_rate), 40)
- y_interp = f(x_interp)
- plt.subplot(5, 1, 3)
- plt.plot(x_interp, y_interp, label='Smoothed Curve', color='blue')
- plt.stem(x_interp, y_interp, markerfmt='ro', linefmt='r-', basefmt='k-')
- plt.title('kind = linear')
- plt.xlabel('Time (s)')
- plt.ylabel('Amplitude')
- plt.tight_layout()
- # plt.show()
-
- # 绘制插值后的曲线
- # f = interp1d(sample_points / sample_rate,sampled_signal, kind='cubic')
- # f = interp1d(sample_points / sample_rate,sampled_signal, kind='linear')
- f = interp1d(sample_points / sample_rate,sampled_signal, kind='slinear')
- x_interp = np.linspace(min(sample_points / sample_rate), max(sample_points / sample_rate), 40)
- y_interp = f(x_interp)
- plt.subplot(5, 1, 4)
- plt.plot(x_interp, y_interp, label='Smoothed Curve', color='blue')
- plt.stem(x_interp, y_interp, markerfmt='ro', linefmt='r-', basefmt='k-')
- plt.title('kind = slinear')
- plt.xlabel('Time (s)')
- plt.ylabel('Amplitude')
- plt.tight_layout()
- # plt.show()
-
- # 绘制插值后的曲线
- f = interp1d(sample_points / sample_rate,sampled_signal, kind='cubic')
- # f = interp1d(sample_points / sample_rate,sampled_signal, kind='linear')
- x_interp = np.linspace(min(sample_points / sample_rate), max(sample_points / sample_rate), 40)
- y_interp = f(x_interp)
- plt.subplot(5, 1, 5)
- plt.plot(x_interp, y_interp, label='Smoothed Curve', color='blue')
- plt.stem(x_interp, y_interp, markerfmt='ro', linefmt='r-', basefmt='k-')
- plt.title('kind = cubic')
- plt.xlabel('Time (s)')
- plt.ylabel('Amplitude')
- plt.tight_layout()
- plt.show()
运行结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。