兩個波形文件(如音頻、振動、傳感器數據),一個為基準期望值,一個為待測輸出。需自動判斷兩者是否「足夠相似」。
MSE = mean((y_true - y_pred)^2)r = cov(x,y) / (std(x)*std(y))import numpy as np
from scipy.spatial.distance import euclidean
from fastdtw import fastdtw
def compare_waveforms(ref_path, test_path, threshold=0.01):
ref = np.loadtxt(ref_path)
test = np.loadtxt(test_path)
# 1. 長度對齊(重採樣或截斷)
if len(ref) != len(test):
# 簡單線性插值對齊
test = np.interp(np.linspace(0, 1, len(ref)), np.linspace(0, 1, len(test)), test)
# 2. 多指標評估
mse = np.mean((ref - test) ** 2)
corr = np.corrcoef(ref, test)[0,1]
dtw_dist, _ = fastdtw(ref, test, dist=euclidean)
report = {
'mse': mse,
'correlation': corr,
'dtw_distance': dtw_dist,
'pass': mse < threshold and corr > 0.99
}
return report
單純比較方差不可靠。建議構建「MSE + 相關係數 + DTW」多維指標儀表盤,並設置業務相關閾值;必要時引入頻域特征。自動化測試應輸出定量報告而非單一布爾值,便於回歸分析。
標簽純文本的正確姿勢
C 語言主函數拼寫錯誤 mian 導致連接器報錯:entry point not found Oracle SQL 錯誤排查:to_char 與 group by 引發的「非單組分組函數」問題