用python写一个光谱平滑函数
时间: 2023-03-31 08:02:41 浏览: 62
您好,我可以回答这个问题。以下是一个用 Python 编写的光谱平滑函数的示例代码:
```python
import numpy as np
from scipy.signal import savgol_filter
def smooth_spectrum(wavelengths, fluxes, window_size=11, poly_order=3):
"""
Smooths a spectrum using the Savitzky-Golay filter.
Parameters:
wavelengths (array-like): The wavelengths of the spectrum.
fluxes (array-like): The fluxes of the spectrum.
window_size (int): The size of the smoothing window.
poly_order (int): The order of the polynomial used in the smoothing.
Returns:
The smoothed fluxes.
"""
smoothed_fluxes = savgol_filter(fluxes, window_size, poly_order)
return smoothed_fluxes
```
这个函数使用了 SciPy 库中的 `savgol_filter` 函数来进行光谱平滑。您可以将光谱的波长和通量作为输入参数,并可以选择平滑窗口的大小和多项式拟合的阶数。函数返回平滑后的通量数组。