{"id":1131504,"student_id":90858,"content":"import numpy as np\r\nfrom scipy.optimize import curve_fit\r\nimport matplotlib.pyplot as plt\r\n\r\n# 1. 定義您的插入損耗模型函數\r\n# The function takes the independent variable (frequency) and all the parameters to fit.\r\ndef il_model(f, a, b, c, d, e):\r\n \"\"\"\r\n Semi-empirical model for insertion loss fitting.\r\n IL(f) = a + b*f^0.5 + c*f + d*f^1.5 + e*f^2\r\n \"\"\"\r\n # 確保頻率為正數,避免 sqrt 產生複數\r\n f = np.abs(f) \r\n return a + b * np.sqrt(f) + c * f + d * np.power(f, 1.5) + e * np.power(f, 2.0)\r\n\r\n# 2. 準備您的測量數據 (這裡我們生成一些模擬數據)\r\n# 在實際應用中,您會從 VNA 或模擬軟體中導入這些數據。\r\n# 頻率單位:GHz\r\nf_data = np.linspace(0.1, 20, 100) # 從 0.1 GHz 到 20 GHz 的 100 個點\r\n\r\n# 假設的真實係數 (用於生成數據)\r\ntrue_a, true_b, true_c, true_d, true_e = 0.05, 0.8, 0.1, 0.001, 0.0005\r\n\r\n# 生成 \"測量\" 的插入損耗數據 (單位:dB,通常為負值,這裡用正值計算)\r\nil_measured = il_model(f_data, true_a, true_b, true_c, true_d, true_e)\r\n\r\n# 加入一些隨機噪聲,模擬真實世界的測量\r\nnoise = 0.05 * np.random.normal(size=f_data.size)\r\nil_measured_noisy = il_measured + noise\r\n\r\n# 3. 執行曲線擬合 (Curve Fitting)\r\n# curve_fit 會找到最佳的 a, b, c, d, e 值\r\n# popt: 包含找到的最佳係數的數組\r\n# pcov: 最佳參數的協方差矩陣\r\ninitial_guesses = [0, 0, 0, 0, 0] # 初始猜測值,可以幫助算法收斂\r\npopt, pcov = curve_fit(il_model, f_data, il_measured_noisy, p0=initial_guesses)\r\n\r\n# 4. 提取並顯示結果\r\nfit_a, fit_b, fit_c, fit_d, fit_e = popt\r\nprint(\"擬合得到的係數:\")\r\nprint(f\"a = {fit_a:.6f}\")\r\nprint(f\"b = {fit_b:.6f}\")\r\nprint(f\"c = {fit_c:.6f}\")\r\nprint(f\"d = {fit_d:.6f}\")\r\nprint(f\"e = {fit_e:.6f}\")\r\n\r\n# 5. 繪圖驗證擬合效果\r\nplt.figure(figsize=(10, 6))\r\n# 繪製帶有噪聲的測量數據點\r\nplt.scatter(f_data, il_measured_noisy, label='Measured Data', s=10, c='red')\r\n# 繪製使用擬合係數得到的曲線\r\nplt.plot(f_data, il_model(f_data, *popt), label='Fitted Curve', linewidth=2)\r\nplt.xlabel('Frequency (GHz)')\r\nplt.ylabel('Insertion Loss (dB)')\r\nplt.title('Insertion Loss Fitting')\r\nplt.legend()\r\nplt.grid(True)\r\nplt.show()\r\n","created_at":"2025-09-30T19:44:57.716+08:00","updated_at":"2025-09-30T19:46:44.850+08:00","name":"clark","language":"python","screenshot":{"url":"https://cdn8.koding.school/uploads/project/screenshot/1131504/1a8751143ccad470d05353353d0c5c28.jpg"},"parent_id":4,"plugin":"","description":null,"note":null,"status":"public","like_student_ids":[],"is_featured":false,"views":1,"hashid":"rdvs3wv2v","is_content_changed":false,"review_status":"unsubmitted","submitted_at":null,"reviewed_at":null,"advise":null,"is_deleted":false}
[{"id":41615955,"file_name":"cloud.png","project_id":1131504,"asset_id":302343,"created_at":"2025-09-30T19:44:57.722+08:00","updated_at":"2025-09-30T19:44:57.722+08:00"},{"id":41615956,"file_name":"bg.jpg","project_id":1131504,"asset_id":302344,"created_at":"2025-09-30T19:44:57.724+08:00","updated_at":"2025-09-30T19:44:57.724+08:00"},{"id":41615957,"file_name":"calm.mp3","project_id":1131504,"asset_id":302345,"created_at":"2025-09-30T19:44:57.725+08:00","updated_at":"2025-09-30T19:44:57.725+08:00"},{"id":41615958,"file_name":"koding.png","project_id":1131504,"asset_id":302346,"created_at":"2025-09-30T19:44:57.726+08:00","updated_at":"2025-09-30T19:44:57.726+08:00"}]
橘蘋學習平台
我的作品
檢視專案頁
匯出
複製
匯入
刪除
截圖
繁中
简中
English
日本語
用手機掃描下方 QRCode 進行安裝
或您也可以
下載 APK
到這台電腦