Files
SiC-2nd-paper/画图/拉曼结果拟合/250923/fitting_raman_tensor.ipynb
2025-09-29 14:48:11 +08:00

240 lines
7.2 KiBLFS
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "22bc2a27",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/home/chn/repo/SiC-2nd-paper'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"from scipy.optimize import curve_fit\n",
"import matplotlib.pyplot as plt\n",
"import plotly.graph_objects as go\n",
"import matplotlib\n",
"\n",
"def lorentzian(x, A, x0, gamma, k, b):\n",
" return A / (1 + ((x - x0) / gamma)**2) + k * x + b\n",
"def double_lorentzian(x, A1, x1, gamma1, A2, x2, gamma2, k, b):\n",
" return lorentzian(x, A1, x1, gamma1, k, b) + lorentzian(x, A2, x2, gamma2, 0, 0)\n",
"\n",
"%pwd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f4aba80e",
"metadata": {},
"outputs": [],
"source": [
"def fitting_peek(x, y, x_range, function, guess, bound):\n",
" mask = (x > x_range[0]) & (x < x_range[1])\n",
" x = x[mask]\n",
" y = y[mask]\n",
" popt, pcov = curve_fit(function, x, y, p0=guess, maxfev=10000, bounds=bound)\n",
" return popt\n",
"\n",
"def fitting_line(x, y):\n",
" integration = []\n",
"\n",
" result = fitting_peek(x, y, fitting_range[0], double_lorentzian, fitting_init_parameter[0], fitting_bound[0])\n",
" integration.append(result[0] * result[2] * np.pi)\n",
" integration.append(result[3] * result[5] * np.pi)\n",
"\n",
" result = fitting_peek(x, y, fitting_range[2], lorentzian, fitting_init_parameter[2], fitting_bound[2])\n",
" integration.append(result[0] * result[2] * np.pi)\n",
"\n",
" result = fitting_peek(x, y, fitting_range[3], double_lorentzian, fitting_init_parameter[3], fitting_bound[3])\n",
" integration.append(result[0] * result[2] * np.pi)\n",
"\n",
" return integration"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "591e7dc9",
"metadata": {},
"outputs": [],
"source": [
"fitting_init_parameter = [\n",
" [300, 196, 1, 2e3, 204, 0.8, 0, 900],\n",
" [50, 266, 5, 0, 100],\n",
" [1e3, 611, 5, 0, 100],\n",
" [1e4, 776, 1, 1e3, 797, 1, 1, 100]\n",
"]\n",
"fitting_range = [\n",
" [180, 220],\n",
" [250, 280],\n",
" [600, 617],\n",
" [700, 850]\n",
"]\n",
"fitting_bound = [\n",
" ([0, 192, 0, 0, 200, 0, -np.inf, -np.inf], [np.inf, 198, np.inf, np.inf, 206, np.inf, np.inf, np.inf]),\n",
" ([0, 260, 0, -np.inf, -np.inf], [np.inf, 270, np.inf, np.inf, np.inf]),\n",
" ([0, 605, 0, -np.inf, -np.inf], [np.inf, 617, np.inf, np.inf, np.inf]),\n",
" (-np.inf, np.inf)\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad979c50",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11\n"
]
}
],
"source": [
"x = np.loadtxt('画图/拉曼结果拟合/250923/1/joined_x.txt')\n",
"y = np.loadtxt('画图/拉曼结果拟合/250923/1/joined_y.txt')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ea7b0b57",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/tmp/ipykernel_3319242/3274484967.py:8: RuntimeWarning:\n",
"\n",
"invalid value encountered in log10\n",
"\n"
]
}
],
"source": [
"# 检查拟合结果\n",
"for site in range(0, 11):\n",
" this_y = y[site]\n",
" print(site)\n",
" result = fitting_peek(x, this_y, fitting_range[2], lorentzian, fitting_init_parameter[2], fitting_bound[2])\n",
" fig = go.Figure()\n",
" fig.add_trace(go.Scatter(x=x, y=np.log10(this_y), mode='lines', name='data'))\n",
" fig.add_trace(go.Scatter(x=x, y=np.log10(lorentzian(x, *result)), mode='lines', name='fit'))\n",
" fig.update_yaxes(range=[1, 4])\n",
" # fig.show()\n",
" # print(result)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e3526b25",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 [np.float64(768.3628596891868), np.float64(5431.55142444282), np.float64(1649.6765906115854), np.float64(64818.59500905687)]\n",
"1 [np.float64(813.1707839257367), np.float64(5555.9820669289875), np.float64(1640.6767570509644), np.float64(64301.12567249636)]\n",
"2 [np.float64(795.4849535737138), np.float64(5635.418903819261), np.float64(1684.8825141737777), np.float64(65135.00002971351)]\n",
"3 [np.float64(779.0372610900633), np.float64(5509.50735172907), np.float64(1679.8558668900923), np.float64(64605.42606535031)]\n",
"4 [np.float64(775.1571577873912), np.float64(5344.159324715969), np.float64(1634.4576445501627), np.float64(63123.40991037276)]\n",
"5 [np.float64(761.2919095984198), np.float64(5393.470206043833), np.float64(1606.6853044260413), np.float64(62467.602569144474)]\n",
"6 [np.float64(787.0969662159204), np.float64(5438.93090716631), np.float64(1586.4586462683103), np.float64(62183.00568994049)]\n",
"7 [np.float64(803.6129171683904), np.float64(5375.814923501621), np.float64(1544.6877252591216), np.float64(60945.148104520624)]\n",
"8 [np.float64(800.4246980153744), np.float64(5456.326920952701), np.float64(1559.35014385633), np.float64(61406.333195373314)]\n",
"9 [np.float64(827.0751964444557), np.float64(5643.83181448829), np.float64(1627.704728903094), np.float64(62847.47803526372)]\n",
"10 [np.float64(810.5939643362409), np.float64(5517.849682319155), np.float64(1605.610706529614), np.float64(62658.68174596619)]\n"
]
}
],
"source": [
"# 收集拟合结果\n",
"integration_all = []\n",
"for site in range(0, 11):\n",
" this_y = y[site]\n",
" integration = fitting_line(x, this_y)\n",
" integration_all.append(integration)\n",
" print(site, integration)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c49bc98d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"mean: [ 792.84624253 5482.07668419 1620.00423896 63135.61872975]\n",
"std: [ 19.56751666 95.54341758 42.883329 1341.69583981]\n"
]
}
],
"source": [
"# 计算峰面积的平均和标准差\n",
"integration_all = np.array(integration_all)\n",
"mean = np.mean(integration_all, axis=0)\n",
"std = np.std(integration_all, axis=0)\n",
"print(\"mean:\", mean)\n",
"print(\"std:\", std)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}