1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| import numpy as np import pandas as pd import matplotlib.pyplot as plt
test_1 = np.array([1529.808, 1529.807, 1529.813, 1529.812, 1529.814, 1529.809]) test_2 = np.array([1541.095, 1541.092, 1541.090, 1541.093, 1541.094, 1541.091])
test1 = test_1 / np.mean(test_1) test2 = test_2 / np.mean(test_2) dist = 0.6
test_points = np.array([1*dist, 2*dist, 3*dist, 4*dist, 5*dist, 6*dist])
df = pd.DataFrame({ "testpoint": test_points, "test1": test1, "test2": test2 })
x = df["testpoint"].values y1 = df["test1"].values y2 = df["test2"].values
coef1_cubic = np.polyfit(x, y1, 3) coef2_cubic = np.polyfit(x, y2, 3) poly1_cubic = np.poly1d(coef1_cubic) poly2_cubic = np.poly1d(coef2_cubic)
x_dense = np.linspace(min(x), max(x), 100) y1_dense = poly1_cubic(x_dense) y2_dense = poly2_cubic(x_dense)
plt.figure(figsize=(8, 5)) plt.scatter(x, y1, color="blue", label="Test1 Data") plt.scatter(x, y2, color="red", label="Test2 Data") plt.plot(x_dense, y1_dense, linestyle="--", color="blue", alpha=0.7, label="Cubic Fit 1 (smooth)") plt.plot(x_dense, y2_dense, linestyle="--", color="red", alpha=0.7, label="Cubic Fit 2 (smooth)")
plt.xlabel("Test Point Distance") plt.ylabel("Normalized Wavelength") plt.legend() plt.title("Cubic Polynomial Fit (Smoothed)") plt.grid() plt.show()
print("Test1 三次拟合系数:", coef1_cubic) print("Test2 三次拟合系数:", coef2_cubic)
test=[test1,test2]
x_need=[0.3,0.4,0.5,0.6,0.7] y1_predict=poly1_cubic(x_need) y2_predict=poly2_cubic(x_need) y1_predict=y1_predict*np.mean(test_1) y2_predict=y2_predict*np.mean(test_2)
lamda0=[1529,1540] c=4200 def lamda2K(y,i): return(c*(y-lamda0[i]))/lamda0[i-1] y1_predict=lamda2K(y1_predict,0) y2_predict=lamda2K(y2_predict,1)
def get_K(x,i): y_predict=poly1_cubic(x)/np.mean(test[i-1]) y_predict=lamda2K(y_predict,i-1)
print(y1_predict) print(y2_predict)
|