Files
2026-11-06 14:45:09 +08:00

112 lines
4.4 KiB
C++

#ifndef PEAKFINDER_H
#define PEAKFINDER_H
#include <QObject>
#include <QVector>
#include <QString>
#include <QList>
#include <cmath>
#include <algorithm>
class PeakFinder : public QObject
{
Q_OBJECT
public:
struct Options {
double ToleranceNm; // 允许的波长误差 nm
bool UseGaussianFit; // 是否使用高斯拟合
int SmoothingWindow; // 滤波窗口大小
double MinProminence; // 最小突起高度
int MinWidthPixels; // 最小宽度像素
bool UseSecondDerivative; // 是否使用二阶导数
double MinCurvature; // 最小曲率
double HalfMaxFraction; // 半最大高度比例
double MaxSkewRatio; // 最大偏斜比
// 添加构造函数来提供默认值
Options() :
ToleranceNm(0.5),
UseGaussianFit(false),
SmoothingWindow(5),
MinProminence(0),
MinWidthPixels(2),
UseSecondDerivative(true),
MinCurvature(0),
HalfMaxFraction(0.5),
MaxSkewRatio(6.0)
{}
};
// 元素目标
struct ElementTarget {
QString name;
QVector<double> characteristicWavelengthsNm;
};
// 峰值信息
struct PeakInfo {
double wavelengthNm = 0;
double intensity = 0;
double fwhmNm = 0;
double area = 0;
QString elementName;
double targetWavelengthNm = 0;
bool matched = false;
};
// 峰值检测结果
struct PeakDetectionResult {
QList<PeakInfo> peaks;
QVector<QString> warnings;
};
// 检测峰值
static PeakDetectionResult DetectPeaks(
const QVector<float>& wavelengthsNm,
const QVector<float>& intensities,
const QVector<ElementTarget>& targets,
const Options& options = Options()
);
// 计算信噪比
static double computeSNR(const QVector<double>& y, int peakStart, int peakEnd,
int bgStart, int bgEnd, double epsilon = 1e-9);
private:
// 辅助函数, 用于计算基线修正
static QVector<float> baselineCorrect(const QVector<float>& y, int window);
// 辅助函数, 用于计算移动平均
static QVector<float> movingAverage(const QVector<float>& y, int w);
// 辅助函数, 用于计算二阶导数
static QVector<float> secondDerivative(const QVector<float>& y);
// 辅助函数, 用于查找局部极大值
static QList<PeakInfo> findLocalMaxima(const QVector<float>& w, const QVector<float>& y,
const QVector<float>& y2, QPair<int, int> window,
const Options& options);
// 辅助函数, 用于插值计算峰值位置
static double interpolateX(const QVector<float>& w, int i, double delta);
// 辅助函数, 用于插值计算峰值高度
static double interpolateY(const QVector<float>& y, int i, double delta);
// 辅助函数, 用于扫描半宽度左侧
static double scanHalfWidthLeft(const QVector<float>& w, const QVector<float>& y,
int iPeak, double half);
// 辅助函数, 用于扫描半宽度右侧
static double scanHalfWidthRight(const QVector<float>& w, const QVector<float>& y,
int iPeak, double half);
// 辅助函数, 用于扫描局部谷值左侧
static float scanLocalValleyLeft(const QVector<float>& y, int iPeak);
// 辅助函数, 用于扫描局部谷值右侧
static float scanLocalValleyRight(const QVector<float>& y, int iPeak);
// 辅助函数, 用于计算像素数
static int countLeftPixelsAbove(const QVector<float>& y, int iPeak, double thr);
// 辅助函数, 用于计算像素数
static int countRightPixelsAbove(const QVector<float>& y, int iPeak, double thr);
// 辅助函数, 用于计算峰面积
static double integrateAroundPeak(const QVector<float>& w, const QVector<float>& y,
int iPeak, double threshold);
// 辅助函数, 用于查找数组中的下界索引
static int lowerBound(const QVector<float>& arr, float value);
// 辅助函数, 用于查找数组中的上界索引
static int upperBound(const QVector<float>& arr, float value);
// 辅助函数, 用于计算数组的中位数
static double median(QVector<double> values);
};
#endif // PEAKFINDER_H