使用 Python 去除拉曼光谱中的尖峰:一步步指南
原文towardsdatascience.com/removing-spikes-from-raman-spectra-a-step-by-step-guide-with-python-b6fd90e8ea77?sourcecollection_archive---------4-----------------------#2024-11-06查找、移除并用插值值替换尖峰https://medium.com/nicopez?sourcepost_page---byline--b6fd90e8ea77--------------------------------https://towardsdatascience.com/?sourcepost_page---byline--b6fd90e8ea77-------------------------------- 尼古拉斯·可卡博士·发表于Towards Data Science ·7 分钟阅读·2024 年 11 月 6 日–本教程是系列文章的一部分系列文章题为使用 Python 进行拉曼光谱的数据科学该系列在Towards Data Science上发布。它基于在《Analytica Chimica Acta》期刊上发布的这篇文章。通过跟随本教程您将为您的数据分析工具包添加一项宝贵工具——一种已经在公开研究中使用的有效方法用于清理拉曼光谱。https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/7b4c0ebe0c2398f9f9dbcf05ecffd68a.png去除石墨烯拉曼光谱中的尖峰。图片由作者提供。简介尖峰去除是拉曼数据预处理中的一个重要步骤。尖峰是由宇宙射线撞击探测器引起的它们表现为强烈、狭窄的峰值可能会扭曲分析结果。这些能量爆发撞击电荷耦合设备CCD摄像头产生尖锐、高强度的峰值如果不加以修正可能会干扰进一步的处理步骤如归一化、光谱搜索或多元数据分析。因此清除这些伪影是一个优先事项。本教程将介绍一种实用的算法用于去除拉曼光谱中的尖峰。我们将使用 Python逐步演示一种用户友好、可定制的尖峰检测与修正方法确保拉曼数据的准确性和可靠性。图 1 展示了一个含有峰值的石墨烯拉曼光谱示例。石墨烯的卓越物理特性——如其电导率和热导率——使其成为一个备受研究的材料。其拉曼光谱包含反映结构特征的峰值揭示有关掺杂、应变和晶界的信息。因此拉曼光谱技术广泛用于表征石墨烯。然而为了充分利用这一工具必须事先去除峰值。https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/0de7cbb79612e8ba4c036fcb4f2ef06f.png图 1. 含峰值的石墨烯拉曼光谱。生成此图的代码如下所示。图片来自作者。importnumpyasnp# Load data directly into a numpy arraydatanp.loadtxt(spiked_spectrum.asc,delimiter,,skiprows1)# Extract Raman shift from the first column (index)ramanshiftdata[:,0]# Extract intensity from the second column (index 1in Python)intensitydata[:,1]# Plot the dataimportmatplotlib.pyplotasplt figplt.figure(figsize(5,3))plt.plot(ramanshift,intensity)plt.xlabel(Raman shift (cm$^{-1}$))plt.ylabel(Intensity (a.u.))plt.show()峰值移除算法这里提出的峰值移除算法包含四个主要步骤1. 峰值寻找2. 峰值检测3. 峰值标记4. 光谱校正让我们来看一下包含 Python 代码片段的不同步骤1. 峰值寻找首先算法通过检查局部最大值设置最小突出度阈值来识别显著的峰值。添加突出度阈值有助于排除由噪声生成的小峰值因为我们并不打算修正所有噪声。请参见下图进行对比。fromscipy.signalimportfind_peaks# Find the peaks in the spectrum (with and without prominence threshold)peaks_wo_p,_find_peaks(intensity)# Peaks found without a prominence thresholdpeaks_w_p,_find_peaks(intensity,prominence20)# Peaks found without a prominence thresholdfig,axplt.subplots(1,2,figsize(10,3))ax[0].plot(ramanshift,intensity,zorder0,labelRaw spectrum)ax[0].scatter(ramanshift[peaks_wo_p],intensity[peaks_wo_p],marker.,colorred,labelFound peaks)ax[1].plot(ramanshift,intensity,zorder0,labelRaw spectrum)ax[1].scatter(ramanshift[peaks_w_p],intensity[peaks_w_p],marker.,colorred,labelFound peaks)plt.show()https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/df05fdd0e60900a17df9f01be6273e5a.png第 1 步在光谱中找到峰值。图片来自作者。2. 峰值检测接下来根据峰值的特征窄宽度进行标记。这一点可能有助于大型光谱数据集的自动化。如果我们知道光谱中存在的拉曼带的宽度我们可以选择低于该值的阈值。例如在我们的系统分辨率下我们不预期出现宽度小于 10 cm-1 的石墨烯拉曼带。fromscipy.signalimportpeak_widths widthspeak_widths(intensity,peaks_w_p)[0]fig,axplt.subplots(figsize(5,3))ax.plot(ramanshift,intensity,zorder0,labelRaw spectrum)ax2ax.twinx()ax2.scatter(ramanshift[peaks_w_p],widths,marker,colorred,labelPeak widths)plt.show()https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/2ac54ea631976e354b792ba9f176db79.png第 2 步寻找峰值的宽度。图片来自作者。3. 峰值标记接下来任何受峰值影响的数据点都会被标记标记范围是根据峰值的突出程度计算的从而有效地隔离损坏的像素。换句话说我们选择必须修正的窗口。# Lets set the parameters:width_param_rel0.8width_threshold10# Estimation of the width of the narrowest Raman band# Calculation of the range where the spectral points are asumed to be corruptedwidths_ext_apeak_widths(intensity,peaks_w_p,rel_heightwidth_param_rel)[2]widths_ext_bpeak_widths(intensity,peaks_w_p,rel_heightwidth_param_rel)[3]# Create a vector where spikes will be flag: no spike 0, spike 1.spikesnp.zeros(len(intensity))# Flagging the area previously defined if the peak is considered a spike (width below width_threshold)fora,width,ext_a,ext_binzip(range(len(widths)),widths,widths_ext_a,widths_ext_b):ifwidthwidth_threshold:spikes[int(ext_a)-1:int(ext_b)2]1figplt.figure(figsize(5,3))plt.plot(ramanshift,intensity,zorder0,labelRaw spectrum)a1plt.scatter(ramanshift[int(widths_ext_a[a])-1:int(widths_ext_b[a])1],intensity[int(widths_ext_a[a])-1:int(widths_ext_b[a])1],colorred,labelcorrupted points)plt.axvline(xramanshift[int(widths_ext_a[a])-1],linestyle--,colorred)plt.axvline(xramanshift[int(widths_ext_b[a])1],linestyle--,colorred)plt.show()https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/6ca5b8f53884b5222fac8b105f5c4608.png第 3 步标记损坏的箱体。图片来自作者。4. 光谱校正最后这些点通过插值方法与邻近值进行修正保持光谱的完整性以便进行后续分析。fromscipyimportinterpolate# Lets set the parameter:moving_average_window10intensity_outintensity.copy()# Interpolation of corrupted pointsfori,spikeinenumerate(spikes):ifspike!0:# If we have an spike in position iwindownp.arange(i-moving_average_window,imoving_average_window1)# we select 2 ma 1 points around our spikewindow_exclude_spikeswindow[spikes[window]0]# From such interval, we choose the ones which are not spikesinterpolatorinterpolate.interp1d(window_exclude_spikes,intensity[window_exclude_spikes],kindlinear)# We use the not corrupted points around the spike to calculate the interpolationintensity_out[i]interpolator(i)# The corrupted point is exchanged by the interpolated value.figplt.figure(figsize(5,3))plt.plot(ramanshift,intensity,zorder0,colorred,labelRaw spectrum)plt.plot(ramanshift,intensity_out,zorder0,labelCorrected spectrum)plt.show()https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/1a41d905031018d2af7adab04d555510.png第 4 步光谱校正。图片来自作者。Python 中的完整峰值移除功能所有这些代码片段可以汇总成一个单独的函数。该函数被设计为根据特定的数据需求进行定制具有调整突出度和宽度的参数importnumpyasnpfromscipy.signalimportfind_peaks,peak_widths,peak_prominencesfromscipyimportinterpolatedefspike_removal(y,width_threshold,prominence_thresholdNone,moving_average_window10,width_param_rel0.8,interp_typelinear): Detects and replaces spikes in the input spectrum with interpolated values. Algorithm first published by N. Coca-Lopez in Analytica Chimica Acta. https://doi.org/10.1016/j.aca.2024.342312 Parameters: y (numpy.ndarray): Input spectrum intensity. width_threshold (float): Threshold for peak width. prominence_threshold (float): Threshold for peak prominence. moving_average_window (int): Number of points in moving average window. width_param_rel (float): Relative height parameter for peak width. tipo: type of interpolation (linear, quadratic, cubic) Returns: numpy.ndarray: Signal with spikes replaced by interpolated values. # First, we find all peaks showing a prominence above prominence_threshold on the spectrapeaks,_find_peaks(y,prominenceprominence_threshold)# Create a vector where spikes will be flag: no spike 0, spike 1.spikesnp.zeros(len(y))# Calculation of the widths of the found peakswidthspeak_widths(y,peaks)[0]# Calculation of the range where the spectral points are asumed to be corruptedwidths_ext_apeak_widths(y,peaks,rel_heightwidth_param_rel)[2]widths_ext_bpeak_widths(y,peaks,rel_heightwidth_param_rel)[3]# Flagging the area previously defined if the peak is considered a spike (width below width_threshold)fora,width,ext_a,ext_binzip(range(len(widths)),widths,widths_ext_a,widths_ext_b):ifwidthwidth_threshold:spikes[int(ext_a)-1:int(ext_b)2]1y_outy.copy()# Interpolation of corrupted pointsfori,spikeinenumerate(spikes):ifspike!0:# If we have an spike in position iwindownp.arange(i-moving_average_window,imoving_average_window1)# we select 2 ma 1 points around our spikewindow_exclude_spikeswindow[spikes[window]0]# From such interval, we choose the ones which are not spikesinterpolatorinterpolate.interp1d(window_exclude_spikes,y[window_exclude_spikes],kindinterp_type)# We use the not corrupted points around the spike to calculate the interpolationy_out[i]interpolator(i)# The corrupted point is exchanged by the interpolated value.returny_out然后带有该算法的函数可以应用于带有峰值的石墨烯光谱如下所示intensity_despikedspike_removal(intensity,width_threshold3,prominence_threshold20,moving_average_window10,width_param_rel0.8,interp_typelinear)fig,axplt.subplots(1,2,figsize(2*5,3))ax[0].plot(ramanshift,intensity,labelspike,colorred,linewidth0.9)ax[0].plot(ramanshift,intensity_despiked)ax[1].plot(ramanshift,intensity_despiked)plt.show()https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/f9d4520f321b03ec94650f47c718108d.png拉曼光谱中峰值移除的示例。图片来自作者。使用这种去除尖峰的方法你可以确保你的拉曼光谱干净可靠最小化伪影同时不丢失重要的光谱细节。该方法非常适合自动化尤其是在已知预期的最小峰宽的情况下使其非常适合大规模光谱数据集和高通量分析。希望你喜欢这个教程。如果有任何问题或者想分享你自己的拉曼数据挑战欢迎在评论中留言——我很想听听这个算法如何帮助你的项目准备好尝试一下吗你可以在这里下载 Jupyter Notebook。如果你觉得这对你有帮助请记得引用原始工作这对我会有很大帮助

