一、原始信号
1、理想数据
(1)系统参数
参数类型 | 数值 |
---|
J | 0.5
k
g
∗
m
2
kg*m^2
kg∗m2 |
K | 0.2 |
b | 5 |
(2)激励曲线
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 1000)
y = 20*np.sin(x)+15*np.sin(2*x)+10*np.cos(x)
plt.figure()
plt.plot(x, y, label='20sin(x)+15sin(2x)+10cos(x)', color='blue', linestyle='-', linewidth=2)
plt.xlabel('x')
plt.ylabel('pos')
plt.title('Excitation Curve')
plt.legend()
plt.grid(True)
plt.show()
(3)其他曲线
import matplotlib.pyplot as plt
import numpy as np
J = 0.5
K = 0.2
b = 51010
x = np.linspace(0, 10, 1000)
pos = 20*np.sin(x) + 15*np.sin(2*x) + 10*np.cos(x)
spd = 20*np.cos(x) + 30*np.cos(2*x) - 10*np.sin(x)
acc = -20*np.sin(x) - 60*np.sin(2*x) - 10*np.cos(x)
tor = J*acc + K*spd + np.sign(spd)*b
plt.figure()
plt.subplot(411)
plt.plot(x, pos, label='pos', color='blue', linestyle='-', linewidth=2)
plt.xlabel('x')
plt.ylabel('pos')
plt.legend()
plt.grid(True)
plt.subplot(412)
plt.plot(x, spd, label='spd', color='red', linestyle='-', linewidth=2)
plt.xlabel('x')
plt.ylabel('spd')
plt.legend()
plt.grid(True)
plt.subplot(413)
plt.plot(x, acc, label='acc', color='green', linestyle='-', linewidth=2)
plt.xlabel('x')
plt.ylabel('acc')
plt.legend()
plt.grid(True)
plt.subplot(414)
plt.plot(x, tor, label='tor', color='black', linestyle='-', linewidth=2)
plt.xlabel('x')
plt.ylabel('tor')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
2、增加噪声
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
J = 0.5
K = 0.2
b = 5
x = np.linspace(0, 10, 1000)
pos = 20*np.sin(x) + 15*np.sin(2*x) + 10*np.cos(x) + np.random.normal(0, 5, 1000)
spd = 20*np.cos(x) + 30*np.cos(2*x) - 10*np.sin(x) + np.random.normal(0, 5, 1000)
acc = -20*np.sin(x) - 60*np.sin(2*x) - 10*np.cos(x) + np.random.normal(0, 5, 1000)
tor = J*acc + K*spd + np.sign(spd)*b + np.random.normal(0, 5, 1000)
plt.figure()
plt.subplot(411)
plt.plot(x, pos, label='pos', color='blue', linestyle='-', linewidth=2)
plt.xlabel('x')
plt.ylabel('pos')
plt.legend()
plt.grid(True)
plt.subplot(412)
plt.plot(x, spd, label='spd', color='red', linestyle='-', linewidth=2)
plt.xlabel('x')
plt.ylabel('spd')
plt.legend()
plt.grid(True)
plt.subplot(413)
plt.plot(x, acc, label='acc', color='green', linestyle='-', linewidth=2)
plt.xlabel('x')
plt.ylabel('acc')
plt.legend()
plt.grid(True)
plt.subplot(414)
plt.plot(x, tor, label='tor', color='black', linestyle='-', linewidth=2)
plt.xlabel('x')
plt.ylabel('tor')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
data = {'x': x, 'pos': pos, 'spd': spd, 'acc': acc, 'tor': tor}
df = pd.DataFrame(data)
df.to_csv('data_with_noise.csv', index=False)
二、数据预处理
1、傅里叶变换画出频谱图
df = pd.read_csv('data_with_noise.csv')
column_name = 'pos'
data = df[column_name].values
fs = 100
fft_data = np.fft.fft(data)
freqs = np.fft.fftfreq(len(fft_data), 1/fs)
freqs = freqs[:len(freqs)//2]
plt.figure()
plt.plot(freqs, np.abs(fft_data[:len(fft_data)//2]))
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude')
plt.title('Spectrum of the data column')
plt.grid()
plt.show()
2、去除高频噪声并逆傅里叶变换
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data_with_noise.csv')
column_name = 'pos'
data = df[column_name].values
fs = 100
fft_data = np.fft.fft(data)
freqs = np.fft.fftfreq(len(fft_data), 1/fs)
freqs = freqs[:len(freqs)]
print(freqs)
fft_data_filtered = np.copy(fft_data)
fft_data_filtered[np.where(np.abs(freqs) > 0.5)] = 0
print(fft_data_filtered)
filtered_data = np.fft.ifft(fft_data_filtered)
new_df = pd.DataFrame({'Original Data': data, 'Filtered Data': np.real(filtered_data)})
new_df.to_csv('filtered_yaw_data.csv', index=False)
plt.figure()
plt.plot(data, label='Original Data')
plt.plot(np.real(filtered_data), label='Filtered Data (freq < 10Hz)')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Original Data vs Filtered Data')
plt.legend()
plt.grid()
plt.show()
三、动力学辨识
1、动力学模型
t
o
r
=
J
∗
α
+
K
∗
s
p
d
+
s
i
g
n
(
s
p
d
)
∗
b
tor = J*\alpha + K*spd+sign(spd)*b
tor=J∗α+K∗spd+sign(spd)∗b
2、最小二乘法
import numpy as np
import pandas as pd
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
df = pd.read_csv('data_with_noise.csv')
column_name_acc = 'Filtered_acc'
column_name_v = 'Filtered_spd'
column_name_tor = 'Filtered_tor'
data_acc = df[column_name_acc].values
data_v = df[column_name_v].values
data_tor = df[column_name_tor].values
def model_func(inputs, a, b, c):
acc, v = inputs
return a * acc + b * v + np.sign(v) * np.abs(c)
initial_guess = [1, 1, 1]
params, covariance = curve_fit(model_func, (data_acc, data_v), data_tor, initial_guess)
a_fit, b_fit, c_fit = params
print("拟合参数:")
print("a =", a_fit)
print("b =", b_fit)
print("c =", c_fit)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(data_acc, data_v, data_tor, color='b', label='Fitted Data')
ax.scatter(data_acc, data_v, model_func((data_acc, data_v), a_fit, b_fit, c_fit), color='r', label='Model Data')
ax.set_xlabel('Filtered Acceleration')
ax.set_ylabel('Filtered Speed')
ax.set_zlabel('Filtered Torque')
ax.set_title('Fitted vs. Actual Data')
plt.legend()
plt.show()
3、结果分析
参数类型 | 数值 | |
---|
J | 0.5 | 0.47 |
K | 0.2 | 0.275 |
b | 5 | -0.676 |
- 转动惯量的部分还是可以的,摩擦力的两个参数就有点离谱了,速度越大摩擦力还小了。这是我觉得辨识方法最尴尬的地方,结果是拟合了,参数没拟合。这还是单自由度的情况下,如果是多自由度的整体辨识,大量参数耦合在一起,就更难说清到底是哪些参数起到了什么样的作用了,这也没比基于神经网络的纯黑盒强多少。