使用python编写基于紫外-可见光谱的水质COD检测代码
时间: 2023-11-11 14:05:35 浏览: 246
水质COD检测需要用到紫外-可见光谱仪器,通过测量水样在不同波长下的吸光度来计算COD浓度。以下是一个基于Python的简单示例代码,需要先安装numpy、scipy和matplotlib库。
```python
import numpy as np
from scipy.signal import savgol_filter
import matplotlib.pyplot as plt
# 波长范围
wavelength = np.arange(200, 800, 2)
# 标准COD浓度
cod_concentration = np.array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
# 标准吸光度
absorbance = np.array([
[0.004, 0.047, 0.091, 0.134, 0.178, 0.221, 0.265, 0.308, 0.352, 0.395, 0.439],
[0.006, 0.071, 0.136, 0.201, 0.266, 0.331, 0.396, 0.461, 0.526, 0.591, 0.656],
[0.009, 0.095, 0.181, 0.266, 0.352, 0.438, 0.524, 0.609, 0.695, 0.781, 0.867],
[0.013, 0.126, 0.239, 0.352, 0.465, 0.578, 0.691, 0.804, 0.917, 1.030, 1.143],
[0.017, 0.157, 0.298, 0.439, 0.580, 0.721, 0.862, 1.003, 1.144, 1.285, 1.426],
[0.021, 0.189, 0.358, 0.527, 0.696, 0.865, 1.034, 1.203, 1.373, 1.542, 1.711],
[0.025, 0.220, 0.418, 0.616, 0.814, 1.012, 1.210, 1.408, 1.606, 1.804, 2.002],
[0.029, 0.252, 0.478, 0.705, 0.932, 1.159, 1.386, 1.613, 1.840, 2.067, 2.294],
[0.034, 0.283, 0.538, 0.794, 1.050, 1.306, 1.561, 1.817, 2.073, 2.329, 2.584],
[0.038, 0.315, 0.598, 0.883, 1.168, 1.453, 1.738, 2.023, 2.308, 2.593, 2.878],
[0.042, 0.346, 0.658, 0.972, 1.284, 1.597, 1.910, 2.223, 2.536, 2.849, 3.162]
])
# 平滑后的标准吸光度
smooth_absorbance = savgol_filter(absorbance, 5, 3)
# 画出标准曲线
plt.plot(wavelength, smooth_absorbance.T)
plt.xlabel('Wavelength (nm)')
plt.ylabel('Absorbance')
plt.legend(cod_concentration, title='COD Concentration (mg/L)')
plt.show()
# 测量样品吸光度
sample_absorbance = np.array([0.052, 0.573, 0.910, 1.272, 1.716, 2.147, 2.621, 3.056, 3.499, 3.942, 4.380])
# 计算COD浓度
cod = np.interp(sample_absorbance, smooth_absorbance.T, cod_concentration)
# 输出结果
print(cod)
```
这段代码使用了标准曲线法进行COD测量,首先准备了一组标准COD浓度和对应的吸光度数据,然后通过平滑处理得到平滑后的标准吸光度曲线。最后,使用np.interp函数将样品吸光度插值到标准曲线上,得到样品的COD浓度。
阅读全文