相关新闻

Flutter 三方库 dart_jts 的鸿蒙化适配指南 - 掌控地理空间资产、拓扑计算实战、鸿蒙级精密 GIS 专家

Flutter 三方库 dart_jts 的鸿蒙化适配指南 - 掌控地理空间资产、拓扑计算实战、鸿蒙级精密 GIS 专家

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net Flutter 三方库 dart_jts 的鸿蒙化适配指南 - 掌控地理空间资产、拓扑计算实战、鸿蒙级精密 GIS 专家 在鸿蒙跨平台应用执行高级地理信息管理与多维空间拓扑资产指控(如构建一个…

2026/7/4 14:07:29 阅读更多 →
SpringBoot+Vue 养老院管理系统管理平台源码【适合毕设/课设/学习】Java+MySQL

SpringBoot+Vue 养老院管理系统管理平台源码【适合毕设/课设/学习】Java+MySQL

摘要 随着人口老龄化问题日益严峻,养老院管理系统的需求逐渐增加。传统的人工管理方式效率低下,难以满足现代养老院对信息化、智能化的要求。养老院管理系统通过数字化手段优化资源配置,提升服务质量,为老人提供更安全、便捷的生活…

2026/7/3 3:11:25 阅读更多 →
Flutter 三方库 serverpod_cloud_storage_s3 的鸿蒙化适配指南 - 掌控云端存储资产、精密 S3 治理实战、鸿蒙级分布式专家

Flutter 三方库 serverpod_cloud_storage_s3 的鸿蒙化适配指南 - 掌控云端存储资产、精密 S3 治理实战、鸿蒙级分布式专家

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.csdn.net Flutter 三方库 serverpod_cloud_storage_s3 的鸿蒙化适配指南 - 掌控云端存储资产、精密 S3 治理实战、鸿蒙级分布式专家 在鸿蒙跨平台应用执行高级云端资源管理与多维 S3 存储资产指控&a…

2026/7/3 3:37:52 阅读更多 →

最新新闻

大模型指纹识别技术:原理、攻防与实战应用

大模型指纹识别技术:原理、攻防与实战应用

1. 项目概述:当大模型学会“签名”,我们如何识别与应对? 最近在跟几个做AI安全的朋友聊天,大家不约而同地提到了一个词:“LLM指纹识别”。这听起来有点玄乎,指纹不是人的生物特征吗,怎么大语言模…

2026/7/4 16:38:50 阅读更多 →
AI冲击下数据岗位重构:国际人才策略与能力原子化实践

AI冲击下数据岗位重构:国际人才策略与能力原子化实践

1. 项目概述:这不是一份“就业报告”,而是一份人才迁徙路线图“2025年美国数据岗位市场”——光看标题,你可能以为这又是一份堆砌招聘平台统计数字、罗列热门职位名称的常规行业简报。但实际不是。我连续三年深度参与硅谷、纽约、奥斯汀三地的…

2026/7/4 16:36:50 阅读更多 →
STM32与MC6470 IMU的硬件协同与运动控制优化

STM32与MC6470 IMU的硬件协同与运动控制优化

1. MC6470与STM32L4S5ZI的硬件协同架构解析MC6470作为一款六轴惯性测量单元(IMU),其核心价值在于将三轴加速度计和三轴陀螺仪集成在单芯片方案中。在实际项目中,我测量到其加速度计量程可达16g,角速度测量范围达到2000dps,这对于大…

2026/7/4 16:34:49 阅读更多 →
XWiki路径遍历漏洞CVE-2025-55747复现与深度解析

XWiki路径遍历漏洞CVE-2025-55747复现与深度解析

1. 项目概述与漏洞背景 最近在梳理一些开源项目的安全公告时,XWiki的一个路径遍历漏洞(CVE-2025-55747)引起了我的注意。这个漏洞编号看着新鲜,但本质上又是一个经典的“输入验证不严”导致的安全问题。简单来说,攻击者…

2026/7/4 16:30:48 阅读更多 →
SpringBoot+Vue家政平台毕设实战:从工程化思维到生产级实现

SpringBoot+Vue家政平台毕设实战:从工程化思维到生产级实现

🚀 30款热门AI模型一站整合,DeepSeek/GLM/Claude 随心用,限时 5 折。 👉 点击领海量免费额度 你有没有过这样的经历:毕业设计选题时,面对“家政服务平台”这类看似普通的题目,感觉无从下手&a…

2026/7/4 16:30:48 阅读更多 →
PC微信小程序V1MMWX加密包逆向解析:AES+XOR双重加密原理与Python解密实战

PC微信小程序V1MMWX加密包逆向解析:AES+XOR双重加密原理与Python解密实战

1. 项目概述:为什么我们需要关注PC微信小程序的加密包?如果你是一名前端开发者、安全研究员,或者单纯对微信小程序的技术实现感到好奇,那么你很可能已经发现,直接从PC端微信获取到的小程序包(.wxapkg文件&a…

2026/7/4 16:30:48 阅读更多 →

日新闻

Memcached 1.6.43 发布:关键安全修复版本,多项问题得到解决

Memcached 1.6.43 发布:关键安全修复版本,多项问题得到解决

Memcached 1.6.43 正式发布,这是一个关键的安全修复版本,修复了多个方面的问题,还对部分功能进行了优化。 安全修复亮点 此次发布在安全修复上表现突出。binprot 避免了项目引用计数溢出,mcmc 因安全问题提升了上游版本号&#xf…

2026/7/4 0:04:29 阅读更多 →
终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案

终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案

终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案 【免费下载链接】HMCL A Minecraft Launcher which is multi-functional, cross-platform and popular 项目地址: https://gitcode.com/gh_mirrors/hm/HMCL HMCL(Hello Minecraft! Lau…

2026/7/4 0:06:29 阅读更多 →
KMX63与PIC18F66K40在嵌入式HMI中的硬件协同与低功耗设计

KMX63与PIC18F66K40在嵌入式HMI中的硬件协同与低功耗设计

1. KMX63与PIC18F66K40的硬件协同架构解析KMX63作为一款三轴加速度计和磁力计组合传感器,与PIC18F66K40微控制器的搭配堪称嵌入式HMI开发的黄金组合。这套硬件组合的核心优势在于KMX63提供的高精度运动感知能力与PIC18F66K40强大的信号处理能力形成了完美互补。KMX6…

2026/7/4 0:06:29 阅读更多 →

周新闻

月新闻