添加项目文件。
This commit is contained in:
@@ -0,0 +1,460 @@
|
||||
#include "algorithmanalysis.h"
|
||||
#include <cmath>
|
||||
#include <QBuffer>
|
||||
#include <fstream>
|
||||
#include <QDebug>
|
||||
AlgorithmAnalysis::AlgorithmAnalysis()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//线性拟合元素集合
|
||||
vector<SpectrumElement> AlgorithmAnalysis::elements = {
|
||||
{"Cu", 324.75},
|
||||
{"Sn", 284.00},
|
||||
{"Mg", 279.52},
|
||||
{"Ca", 317.96},
|
||||
{"Cr", 357.87},
|
||||
{"Fe", 302.05},
|
||||
{"Si", 288.15},
|
||||
{"Ti", 334.94},
|
||||
// 这里可以添加更多的元素和对应的波长
|
||||
};
|
||||
|
||||
//求光谱强度的平均值
|
||||
vector<double> AlgorithmAnalysis::average(const vector<vector<double>> &multiLst )
|
||||
{
|
||||
|
||||
// 计算每列的平均值
|
||||
vector<double> averageSpecLst;
|
||||
for (int i = 0; i < multiLst[0].size(); ++i) {
|
||||
double columnSum = 0;
|
||||
|
||||
for (int j = 0; j < multiLst.size(); ++j) {
|
||||
columnSum += multiLst[j][i];
|
||||
|
||||
}
|
||||
double columnAverage = columnSum / multiLst.size(); //输出平均后的一行,如果读取不到有效值那就输出“,”
|
||||
|
||||
averageSpecLst.push_back(columnAverage);
|
||||
}
|
||||
return averageSpecLst;
|
||||
}
|
||||
|
||||
//求光谱强度的平均值
|
||||
vector<SpectrumChannelData> AlgorithmAnalysis::average(const vector<vector<SpectrumChannelData>> &multiLst )
|
||||
{
|
||||
// 计算每列的平均值
|
||||
vector<SpectrumChannelData> averageSpecLst;
|
||||
for (int i = 0; i < multiLst[0].size(); ++i) {
|
||||
double columnSum = 0;
|
||||
double wavelengthSum = 0;
|
||||
for (int j = 0; j < multiLst.size(); ++j) {
|
||||
columnSum += multiLst[j][i].intensity;
|
||||
wavelengthSum += multiLst[j][i].wavelength;
|
||||
}
|
||||
double columnAverage = columnSum / multiLst.size(); //输出平均后的一行,如果读取不到有效值那就输出“,”
|
||||
double waveLengthAverage = wavelengthSum / multiLst.size();
|
||||
averageSpecLst.push_back({waveLengthAverage,columnAverage});
|
||||
}
|
||||
return averageSpecLst;
|
||||
}
|
||||
|
||||
//百分位滤波去背景
|
||||
vector<double> AlgorithmAnalysis::background(vector<double> waveLst,vector<double> averageSpecLst) { //这个函数background用来去背景,是整个代码的核心部分
|
||||
|
||||
|
||||
vector<double> BackgroundLst(averageSpecLst.size(), 0);
|
||||
for (int i = 0; i < averageSpecLst.size() - 99; i++) { // 循环data1中的每个数据点,i表示data1中的起始位置
|
||||
vector<double> temp(averageSpecLst.begin() + i, averageSpecLst.begin() + i + 100); // 从data1中取出100个数据,存储到temp中
|
||||
sort(temp.begin(), temp.end()); // 对temp中的数据进行排序
|
||||
double bg = temp[10]; // 取排序后的第10个数据,即背景数据
|
||||
/*data2[i + 50] = bg;*/ // 将背景数据存储到data2中,存储位置为i+50,因为temp中的数据是从i开始的
|
||||
BackgroundLst[i] = bg;
|
||||
}
|
||||
for (int i = averageSpecLst.size() - 99; i < averageSpecLst.size(); i++) { // 循环data1中的每个数据点,i表示data1中的起始位置
|
||||
vector<double> temp(averageSpecLst.begin() + i - 100, averageSpecLst.begin() + i); // 从data1中取出100个数据,存储到temp中
|
||||
sort(temp.begin(), temp.end()); // 对temp中的数据进行排序
|
||||
double bg2 = temp[10]; // 取排序后的第10个数据,即背景数据
|
||||
/*data2[i + 50] = bg;*/ // 将背景数据存储到data2中,存储位置为i+50,因为temp中的数据是从i开始的
|
||||
BackgroundLst[i] = bg2;
|
||||
}
|
||||
vector<double> diff(averageSpecLst.size(), 0); // 定义一个双精度浮点数向量diff,大小与data1相同,所有元素初始化为0
|
||||
for (int i = 0; i < averageSpecLst.size(); i++) { // 循环data1中的每个数据点
|
||||
double val = averageSpecLst[i] - BackgroundLst[i]; // 计算原始数据和背景数据的差值
|
||||
if (val < 0) { // 如果差值小于0
|
||||
val = 0; // 置为0,因为强度不能为负数
|
||||
}
|
||||
diff[i]=val; // 将差值存储到diff向量中
|
||||
}
|
||||
|
||||
|
||||
return diff;
|
||||
|
||||
}
|
||||
|
||||
void AlgorithmAnalysis::background(vector<SpectrumChannelData> &channelData)
|
||||
{
|
||||
vector<double> temp;
|
||||
vector<double> backGroundLst(channelData.size(), 0.0);
|
||||
for (size_t i = 0; i < channelData.size(); i++) {
|
||||
temp.push_back(channelData[i].intensity);
|
||||
}
|
||||
for (size_t i = 0; i < temp.size() - 99; i++) { // 循环data1中的每个数据点,i表示data1中的起始位置
|
||||
vector<double> intensityDemo(temp.begin() + i, temp.begin() + i + 100); // 从data1中取出100个数据,存储到temp中
|
||||
|
||||
sort(intensityDemo.begin(), intensityDemo.end()); // 对temp中的数据进行排序
|
||||
double bg = intensityDemo[10]; // 取排序后的第10个数据,即背景数据
|
||||
backGroundLst[i] = bg;
|
||||
}
|
||||
for (size_t i = temp.size() - 99; i < temp.size(); i++) { // 循环data1中的每个数据点,i表示data1中的起始位置
|
||||
vector<double> intensityDemo(temp.begin() + i - 99, temp.begin() + i); // 从data1中取出100个数据,存储到temp中
|
||||
sort(intensityDemo.begin(), intensityDemo.end()); // 对temp中的数据进行排序
|
||||
double bg2 = intensityDemo[10]; // 取排序后的第10个数据,即背景数据
|
||||
backGroundLst[i] = bg2;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < channelData.size(); i++) { // 循环data1中的每个数据点
|
||||
double val = channelData[i].intensity - backGroundLst[i]; // 计算原始数据和背景数据的差值
|
||||
if (val < 0.0) { // 如果差值小于0
|
||||
val = 0.0; // 置为0,因为强度不能为负数
|
||||
}
|
||||
channelData[i].intensity = val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
//裁剪重叠的光谱数据
|
||||
vector<SpectrumChannelData> AlgorithmAnalysis::cutRepeatSpecture(vector<SpectrumChannelData> ospecV1,
|
||||
vector<SpectrumChannelData> ospecV2,
|
||||
vector<SpectrumChannelData> ospecV3,
|
||||
vector<SpectrumChannelData> ospecV4, int cutSize) {
|
||||
|
||||
// 裁剪掉cutSize个
|
||||
vector<SpectrumChannelData> dspecV1(ospecV1.begin(), ospecV1.end() - cutSize );
|
||||
vector<SpectrumChannelData> dspecV2(ospecV2.begin() + cutSize, ospecV2.end() - cutSize);
|
||||
vector<SpectrumChannelData> dspecV3(ospecV3.begin() + cutSize, ospecV3.end() - cutSize);
|
||||
vector<SpectrumChannelData> dspecV4(ospecV4.begin() + cutSize, ospecV4.end() - cutSize);
|
||||
vector<SpectrumChannelData> specV;
|
||||
// 找重叠的
|
||||
int flag1 = dspecV1.size();
|
||||
int flag11 = 0;
|
||||
int flag2 = dspecV2.size();
|
||||
int flag22 = 0;
|
||||
int flag3 = dspecV3.size();
|
||||
int flag33 = 0;
|
||||
//判断首个重叠的数据大小
|
||||
bool static1 = dspecV1.back().wavelength > dspecV2[0].wavelength;
|
||||
|
||||
if(static1){
|
||||
for (int i = 0; i < dspecV2.size(); i++) {
|
||||
if (dspecV1.back().wavelength < dspecV2[i].wavelength) {
|
||||
flag11 = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(dspecV2[flag11].intensity > dspecV1.back().intensity){
|
||||
for (int i = 0; i < dspecV1.size(); i++) {
|
||||
if (dspecV1[i].wavelength > dspecV2[0].wavelength) {
|
||||
flag11 = 0;
|
||||
flag1 = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
flag1 = dspecV1.size();
|
||||
flag11 = 0;
|
||||
}
|
||||
|
||||
bool static2 = dspecV2.back().wavelength > dspecV3[0].wavelength;
|
||||
|
||||
if(static2){
|
||||
for (int i = 0; i < dspecV3.size(); i++) {
|
||||
if (dspecV2.back().wavelength < dspecV3[i].wavelength) {
|
||||
flag22 = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(dspecV3[flag22].intensity > dspecV2.back().intensity){
|
||||
for (int i = 0; i < dspecV2.size(); i++) {
|
||||
if (dspecV2[i].wavelength > dspecV3[0].wavelength) {
|
||||
flag2 = i;
|
||||
flag22 = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
flag2 = dspecV2.size();
|
||||
flag22 = 0;
|
||||
}
|
||||
|
||||
bool static3 = dspecV3.back().wavelength > dspecV4[0].wavelength;
|
||||
|
||||
if(static3){
|
||||
for (int i = 0; i < dspecV4.size(); i++) {
|
||||
if (dspecV3.back().wavelength < dspecV4[i].wavelength) {
|
||||
flag33 = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(dspecV4[flag33].intensity > dspecV3.back().intensity){
|
||||
for (int i = 0; i < dspecV3.size(); i++) {
|
||||
if (dspecV3[i].wavelength > dspecV4[0].wavelength) {
|
||||
flag3 = i;
|
||||
flag33 = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
flag3 = dspecV3.size();
|
||||
flag33 = 0;
|
||||
}
|
||||
//进行通道拼接
|
||||
specV.insert(specV.end(), dspecV1.begin(), dspecV1.begin() + flag1);
|
||||
specV.insert(specV.end(), dspecV2.begin() + flag11, dspecV2.begin() + flag2);
|
||||
specV.insert(specV.end(), dspecV3.begin() + flag22, dspecV3.begin() + flag3);
|
||||
specV.insert(specV.end(), dspecV4.begin() + flag33, dspecV4.end());
|
||||
|
||||
return specV;
|
||||
}
|
||||
|
||||
vector<SpectrumChannelData> AlgorithmAnalysis::cutRepeatSpecture(vector<pair<int, vector<SpectrumChannelData>>> ospecV, int cutSize)
|
||||
{
|
||||
std::vector<std::vector<SpectrumChannelData>> dspecV;
|
||||
for (auto& specPair : ospecV) {
|
||||
std::vector<SpectrumChannelData>& spec = specPair.second;
|
||||
dspecV.push_back(std::vector<SpectrumChannelData>(spec.begin() + cutSize, spec.end() - cutSize));
|
||||
}
|
||||
std::vector<SpectrumChannelData> specV;
|
||||
size_t channelNextStart = 0;
|
||||
for (size_t i = 0; i < dspecV.size() - 1; ++i) {
|
||||
std::vector<SpectrumChannelData>& currentSpec = dspecV[i];
|
||||
std::vector<SpectrumChannelData>& nextSpec = dspecV[i + 1];
|
||||
size_t currentEnd = currentSpec.size();
|
||||
size_t nextStart = 0;
|
||||
bool overlap = currentSpec.back().wavelength > nextSpec.front().wavelength;
|
||||
|
||||
if (overlap) {
|
||||
for (size_t j = 0; j < nextSpec.size(); ++j) {
|
||||
if (currentSpec.back().wavelength < nextSpec[j].wavelength) {
|
||||
nextStart = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nextSpec[nextStart].intensity > currentSpec.back().intensity) {
|
||||
for (size_t j = 0; j < currentSpec.size(); ++j) {
|
||||
if (currentSpec[j].wavelength > nextSpec.front().wavelength) {
|
||||
currentEnd = j;
|
||||
nextStart = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
currentEnd = currentSpec.size();
|
||||
nextStart = 0;
|
||||
}
|
||||
specV.insert(specV.end(), currentSpec.begin() + channelNextStart, currentSpec.begin() + currentEnd);
|
||||
channelNextStart = nextStart;
|
||||
|
||||
}
|
||||
if (!dspecV.empty()) {
|
||||
specV.insert(specV.end(), dspecV.back().begin() + channelNextStart, dspecV.back().end());
|
||||
}
|
||||
|
||||
return specV;
|
||||
}
|
||||
|
||||
|
||||
bool AlgorithmAnalysis::cmp(pair<int,vector<SpectrumChannelData>> a, pair<int,vector<SpectrumChannelData>> b) {
|
||||
return a.second[0].wavelength < b.second[0].wavelength;
|
||||
}
|
||||
|
||||
//3点寻峰
|
||||
vector<SpectrumChannelData> AlgorithmAnalysis::threeFindPeak(vector<SpectrumChannelData> specLst,vector<SpectrumChannelData> specLst1){
|
||||
vector<SpectrumChannelData> peakLst;
|
||||
for (int i = 1; i < specLst.size() - 1; i++) {
|
||||
if (specLst[i].intensity > specLst[i - 1].intensity
|
||||
&& specLst[i].intensity > specLst[i + 1].intensity) {
|
||||
peakLst.push_back({specLst[i].wavelength,specLst1[i].intensity});
|
||||
}
|
||||
}
|
||||
return peakLst;
|
||||
}
|
||||
|
||||
//寻峰
|
||||
vector<SpectrumChannelData> AlgorithmAnalysis::findPeak(vector<SpectrumChannelData> specLst,vector<SpectrumChannelData> specLst1){
|
||||
vector<SpectrumChannelData> peakLst;
|
||||
vector<SpectrumChannelData> AApeakLst;
|
||||
vector<double> findmax;
|
||||
double start = 0;
|
||||
double end = 0;
|
||||
double whl = 0;
|
||||
double gk = 0;
|
||||
for(int i = 0 ; i < specLst1.size() ; ++i){
|
||||
findmax.push_back(specLst1[i].intensity);
|
||||
}
|
||||
|
||||
|
||||
auto maxElement = max_element(findmax.begin(), findmax.end());
|
||||
gk = *maxElement * 0.0001;
|
||||
for (int i = 4; i < specLst.size() - 4; i++) {
|
||||
|
||||
if (specLst[i].intensity > specLst[i - 1].intensity
|
||||
&& specLst[i - 1].intensity > specLst[i - 2].intensity
|
||||
&& specLst[i].intensity > specLst[i + 1].intensity
|
||||
&& specLst[i + 1].intensity > specLst[i + 2].intensity
|
||||
&& specLst1[i].intensity > gk
|
||||
)
|
||||
{
|
||||
if(specLst[i].wavelength > 359){
|
||||
specLst[i].wavelength = specLst[i].wavelength -0.07;
|
||||
}else if(specLst[i].wavelength < 309){
|
||||
specLst[i].wavelength = specLst[i].wavelength - 0.03;
|
||||
}else {
|
||||
specLst[i].wavelength = specLst[i].wavelength -0.02;
|
||||
}
|
||||
|
||||
AApeakLst.push_back({specLst[i].wavelength,specLst1[i].intensity});
|
||||
|
||||
start = 0.5 * (specLst[i - 3].intensity + specLst[i - 4].intensity) * (specLst[i - 3].wavelength - specLst[i - 4].wavelength)
|
||||
+ 0.5 * (specLst[i - 2].intensity + specLst[i - 3].intensity) * (specLst[i - 2].wavelength - specLst[i - 3].wavelength)
|
||||
+ 0.5 * (specLst[i - 1].intensity + specLst[i - 2].intensity) * (specLst[i - 1].wavelength - specLst[i - 2].wavelength)
|
||||
+ 0.5 * (specLst[i].intensity + specLst[i - 1].intensity) * (specLst[i].wavelength - specLst[i - 1].wavelength);
|
||||
end = 0.5 * (specLst[i].intensity + specLst[i + 1].intensity) * (specLst[i + 1].wavelength - specLst[i].wavelength)
|
||||
+ 0.5 * (specLst[i + 2].intensity + specLst[i + 1].intensity) * (specLst[i + 2].wavelength - specLst[i + 1].wavelength)
|
||||
+ 0.5 * (specLst[i + 3].intensity + specLst[i + 2].intensity) * (specLst[i + 3].wavelength - specLst[i + 2].wavelength)
|
||||
+ 0.5 * (specLst[i + 4].intensity + specLst[i + 3].intensity) * (specLst[i + 4].wavelength - specLst[i + 3].wavelength);
|
||||
whl = start / end;
|
||||
|
||||
if( whl > 0.4 && whl < 0.6)
|
||||
{
|
||||
peakLst.push_back({specLst[i].wavelength,specLst1[i].intensity});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return AApeakLst;
|
||||
}
|
||||
|
||||
//由峰值寻找元素
|
||||
vector<SpectrumElementPeak> AlgorithmAnalysis::findElement(vector<SpectrumChannelData> peakVector){
|
||||
vector<SpectrumElementPeak> peaks;
|
||||
SpectrumElementPeak elementpeak;
|
||||
for(int i = 0;i < peakVector.size(); i++){
|
||||
for(int j= 0; j < elements.size(); j++){
|
||||
double elementWavelength = elements[j].wavelength;
|
||||
double peakWavelength = peakVector[i].wavelength;
|
||||
|
||||
if(abs(elementWavelength - peakWavelength) < 0.11){
|
||||
elementpeak.elementName = elements[j].name;
|
||||
elementpeak.intensity = peakVector[i].intensity;
|
||||
peaks.push_back(elementpeak);
|
||||
}
|
||||
}
|
||||
}
|
||||
return peaks;
|
||||
}
|
||||
|
||||
//光谱平滑处理
|
||||
vector<SpectrumChannelData> AlgorithmAnalysis::smoothingAlgorithm(vector<SpectrumChannelData> smoothLst){
|
||||
int N = smoothLst.size();
|
||||
int M = N - 8;
|
||||
vector<double> NP(9);
|
||||
double Nsum = 0;
|
||||
vector<SpectrumChannelData> smoothLst1;
|
||||
smoothLst1 = smoothLst;
|
||||
for (int i = 1; i < 9; i++) {
|
||||
NP[i] = smoothLst[i - 1].intensity;
|
||||
}
|
||||
|
||||
for (int i = 0; i < M; i++) {
|
||||
for (int k = 0; k < 8; k++) {
|
||||
NP[k] = NP[k + 1];
|
||||
}
|
||||
NP[8] = smoothLst[i + 8].intensity;
|
||||
|
||||
Nsum = (59 * NP[4] + 54 * (NP[3] + NP[5]) + 39 * (NP[2] + NP[6]) + 14 * (NP[1] + NP[7]) - 21 * (NP[0] + NP[8]))/ 231;
|
||||
smoothLst1[i + 4].intensity = Nsum;
|
||||
}
|
||||
return smoothLst1;
|
||||
}
|
||||
|
||||
//领域平均去背景
|
||||
vector<double> AlgorithmAnalysis::filedAverageAlgorithm( vector<double> intensity)
|
||||
{
|
||||
int N = intensity.size();
|
||||
int M = N - 10;
|
||||
vector<double> NP(11);
|
||||
double Nsum = 0;
|
||||
vector<double> myIntensity;
|
||||
myIntensity = intensity;
|
||||
//vector<SpectrumChannelData> smoothLst1;
|
||||
//smoothLst1 = channelDataList;
|
||||
for (int i = 1; i < 11; i++) {
|
||||
NP[i] = myIntensity[i-1];
|
||||
}
|
||||
|
||||
for (int i = 0; i < M; i++) {
|
||||
for (int k = 0; k < 10; k++) {
|
||||
NP[k] = NP[k + 1];
|
||||
}
|
||||
NP[10] = myIntensity[i + 10];
|
||||
|
||||
Nsum = (NP[0] + NP[1] + NP[2] + NP[3] + NP[4] + NP[5]+ NP[6] + NP[7] + NP[8] + NP[9] + NP[10] ) / 11;
|
||||
intensity[i + 5] = Nsum;
|
||||
}
|
||||
return intensity;
|
||||
|
||||
}
|
||||
|
||||
//寻找3个通道的峰值数据
|
||||
vector<SpectrumChannelData> AlgorithmAnalysis::findPeakList (vector<SpectrumChannelData> channelData, int serialNumber)
|
||||
{
|
||||
vector<SpectrumChannelData> peakList;
|
||||
string fileName;
|
||||
vector<int> oridianlList;
|
||||
switch(serialNumber)
|
||||
{
|
||||
case 1:
|
||||
fileName = "/home/pi/SpecData/Channel1.txt";
|
||||
break;
|
||||
case 2:
|
||||
fileName = "/home/pi/SpecData/Channel2.txt";
|
||||
break;
|
||||
case 3:
|
||||
fileName = "/home/pi/SpecData/Channel3.txt";
|
||||
break;
|
||||
default:
|
||||
fileName = "";
|
||||
break;
|
||||
};
|
||||
ifstream infile(fileName);
|
||||
if (!infile) {
|
||||
qDebug() << "Failed to open file!";
|
||||
return peakList ;
|
||||
}
|
||||
vector<std::string> elementNameList;
|
||||
vector<int> ordinalList;
|
||||
|
||||
while(!infile.eof()){
|
||||
string eleName;
|
||||
int ordinal;
|
||||
infile >> eleName >> ordinal;
|
||||
//elementNameList.push_back(eleName);
|
||||
oridianlList.push_back(ordinal);
|
||||
}
|
||||
infile.close();
|
||||
|
||||
for(const int oneOrdinal : oridianlList){
|
||||
peakList.push_back(channelData[oneOrdinal]);
|
||||
}
|
||||
|
||||
return peakList;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
#ifndef ALGORITHMANALYSIS_H
|
||||
#define ALGORITHMANALYSIS_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct SpectrumChannelData {
|
||||
double wavelength;
|
||||
double intensity;
|
||||
};
|
||||
|
||||
// 定义元素结构体
|
||||
struct SpectrumElement {
|
||||
string name;
|
||||
double wavelength;
|
||||
};
|
||||
|
||||
struct SpectrumElementPeak{
|
||||
string elementName;
|
||||
double intensity;
|
||||
};
|
||||
|
||||
|
||||
class AlgorithmAnalysis
|
||||
{
|
||||
public:
|
||||
AlgorithmAnalysis();
|
||||
|
||||
|
||||
|
||||
//求光谱强度的平均值
|
||||
static vector<double> average(const vector<vector<double>> &multiLst );
|
||||
|
||||
//求光谱强度的平均值
|
||||
static vector<SpectrumChannelData> average(const vector<vector<SpectrumChannelData>> &multiLst );
|
||||
|
||||
//百分位滤波去背景
|
||||
static vector<double> background(vector<double> waveLst,vector<double> averageSpecLst);
|
||||
|
||||
|
||||
static void background(vector<SpectrumChannelData> &SpectrumChannelData);
|
||||
|
||||
/*
|
||||
* 裁剪重叠的光谱数据
|
||||
*/
|
||||
static vector<SpectrumChannelData> cutRepeatSpecture(vector<SpectrumChannelData> ospecV1,
|
||||
vector<SpectrumChannelData> ospecV2,
|
||||
vector<SpectrumChannelData> ospecV3,
|
||||
vector<SpectrumChannelData> ospecV4,int cutSize);
|
||||
|
||||
static vector<SpectrumChannelData> cutRepeatSpecture(vector<pair<int, vector<SpectrumChannelData>>>,int cutSize);
|
||||
|
||||
//光谱数据排序
|
||||
static bool cmp(pair<int,vector<SpectrumChannelData>> a, pair<int,vector<SpectrumChannelData>> b);
|
||||
|
||||
//寻峰
|
||||
static vector<SpectrumChannelData> findPeak(vector<SpectrumChannelData> specLst,vector<SpectrumChannelData> specLst1);
|
||||
|
||||
//3点寻峰
|
||||
static vector<SpectrumChannelData> threeFindPeak(vector<SpectrumChannelData> specLst,vector<SpectrumChannelData> specLst1);
|
||||
|
||||
//由峰值寻找元素
|
||||
static vector<SpectrumElementPeak> findElement(vector<SpectrumChannelData> peakVector);
|
||||
|
||||
static vector<SpectrumElement> elements;
|
||||
|
||||
//光谱平滑处理
|
||||
static vector<SpectrumChannelData> smoothingAlgorithm(vector<SpectrumChannelData> smoothLst);
|
||||
|
||||
//寻找3个通道的峰值数据
|
||||
static vector<SpectrumChannelData> findPeakList (vector<SpectrumChannelData> SpectrumChannelData, int serialNumber);
|
||||
|
||||
//领域平均去背景
|
||||
static vector<double> filedAverageAlgorithm( vector<double> intensity);
|
||||
};
|
||||
|
||||
#endif // ALGORITHMANALYSIS_H
|
||||
@@ -0,0 +1,204 @@
|
||||
// ===========================================================================
|
||||
// arisbridge.cpp —— MSVC 侧动态加载 aris_bridge.dll 并调用其纯 C 接口
|
||||
// ===========================================================================
|
||||
|
||||
#include "arisbridge.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
namespace {
|
||||
const char *const kBridgeFileName = "aris_bridge.dll";
|
||||
}
|
||||
|
||||
ArisBridge *ArisBridge::s_instance = nullptr;
|
||||
|
||||
ArisBridge &ArisBridge::instance()
|
||||
{
|
||||
// 函数内静态:第一次使用时加载 DLL,之后复用
|
||||
static ArisBridge bridge;
|
||||
return bridge;
|
||||
}
|
||||
|
||||
ArisBridge::ArisBridge()
|
||||
{
|
||||
load();
|
||||
}
|
||||
|
||||
ArisBridge::~ArisBridge()
|
||||
{
|
||||
if (m_available)
|
||||
{
|
||||
disconnect();
|
||||
}
|
||||
m_available = false;
|
||||
s_instance = nullptr;
|
||||
if (m_module)
|
||||
{
|
||||
FreeLibrary(static_cast<HMODULE>(m_module));
|
||||
m_module = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool ArisBridge::load()
|
||||
{
|
||||
// 优先从 exe 所在目录加载,避免被 PATH 里的同名 DLL 抢先
|
||||
char exePath[MAX_PATH] = {0};
|
||||
GetModuleFileNameA(nullptr, exePath, MAX_PATH);
|
||||
std::string directory(exePath);
|
||||
const std::string::size_type pos = directory.find_last_of("\\/");
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
directory.erase(pos);
|
||||
}
|
||||
const std::string fullPath = directory + "\\" + kBridgeFileName;
|
||||
|
||||
HMODULE module = LoadLibraryA(fullPath.c_str());
|
||||
if (!module)
|
||||
{
|
||||
module = LoadLibraryA(kBridgeFileName);
|
||||
}
|
||||
if (!module)
|
||||
{
|
||||
m_lastError = "加载 " + fullPath + " 失败(错误码 "
|
||||
+ std::to_string(static_cast<long>(GetLastError())) + ")";
|
||||
return false;
|
||||
}
|
||||
m_module = module;
|
||||
|
||||
m_connect = reinterpret_cast<FnConnect>(GetProcAddress(module, "aris_bridge_connect"));
|
||||
m_disconnect = reinterpret_cast<FnDisconnect>(GetProcAddress(module, "aris_bridge_disconnect"));
|
||||
m_deviceCount = reinterpret_cast<FnDeviceCount>(GetProcAddress(module, "aris_bridge_device_count"));
|
||||
m_deviceInfo = reinterpret_cast<FnDeviceInfo>(GetProcAddress(module, "aris_bridge_device_info"));
|
||||
m_deviceWavelengths = reinterpret_cast<FnDeviceWavelengths>(GetProcAddress(module, "aris_bridge_device_wavelengths"));
|
||||
m_setDataCallback = reinterpret_cast<FnSetDataCallback>(GetProcAddress(module, "aris_bridge_set_data_callback"));
|
||||
m_setSoftwareTrigger = reinterpret_cast<FnNoArg>(GetProcAddress(module, "aris_bridge_set_software_trigger"));
|
||||
m_startMeasure = reinterpret_cast<FnStartMeasure>(GetProcAddress(module, "aris_bridge_start_measure"));
|
||||
m_stopMeasure = reinterpret_cast<FnNoArg>(GetProcAddress(module, "aris_bridge_stop_measure"));
|
||||
|
||||
if (!m_connect || !m_disconnect || !m_deviceCount || !m_deviceInfo
|
||||
|| !m_deviceWavelengths || !m_setDataCallback || !m_setSoftwareTrigger
|
||||
|| !m_startMeasure || !m_stopMeasure)
|
||||
{
|
||||
m_lastError = "aris_bridge.dll 的导出函数不完整(版本不匹配?)";
|
||||
FreeLibrary(module);
|
||||
m_module = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
s_instance = this;
|
||||
m_setDataCallback(&ArisBridge::dataTrampoline);
|
||||
m_available = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void __cdecl ArisBridge::dataTrampoline(const char *serial,
|
||||
const float *wavelengths, int wavelengthCount,
|
||||
const float *intensities, int intensityCount)
|
||||
{
|
||||
ArisBridge *self = s_instance;
|
||||
if (!self || !self->m_handler)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::string serialNumber = serial ? serial : "";
|
||||
std::vector<float> wave;
|
||||
if (wavelengths && wavelengthCount > 0)
|
||||
{
|
||||
wave.assign(wavelengths, wavelengths + wavelengthCount);
|
||||
}
|
||||
std::vector<float> spectrum;
|
||||
if (intensities && intensityCount > 0)
|
||||
{
|
||||
spectrum.assign(intensities, intensities + intensityCount);
|
||||
}
|
||||
|
||||
self->m_handler(serialNumber, wave, spectrum);
|
||||
}
|
||||
|
||||
int ArisBridge::connect()
|
||||
{
|
||||
if (!m_available)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return m_connect();
|
||||
}
|
||||
|
||||
void ArisBridge::disconnect()
|
||||
{
|
||||
if (m_available && m_disconnect)
|
||||
{
|
||||
m_disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
int ArisBridge::deviceCount() const
|
||||
{
|
||||
if (!m_available || !m_deviceCount)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return m_deviceCount();
|
||||
}
|
||||
|
||||
ArisBridge::DeviceInfo ArisBridge::deviceInfo(int index) const
|
||||
{
|
||||
DeviceInfo info;
|
||||
if (!m_available || !m_deviceInfo)
|
||||
{
|
||||
return info;
|
||||
}
|
||||
|
||||
char serial[256] = {0};
|
||||
int startPixel = 0;
|
||||
int endPixel = 0;
|
||||
int pixelCount = 0;
|
||||
if (m_deviceInfo(index, serial, static_cast<int>(sizeof(serial)),
|
||||
&startPixel, &endPixel, &pixelCount) != 0)
|
||||
{
|
||||
return info;
|
||||
}
|
||||
|
||||
info.serialNumber = serial;
|
||||
info.startPixel = startPixel;
|
||||
info.endPixel = endPixel;
|
||||
info.pixelCount = pixelCount;
|
||||
|
||||
const int capacity = (pixelCount > 0) ? pixelCount : 8192;
|
||||
std::vector<float> wavelengths(static_cast<size_t>(capacity), 0.0f);
|
||||
const int got = m_deviceWavelengths(index, wavelengths.data(), capacity);
|
||||
if (got > 0)
|
||||
{
|
||||
wavelengths.resize(static_cast<size_t>(got));
|
||||
info.wavelengths.swap(wavelengths);
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
void ArisBridge::setSoftwareTrigger()
|
||||
{
|
||||
if (m_available && m_setSoftwareTrigger)
|
||||
{
|
||||
m_setSoftwareTrigger();
|
||||
}
|
||||
}
|
||||
|
||||
int ArisBridge::startMeasure(int integrationTimeUs, int averaging)
|
||||
{
|
||||
if (!m_available || !m_startMeasure)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return m_startMeasure(integrationTimeUs, averaging);
|
||||
}
|
||||
|
||||
void ArisBridge::stopMeasure()
|
||||
{
|
||||
if (m_available && m_stopMeasure)
|
||||
{
|
||||
m_stopMeasure();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
// ===========================================================================
|
||||
// arisbridge.h —— MSVC 侧对 aris_bridge.dll 的封装
|
||||
//
|
||||
// 厂商的 libaris.dll 是 MinGW/GCC 编的 C++ 库,MSVC 程序无法直接链接;
|
||||
// aris_bridge.dll 由 MinGW 编译,对外只暴露纯 C 函数(见同目录/third_party/
|
||||
// aris-bridge/src/aris_bridge.h)。本类用 LoadLibrary 动态加载它,把 C 接口
|
||||
// 转成 C++ 方法,供 DeviceManager 使用。
|
||||
// ===========================================================================
|
||||
|
||||
#ifndef ARISBRIDGE_H
|
||||
#define ARISBRIDGE_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class ArisBridge
|
||||
{
|
||||
public:
|
||||
// 一台设备的静态参数
|
||||
struct DeviceInfo {
|
||||
std::string serialNumber;
|
||||
int startPixel = 0;
|
||||
int endPixel = 0;
|
||||
int pixelCount = 0;
|
||||
std::vector<float> wavelengths;
|
||||
};
|
||||
|
||||
// 光谱数据回调(在 SDK 的采集线程里被调用)
|
||||
typedef void (*DataHandler)(const std::string &serialNumber,
|
||||
const std::vector<float> &wavelengths,
|
||||
const std::vector<float> &intensities);
|
||||
|
||||
static ArisBridge &instance();
|
||||
|
||||
// 桥接 DLL 是否加载成功
|
||||
bool isAvailable() const { return m_available; }
|
||||
// 加载失败时的原因
|
||||
const std::string &lastError() const { return m_lastError; }
|
||||
|
||||
int connect(); // 查找并初始化设备,返回设备数量;-1 表示失败
|
||||
void disconnect(); // 停止测量并断开
|
||||
int deviceCount() const;
|
||||
|
||||
DeviceInfo deviceInfo(int index) const;
|
||||
void setSoftwareTrigger();
|
||||
int startMeasure(int integrationTimeUs, int averaging);
|
||||
void stopMeasure();
|
||||
|
||||
void setDataHandler(DataHandler handler) { m_handler = handler; }
|
||||
|
||||
private:
|
||||
ArisBridge();
|
||||
~ArisBridge();
|
||||
ArisBridge(const ArisBridge &) = delete;
|
||||
ArisBridge &operator=(const ArisBridge &) = delete;
|
||||
|
||||
bool load();
|
||||
static void __cdecl dataTrampoline(const char *serial,
|
||||
const float *wavelengths, int wavelengthCount,
|
||||
const float *intensities, int intensityCount);
|
||||
|
||||
bool m_available = false;
|
||||
std::string m_lastError;
|
||||
void *m_module = nullptr;
|
||||
DataHandler m_handler = nullptr;
|
||||
|
||||
// 从 aris_bridge.dll 解析出来的 C 函数
|
||||
typedef int (__cdecl *FnConnect)(void);
|
||||
typedef void (__cdecl *FnDisconnect)(void);
|
||||
typedef int (__cdecl *FnDeviceCount)(void);
|
||||
typedef int (__cdecl *FnDeviceInfo)(int, char *, int, int *, int *, int *);
|
||||
typedef int (__cdecl *FnDeviceWavelengths)(int, float *, int);
|
||||
typedef void (__cdecl *FnSetDataCallback)(void (__cdecl *)(const char *, const float *, int, const float *, int));
|
||||
typedef void (__cdecl *FnNoArg)(void);
|
||||
typedef int (__cdecl *FnStartMeasure)(int, int);
|
||||
|
||||
FnConnect m_connect = nullptr;
|
||||
FnDisconnect m_disconnect = nullptr;
|
||||
FnDeviceCount m_deviceCount = nullptr;
|
||||
FnDeviceInfo m_deviceInfo = nullptr;
|
||||
FnDeviceWavelengths m_deviceWavelengths = nullptr;
|
||||
FnSetDataCallback m_setDataCallback = nullptr;
|
||||
FnNoArg m_setSoftwareTrigger = nullptr;
|
||||
FnStartMeasure m_startMeasure = nullptr;
|
||||
FnNoArg m_stopMeasure = nullptr;
|
||||
|
||||
static ArisBridge *s_instance;
|
||||
};
|
||||
|
||||
#endif // ARISBRIDGE_H
|
||||
@@ -0,0 +1,312 @@
|
||||
#define _WINSOCKAPI_
|
||||
#define _HAS_STD_BYTE 0
|
||||
#include <winsock2.h> // 确保首先包含
|
||||
#include <windows.h> // 然后包含windows.h
|
||||
#include <QDebug>
|
||||
#include "devicemanager.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "qdatetime.h"
|
||||
#include "../Public/util.h"
|
||||
#include <QFile>
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#ifndef CAVITY_USE_ARIS_BRIDGE
|
||||
#include "ziolinkprotocol.h"
|
||||
#include "spectrometerdevice.h"
|
||||
#endif
|
||||
#include "spectrometer.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
DeviceManager* g_pDeviceManager = nullptr;
|
||||
|
||||
unsigned int DeviceManager::m_nDeviceNum = 0; //实际已连接的设备数量
|
||||
std::vector<Spectrometer*> DeviceManager::m_pDeviceLst ;
|
||||
|
||||
DeviceManager::DeviceManager(QObject *parent) : QObject(parent), pixelCount(0)
|
||||
{
|
||||
qDebug() << "激光器串口连接" << endl;
|
||||
|
||||
m_pDeviceLst.resize(0); //new std::vector<NewDevice*>();
|
||||
|
||||
m_nDeviceNum = 0;
|
||||
|
||||
g_pDeviceManager = this;
|
||||
|
||||
#ifdef CAVITY_USE_ARIS_BRIDGE
|
||||
// MSVC:通过 MinGW 桥接 DLL 使用真实 ARIS SDK
|
||||
ArisBridge &bridge = ArisBridge::instance();
|
||||
bridge.setDataHandler(&DeviceManager::onBridgeData);
|
||||
if (bridge.isAvailable())
|
||||
{
|
||||
qDebug() << "ARIS 桥接 DLL 已加载(aris_bridge.dll)";
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "ARIS 桥接 DLL 不可用:" << QString::fromStdString(bridge.lastError());
|
||||
}
|
||||
#else
|
||||
m_pZio = new ZioLinkProtocol();
|
||||
#endif
|
||||
}
|
||||
|
||||
DeviceManager::~DeviceManager()
|
||||
{
|
||||
DeactivateDeviceLst();
|
||||
#ifndef CAVITY_USE_ARIS_BRIDGE
|
||||
delete m_pZio;
|
||||
m_pZio = nullptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
// 读取波长校准txt文件
|
||||
std::vector<float> DeviceManager::readWavelengthCalibratedFile(const string &serialNumber) {
|
||||
std::vector<float> result;
|
||||
std::string filename = "wavelength_calibrated_files/wavelength_calibrated_" + serialNumber + ".txt";
|
||||
std::ifstream file(filename);
|
||||
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("无法打开文件: " + filename);
|
||||
}
|
||||
qDebug() << "成功读取波长校准文件:" + QString::fromStdString(filename);
|
||||
float value;
|
||||
while (file >> value) {
|
||||
result.push_back(value);
|
||||
}
|
||||
// 检查读取过程是否出错(排除正常到达文件末尾的情况)
|
||||
if (!file.eof() && file.fail()) {
|
||||
throw std::runtime_error("文件格式错误: " + filename);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 用校准文件的波长覆盖设备波长;失败时退回设备自带波长
|
||||
void DeviceManager::loadDeviceWavelengths(Spectrometer *device, const std::vector<float> &fallback)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::vector<float> wavelengthListFixed = readWavelengthCalibratedFile(device->m_serialNumber);
|
||||
if (!wavelengthListFixed.empty())
|
||||
{
|
||||
device->m_wavelengthList.assign(wavelengthListFixed.begin(), wavelengthListFixed.end());
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// 校准文件不存在或格式错误,走下面的回退分支
|
||||
}
|
||||
|
||||
qDebug() << "波长未校准,光谱仪序列号=" + QString::fromStdString(device->m_serialNumber);
|
||||
device->m_wavelengthList.assign(fallback.begin(), fallback.end());
|
||||
}
|
||||
|
||||
std::vector<Spectrometer*> DeviceManager::DetectDeviceLst() //检测设备列表,并激活(打开设备)
|
||||
{
|
||||
qDebug() << "检测设备列表,并激活(打开设备)" << endl;
|
||||
// 如果设备列表不为空,直接返回缓存的设备列表
|
||||
if(m_pDeviceLst.size() > 0){
|
||||
qDebug() << "返回已缓存的设备列表,设备数量:" << m_pDeviceLst.size();
|
||||
return m_pDeviceLst;
|
||||
}
|
||||
qDebug() << "首次检测设备,开始连接...";
|
||||
|
||||
#ifdef CAVITY_USE_ARIS_BRIDGE
|
||||
// ---------------- 桥接 DLL 后端(MSVC)----------------
|
||||
ArisBridge &bridge = ArisBridge::instance();
|
||||
if (!bridge.isAvailable())
|
||||
{
|
||||
qDebug() << "光谱仪不可用(桥接 DLL 未加载):"
|
||||
<< QString::fromStdString(bridge.lastError());
|
||||
m_nDeviceNum = 0;
|
||||
return m_pDeviceLst;
|
||||
}
|
||||
|
||||
const int deviceCount = bridge.connect();
|
||||
m_nDeviceNum = (deviceCount > 0) ? static_cast<unsigned int>(deviceCount) : 0;
|
||||
m_pDeviceLst.clear();
|
||||
|
||||
for (int i = 0; i < deviceCount; i++)
|
||||
{
|
||||
const ArisBridge::DeviceInfo info = bridge.deviceInfo(i);
|
||||
|
||||
Spectrometer* device = new Spectrometer();
|
||||
device->m_serialNumber = info.serialNumber;
|
||||
device->m_handle = static_cast<short>(i);
|
||||
device->m_pixelsCount = static_cast<unsigned short>(info.pixelCount);
|
||||
device->m_startPixels = static_cast<unsigned short>(info.startPixel);
|
||||
device->m_stopPixels = static_cast<unsigned short>(info.endPixel);
|
||||
|
||||
loadDeviceWavelengths(device, info.wavelengths);
|
||||
|
||||
m_pDeviceLst.push_back(device);
|
||||
qDebug() << "创建设备:" << QString::fromStdString(device->m_serialNumber)
|
||||
<< ",句柄:" << i << ",像素:" << static_cast<int>(device->m_pixelsCount);
|
||||
pixelCount += static_cast<int>(device->m_wavelengthList.size());
|
||||
}
|
||||
|
||||
if (m_nDeviceNum > 0)
|
||||
{
|
||||
qDebug() << "设置触发模式";
|
||||
bridge.setSoftwareTrigger();
|
||||
}
|
||||
|
||||
qDebug() << "设备检测完成,总共创建" << m_nDeviceNum << "个设备";
|
||||
return m_pDeviceLst;
|
||||
#else
|
||||
// ---------------- SDK / 兼容桩后端 ----------------
|
||||
m_pZio->connectDevice();
|
||||
m_nDeviceNum = m_pZio->getConnectedDeviceCount();
|
||||
// 确保列表为空(防止重复添加
|
||||
m_pDeviceLst.clear();
|
||||
|
||||
for(int i = 0; i < m_nDeviceNum; i++){
|
||||
SpectrometerParameters params = m_pZio->getDevice(i)->getParameters();
|
||||
|
||||
Spectrometer* device = new Spectrometer();
|
||||
device->m_serialNumber = params.serialNumber;
|
||||
device->m_handle = i;
|
||||
device->m_pixelsCount = static_cast<unsigned short>(params.pixelCount);
|
||||
device->m_startPixels = static_cast<unsigned short>(params.startPixel);
|
||||
device->m_stopPixels = static_cast<unsigned short>(params.endPixel);
|
||||
|
||||
loadDeviceWavelengths(device, params.wavelengths);
|
||||
|
||||
m_pDeviceLst.push_back(device);
|
||||
qDebug() << "创建设备:" << QString::fromStdString(device->m_serialNumber) << ",句柄:" << i;
|
||||
pixelCount += static_cast<int>(device->m_wavelengthList.size());
|
||||
}
|
||||
|
||||
if(m_nDeviceNum > 0){
|
||||
qDebug() << "设置触发模式";
|
||||
m_pZio->setTriggerMode(TriggerMode::SOFTWARE);
|
||||
}
|
||||
|
||||
qDebug() << "设备检测完成,总共创建" << m_nDeviceNum << "个设备";
|
||||
|
||||
return m_pDeviceLst;
|
||||
#endif
|
||||
}
|
||||
|
||||
void DeviceManager::DeactivateDeviceLst() //关闭所有设备
|
||||
{
|
||||
qDebug() << "关闭所有设备,当前设备数量:" << m_pDeviceLst.size();
|
||||
|
||||
// 先断开设备连接
|
||||
#ifdef CAVITY_USE_ARIS_BRIDGE
|
||||
ArisBridge::instance().disconnect();
|
||||
#else
|
||||
if(m_pZio) {
|
||||
m_pZio->disconnectDevice();
|
||||
}
|
||||
#endif
|
||||
|
||||
// 删除所有设备对象
|
||||
for(auto& device : m_pDeviceLst){
|
||||
if(device != nullptr) {
|
||||
qDebug() << "删除设备:" << QString::fromStdString(device->m_serialNumber);
|
||||
delete device;
|
||||
device = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// 清空容器
|
||||
m_pDeviceLst.clear();
|
||||
m_nDeviceNum = 0;
|
||||
|
||||
qDebug() << "所有设备已关闭并清理";
|
||||
}
|
||||
|
||||
void DeviceManager::onDetectDeviceLst()
|
||||
{
|
||||
DetectDeviceLst();
|
||||
emit DetectDeviceFinish(m_pDeviceLst);
|
||||
}
|
||||
|
||||
void DeviceManager::CloseAllDevice()
|
||||
{
|
||||
#ifdef CAVITY_USE_ARIS_BRIDGE
|
||||
ArisBridge::instance().disconnect();
|
||||
#else
|
||||
m_pZio->disconnectDevice();
|
||||
#endif
|
||||
}
|
||||
|
||||
//获取设备列表
|
||||
std::vector<Spectrometer*> DeviceManager::GetDeviceList()
|
||||
{
|
||||
if(m_pDeviceLst.size() == 0){
|
||||
return DetectDeviceLst();
|
||||
}
|
||||
return m_pDeviceLst;
|
||||
}
|
||||
|
||||
void DeviceManager::stopMeasure()
|
||||
{
|
||||
#ifdef CAVITY_USE_ARIS_BRIDGE
|
||||
ArisBridge::instance().stopMeasure();
|
||||
#else
|
||||
m_pZio->stopMeasurement();
|
||||
#endif
|
||||
}
|
||||
|
||||
int DeviceManager::StartMeasure(int integrationTime, int unNrAverages)
|
||||
{
|
||||
#ifdef CAVITY_USE_ARIS_BRIDGE
|
||||
ArisBridge::instance().startMeasure(integrationTime, unNrAverages);
|
||||
#else
|
||||
m_pZio->startMeasurement(this, integrationTime, unNrAverages);
|
||||
#endif
|
||||
bStartMeasure=true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 把光谱数据转成 Qt 信号(可能在 SDK 采集线程里被调用,Qt 会自动排队到界面线程)
|
||||
void DeviceManager::handleData(const std::string &spectrumSerialNumber,
|
||||
const std::vector<float> &wavelengthList,
|
||||
const std::vector<float> &intensityList)
|
||||
{
|
||||
QVector<float> qWavelengthList(wavelengthList.begin(), wavelengthList.end());
|
||||
QVector<float> qIntensityList(intensityList.begin(), intensityList.end());
|
||||
emit spectrumDataReady(QString::fromStdString(spectrumSerialNumber),
|
||||
qWavelengthList,
|
||||
qIntensityList);
|
||||
}
|
||||
|
||||
#ifdef CAVITY_USE_ARIS_BRIDGE
|
||||
// 桥接 DLL 的 C 回调 → 当前 DeviceManager 实例
|
||||
void DeviceManager::onBridgeData(const std::string &spectrumSerialNumber,
|
||||
const std::vector<float> &wavelengthList,
|
||||
const std::vector<float> &intensityList)
|
||||
{
|
||||
if (g_pDeviceManager)
|
||||
{
|
||||
g_pDeviceManager->handleData(spectrumSerialNumber, wavelengthList, intensityList);
|
||||
}
|
||||
}
|
||||
#else
|
||||
void DeviceManager::onDataReceived(std::string spectrumSerialNumber,
|
||||
const std::vector<float> &wavelengthList,
|
||||
const std::vector<float> &intensityList)
|
||||
{
|
||||
handleData(spectrumSerialNumber, wavelengthList, intensityList);
|
||||
}
|
||||
|
||||
void DeviceManager::onDataReceivedBanch(std::string spectrumSerialNumber,
|
||||
const std::vector<float> &wavelengthList,
|
||||
const std::vector<std::vector<float>> &spectrumList)
|
||||
{
|
||||
// 批量模式:逐帧转成单帧信号发出,界面处理逻辑无需改动
|
||||
for (const auto &intensityList : spectrumList) {
|
||||
onDataReceived(spectrumSerialNumber, wavelengthList, intensityList);
|
||||
}
|
||||
}
|
||||
|
||||
void DeviceManager::onError(std::string deviceIndex, const string &error)
|
||||
{
|
||||
qDebug() << "光谱仪出错了" << "deviceIndex=" << QString::fromStdString(deviceIndex)
|
||||
<< "error is " << QString::fromStdString(error);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,202 @@
|
||||
#define _WINSOCKAPI_
|
||||
#define _HAS_STD_BYTE 0
|
||||
#include <winsock2.h> // 确保首先包含
|
||||
#include <windows.h> // 然后包含windows.h
|
||||
#include <QDebug>
|
||||
#include "devicemanager.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "qdatetime.h"
|
||||
#include "../Public/util.h"
|
||||
#include <QFile>
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include "ziolinkprotocol.h"
|
||||
#include "spectrometerdevice.h"
|
||||
#include "spectrometer.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
DeviceManager* g_pDeviceManager = nullptr;
|
||||
|
||||
unsigned int DeviceManager::m_nDeviceNum = 0; //实际已连接的设备数量
|
||||
std::vector<Spectrometer*> DeviceManager::m_pDeviceLst ;
|
||||
|
||||
DeviceManager::DeviceManager(QObject *parent) : QObject(parent), pixelCount(0)
|
||||
{
|
||||
qDebug() << "激光器串口连接" << endl;
|
||||
|
||||
m_pDeviceLst.resize(0); //new std::vector<NewDevice*>();
|
||||
|
||||
m_nDeviceNum = 0;
|
||||
|
||||
g_pDeviceManager = this;
|
||||
|
||||
m_pZio = new ZioLinkProtocol();
|
||||
}
|
||||
|
||||
DeviceManager::~DeviceManager()
|
||||
{
|
||||
DeactivateDeviceLst();
|
||||
delete m_pZio;
|
||||
m_pZio = nullptr;
|
||||
}
|
||||
|
||||
// 读取波长校准txt文件
|
||||
std::vector<float> DeviceManager::readWavelengthCalibratedFile(const string &serialNumber) {
|
||||
std::vector<float> result;
|
||||
std::string filename = "wavelength_calibrated_files/wavelength_calibrated_" + serialNumber + ".txt";
|
||||
std::ifstream file(filename);
|
||||
|
||||
if (!file.is_open()) {
|
||||
throw std::runtime_error("无法打开文件: " + filename);
|
||||
}
|
||||
qDebug() << "成功读取波长校准文件:" + QString::fromStdString(filename);
|
||||
float value;
|
||||
while (file >> value) {
|
||||
result.push_back(value);
|
||||
}
|
||||
// 检查读取过程是否出错(排除正常到达文件末尾的情况)
|
||||
if (!file.eof() && file.fail()) {
|
||||
throw std::runtime_error("文件格式错误: " + filename);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<Spectrometer*> DeviceManager::DetectDeviceLst() //检测设备列表,并激活(打开设备)
|
||||
{
|
||||
qDebug() << "检测设备列表,并激活(打开设备)" << endl;
|
||||
// 如果设备列表不为空,直接返回缓存的设备列表
|
||||
if(m_pDeviceLst.size() > 0){
|
||||
qDebug() << "返回已缓存的设备列表,设备数量:" << m_pDeviceLst.size();
|
||||
return m_pDeviceLst;
|
||||
}
|
||||
qDebug() << "首次检测设备,开始连接...";
|
||||
m_pZio->connectDevice();
|
||||
m_nDeviceNum = m_pZio->getConnectedDeviceCount();
|
||||
// 确保列表为空(防止重复添加
|
||||
m_pDeviceLst.clear();
|
||||
|
||||
for(int i = 0; i < m_nDeviceNum; i++){
|
||||
SpectrometerParameters params = m_pZio->getDevice(i)->getParameters();
|
||||
|
||||
Spectrometer* device = new Spectrometer();
|
||||
device->m_serialNumber = params.serialNumber;
|
||||
device->m_handle = i;
|
||||
|
||||
try
|
||||
{
|
||||
std::vector<float> wavelengthListFixed = readWavelengthCalibratedFile(device->m_serialNumber);
|
||||
if (wavelengthListFixed.empty()) {
|
||||
qDebug() << "波长未校准,光谱仪序列号=" + QString::fromStdString(device->m_serialNumber);
|
||||
device->m_wavelengthList.assign(params.wavelengths.begin(), params.wavelengths.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
device->m_wavelengthList.assign(wavelengthListFixed.begin(), wavelengthListFixed.end());
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
qDebug() << "波长未校准,光谱仪序列号=" + QString::fromStdString(device->m_serialNumber);
|
||||
}
|
||||
|
||||
m_pDeviceLst.push_back(device);
|
||||
qDebug() << "创建设备:" << QString::fromStdString(device->m_serialNumber) << ",句柄:" << i;
|
||||
pixelCount += device->m_wavelengthList.size();
|
||||
}
|
||||
|
||||
if(m_nDeviceNum > 0){
|
||||
qDebug() << "设置触发模式";
|
||||
m_pZio->setTriggerMode(TriggerMode::SOFTWARE);
|
||||
}
|
||||
|
||||
qDebug() << "设备检测完成,总共创建" << m_nDeviceNum << "个设备";
|
||||
|
||||
return m_pDeviceLst;
|
||||
}
|
||||
|
||||
void DeviceManager::DeactivateDeviceLst() //关闭所有设备
|
||||
{
|
||||
qDebug() << "关闭所有设备,当前设备数量:" << m_pDeviceLst.size();
|
||||
|
||||
// 先断开设备连接
|
||||
if(m_pZio) {
|
||||
m_pZio->disconnectDevice();
|
||||
}
|
||||
|
||||
// 删除所有设备对象
|
||||
for(auto& device : m_pDeviceLst){
|
||||
if(device != nullptr) {
|
||||
qDebug() << "删除设备:" << QString::fromStdString(device->m_serialNumber);
|
||||
delete device;
|
||||
device = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// 清空容器
|
||||
m_pDeviceLst.clear();
|
||||
m_nDeviceNum = 0;
|
||||
|
||||
qDebug() << "所有设备已关闭并清理";
|
||||
}
|
||||
|
||||
void DeviceManager::onDetectDeviceLst()
|
||||
{
|
||||
DetectDeviceLst();
|
||||
emit DetectDeviceFinish(m_pDeviceLst);
|
||||
}
|
||||
|
||||
void DeviceManager::CloseAllDevice()
|
||||
{
|
||||
m_pZio->disconnectDevice();
|
||||
}
|
||||
|
||||
//获取设备列表
|
||||
std::vector<Spectrometer*> DeviceManager::GetDeviceList()
|
||||
{
|
||||
if(m_pDeviceLst.size() == 0){
|
||||
return DetectDeviceLst();
|
||||
}
|
||||
return m_pDeviceLst;
|
||||
}
|
||||
|
||||
void DeviceManager::stopMeasure()
|
||||
{
|
||||
m_pZio->stopMeasurement();
|
||||
}
|
||||
|
||||
int DeviceManager::StartMeasure(int integrationTime, int unNrAverages)
|
||||
{
|
||||
m_pZio->startMeasurement(this, integrationTime, unNrAverages);
|
||||
bStartMeasure=true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void DeviceManager::onDataReceived(std::string spectrumSerialNumber,
|
||||
const std::vector<float> &wavelengthList,
|
||||
const std::vector<float> &intensityList)
|
||||
{
|
||||
QVector<float> qWavelengthList = QVector<float>::fromStdVector(wavelengthList);
|
||||
QVector<float> qIntensityList = QVector<float>::fromStdVector(intensityList);
|
||||
emit spectrumDataReady(QString::fromStdString(spectrumSerialNumber),
|
||||
qWavelengthList,
|
||||
qIntensityList);
|
||||
}
|
||||
|
||||
void DeviceManager::onDataReceivedBanch(std::string spectrumSerialNumber,
|
||||
const std::vector<float> &wavelengthList,
|
||||
const std::vector<std::vector<float>> &spectrumList)
|
||||
{
|
||||
// 批量模式:逐帧转成单帧信号发出,界面处理逻辑无需改动
|
||||
for (const auto &intensityList : spectrumList) {
|
||||
onDataReceived(spectrumSerialNumber, wavelengthList, intensityList);
|
||||
}
|
||||
}
|
||||
|
||||
void DeviceManager::onError(std::string deviceIndex, const string &error)
|
||||
{
|
||||
qDebug() << "光谱仪出错了" << "deviceIndex=" << QString::fromStdString(deviceIndex)
|
||||
<< "error is " << QString::fromStdString(error);
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
#ifndef DEVICEMANAGER_H
|
||||
#define DEVICEMANAGER_H
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "../Public/util.h"
|
||||
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include "../Spectrometer/spectrometer.h"
|
||||
|
||||
#ifdef CAVITY_USE_ARIS_BRIDGE
|
||||
// MSVC 构建:通过 MinGW 编译的 aris_bridge.dll 驱动真实 ARIS SDK(纯 C 接口)
|
||||
#include "arisbridge.h"
|
||||
#else
|
||||
#include "spectrometercallback.h"
|
||||
#endif
|
||||
|
||||
namespace std
|
||||
{
|
||||
//class vector;
|
||||
}
|
||||
|
||||
#ifndef CAVITY_USE_ARIS_BRIDGE
|
||||
class ZioLinkProtocol;
|
||||
#endif
|
||||
|
||||
// 两种后端:
|
||||
// * CAVITY_USE_ARIS_BRIDGE(MSVC + aris_bridge.dll):不继承 SDK 的回调类
|
||||
// * 否则直接链接 ARIS SDK(MinGW)或兼容桩(aris_stub.dll)
|
||||
#ifdef CAVITY_USE_ARIS_BRIDGE
|
||||
class DeviceManager : public QObject
|
||||
#else
|
||||
class DeviceManager : public QObject, public SpectrometerCallback
|
||||
#endif
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
DeviceManager(QObject *parent = nullptr);
|
||||
~DeviceManager();
|
||||
|
||||
std::vector<float> readWavelengthCalibratedFile(const string &serialNumber);
|
||||
std::vector<Spectrometer*> DetectDeviceLst(); //检测设备列表,并激活(打开设备)
|
||||
void CloseAllDevice(); //关闭所有的设备
|
||||
|
||||
std::vector<Spectrometer*> GetDeviceList();//获取设备列表
|
||||
|
||||
static unsigned int m_nDeviceNum ; //实际已连接的设备数量
|
||||
|
||||
static std::vector<Spectrometer*> m_pDeviceLst;
|
||||
|
||||
int pixelCount;
|
||||
|
||||
private:
|
||||
|
||||
bool IsAllDevicesReady(); //检测所有设备是否都已准备就绪
|
||||
void DeactivateDeviceLst(); //关闭所有设备
|
||||
|
||||
// 用波长校准文件覆盖设备的波长表;文件缺失/格式错误时退回设备自带波长
|
||||
void loadDeviceWavelengths(Spectrometer *device, const std::vector<float> &fallback);
|
||||
|
||||
// 把光谱数据转成 Qt 信号发出去(可能来自设备的采集线程)
|
||||
void handleData(const std::string &serialNumber,
|
||||
const std::vector<float> &wavelengthList,
|
||||
const std::vector<float> &intensityList);
|
||||
|
||||
bool bStartMeasure;
|
||||
bool stopscanning = true;
|
||||
|
||||
int iRecvCnt;
|
||||
int iCurExecTimes;
|
||||
int iDataLen;
|
||||
|
||||
#ifdef CAVITY_USE_ARIS_BRIDGE
|
||||
// 桥接 DLL 的数据回调(静态,转给当前实例)
|
||||
static void onBridgeData(const std::string &serialNumber,
|
||||
const std::vector<float> &wavelengthList,
|
||||
const std::vector<float> &intensityList);
|
||||
#else
|
||||
ZioLinkProtocol* m_pZio;
|
||||
#endif
|
||||
|
||||
public slots:
|
||||
|
||||
void onDetectDeviceLst();
|
||||
|
||||
#ifndef CAVITY_USE_ARIS_BRIDGE
|
||||
void onDataReceived(std::string deviceIndex,const std::vector<float> &wavelength,
|
||||
const std::vector<float> &spectrum) override;
|
||||
|
||||
// 批量测量回调(ARIS 0411 版 SDK 新增的纯虚函数,必须实现,否则本类仍是抽象类)
|
||||
void onDataReceivedBanch(std::string deviceIndex,const std::vector<float> &wavelength,
|
||||
const std::vector<std::vector<float>> &spectrum) override;
|
||||
|
||||
// 回调函数,当发生错误时被调用
|
||||
void onError(std::string,
|
||||
const std::string& error) override ;
|
||||
#endif
|
||||
|
||||
int StartMeasure(int integrationTime, int nrAverages);
|
||||
void stopMeasure();
|
||||
|
||||
signals:
|
||||
void DetectDeviceFinish(std::vector<Spectrometer*> m_pDeviceLst);
|
||||
|
||||
|
||||
void spectrumDataReady(QString deviceIndex,
|
||||
const QVector<float>& wavelengthList,
|
||||
const QVector<float>& spectrumList);
|
||||
};
|
||||
|
||||
#endif // DEVICEMANAGER_H
|
||||
@@ -0,0 +1,81 @@
|
||||
#ifndef DEVICEMANAGER_H
|
||||
#define DEVICEMANAGER_H
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "../Public/util.h"
|
||||
|
||||
#include <QtSerialPort/QSerialPort>
|
||||
#include <QtSerialPort/QSerialPortInfo>
|
||||
#include "spectrometercallback.h"
|
||||
#include "../Spectrometer/spectrometer.h"
|
||||
|
||||
namespace std
|
||||
{
|
||||
//class vector;
|
||||
}
|
||||
|
||||
class ZioLinkProtocol;
|
||||
|
||||
|
||||
class DeviceManager : public QObject, public SpectrometerCallback
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
||||
DeviceManager(QObject *parent = nullptr);
|
||||
~DeviceManager();
|
||||
|
||||
std::vector<float> readWavelengthCalibratedFile(const string &serialNumber);
|
||||
std::vector<Spectrometer*> DetectDeviceLst(); //检测设备列表,并激活(打开设备)
|
||||
void CloseAllDevice(); //关闭所有的设备
|
||||
|
||||
std::vector<Spectrometer*> GetDeviceList();//获取设备列表
|
||||
|
||||
static unsigned int m_nDeviceNum ; //实际已连接的设备数量
|
||||
|
||||
static std::vector<Spectrometer*> m_pDeviceLst;
|
||||
|
||||
int pixelCount;
|
||||
|
||||
private:
|
||||
|
||||
bool IsAllDevicesReady(); //检测所有设备是否都已准备就绪
|
||||
void DeactivateDeviceLst(); //关闭所有设备
|
||||
|
||||
bool bStartMeasure;
|
||||
bool stopscanning = true;
|
||||
|
||||
int iRecvCnt;
|
||||
int iCurExecTimes;
|
||||
int iDataLen;
|
||||
|
||||
ZioLinkProtocol* m_pZio;
|
||||
|
||||
public slots:
|
||||
|
||||
void onDetectDeviceLst();
|
||||
|
||||
void onDataReceived(std::string deviceIndex,const std::vector<float> &wavelength,
|
||||
const std::vector<float> &spectrum) override;
|
||||
|
||||
// 批量测量回调(ARIS 0411 版 SDK 新增的纯虚函数,必须实现,否则本类仍是抽象类)
|
||||
void onDataReceivedBanch(std::string deviceIndex,const std::vector<float> &wavelength,
|
||||
const std::vector<std::vector<float>> &spectrum) override;
|
||||
|
||||
// 回调函数,当发生错误时被调用
|
||||
void onError(std::string,
|
||||
const std::string& error) override ;
|
||||
|
||||
int StartMeasure(int integrationTime, int nrAverages);
|
||||
void stopMeasure();
|
||||
|
||||
signals:
|
||||
void DetectDeviceFinish(std::vector<Spectrometer*> m_pDeviceLst);
|
||||
|
||||
|
||||
void spectrumDataReady(QString deviceIndex,
|
||||
const QVector<float>& wavelengthList,
|
||||
const QVector<float>& spectrumList);
|
||||
};
|
||||
|
||||
#endif // DEVICEMANAGER_H
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,224 @@
|
||||
#ifndef ELEMENTANALYSIS_H
|
||||
#define ELEMENTANALYSIS_H
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include "qobject.h"
|
||||
#include "algorithmanalysis.h"
|
||||
#include <iomanip>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 定义峰值结构体
|
||||
struct Peak {
|
||||
int index;
|
||||
double intensity;
|
||||
double wavelength;
|
||||
string name;
|
||||
};
|
||||
|
||||
struct WaveSpectrum {
|
||||
double intensity;
|
||||
double wavelength;
|
||||
|
||||
};
|
||||
|
||||
//定义元素条数结构体
|
||||
struct TTname{
|
||||
// 元素名称
|
||||
string Telementname;
|
||||
int lines = 0;
|
||||
};
|
||||
|
||||
|
||||
//定义元素、强度结构体
|
||||
//struct ElementPeak {
|
||||
// string elementName;
|
||||
// double intensity;
|
||||
//};
|
||||
|
||||
// 定义数据点结构体
|
||||
struct DataPoint {
|
||||
double concentration;
|
||||
double intensity;
|
||||
};
|
||||
|
||||
// 定义拟合参数结构体
|
||||
struct FitParameters {
|
||||
double slope;
|
||||
double intercept;
|
||||
};
|
||||
|
||||
// 定义元素含量结构体
|
||||
struct SpectrumElementQuantity {
|
||||
string name;
|
||||
double quantity;
|
||||
double percent;// CFLibs计算的百分比
|
||||
|
||||
};
|
||||
|
||||
|
||||
// 光谱数据
|
||||
struct SpecData{
|
||||
// 元素名称
|
||||
string eleName;
|
||||
// 波长
|
||||
double waveLength = 0;
|
||||
// 强度
|
||||
double intensity = 0;
|
||||
// 跃升能级
|
||||
double transpro;
|
||||
// 上能级
|
||||
double upplevel;
|
||||
// 权重
|
||||
double statis;
|
||||
// 0=原子,1=离子
|
||||
bool ionization;
|
||||
|
||||
bool operator==(const SpecData& other) const {
|
||||
return eleName == other.eleName; // 按照name字段进行去重
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
// 元素数据
|
||||
struct ElementData{
|
||||
// 元素名称
|
||||
string eleName;
|
||||
// 原子数量
|
||||
int atomNum = 0;
|
||||
// 离子数量
|
||||
int ionNum = 0;
|
||||
|
||||
// cs原子数量
|
||||
int atomNumCS = 0;
|
||||
// cs离子数量
|
||||
int ionNumCS = 0;
|
||||
|
||||
// 原子截距
|
||||
double bsa = 0;
|
||||
// 离子截距
|
||||
double bsi = 0;
|
||||
|
||||
|
||||
// 原子的cs
|
||||
double csa = 0;
|
||||
|
||||
// 离子的cs
|
||||
double csi = 0;
|
||||
|
||||
double csit = 0;
|
||||
|
||||
// 含量比例:未乘以100
|
||||
double abundence = 0.0;
|
||||
|
||||
|
||||
};
|
||||
// 波长修正数据
|
||||
struct FixData{
|
||||
double wavelength;
|
||||
double fix;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class ElementAnalysis : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ElementAnalysis(QObject *parent = nullptr);
|
||||
vector<SpectrumElementQuantity> analysisElementQuantity(vector<pair<int,vector<SpectrumChannelData>>>);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
//最小二乘法拟合所需要的斜率和截距
|
||||
FitParameters fitCurve(string elementName);
|
||||
|
||||
|
||||
// 分割字符串为数组
|
||||
vector<string> split(string str, char del);
|
||||
|
||||
// 2 输入 比对库:200行,7列,输出 到比对库的数据
|
||||
vector<SpecData> findCompareLst(string compareFileName);
|
||||
|
||||
// 3 输入 1中波长和2中波长,输出 差值小于0.02的波长(2中的波长值), 1中的强度,2中的其他字段
|
||||
vector<SpecData> doCompare(vector<SpectrumChannelData> peakLst,vector<SpecData> &compareLst,double distance);
|
||||
|
||||
// 4 输入 3中的差值比较后的光谱数据,修正数据库,输出 修正后的强度
|
||||
void fixIntensity(vector<SpecData> &comparedLst,vector<FixData> fixLst, double baseline);
|
||||
|
||||
// 5 输入 3中fixedLst为修正完的元素数据,casee=计算方式,输出 用函数计算后的温度/密度
|
||||
void calculateElementTempne(vector<SpecData> fixedLst,string Telement,int casee,int count7);
|
||||
|
||||
//获取平均温度和电离子密度
|
||||
void getTTMM(vector<TTname> Telementname,vector<SpecData> comparedLst);
|
||||
|
||||
// 6 输入 温度/密度/强度/元素种类 输出用函数计算后的含量
|
||||
void calculateConcentration(vector<SpecData> &tempneLst,vector<ElementData> &elementLst);
|
||||
|
||||
// 根据截距计算元素含量
|
||||
double calculateConcentrationByIntercept(double inten,double transpro,double upplevel,double statis,double temprature);
|
||||
|
||||
// 根据CS计算元素含量
|
||||
double calculateConcentrationByCS(string element,double temprature,int ionization,double electroD,double bs);
|
||||
|
||||
// 计算U
|
||||
double calculateU(string element, double temprature, int ionization);
|
||||
|
||||
// 计算saha
|
||||
double saha(string element,double temprature,double electroD,double csa);
|
||||
|
||||
// 从光谱数据中获取到元素名称集合
|
||||
vector<ElementData> getElementLst(vector<SpecData> peakLst);
|
||||
|
||||
//获取元素的离子和原子条数
|
||||
vector<TTname> GetTTname(vector<ElementData> elementLst,vector<SpecData> comparedLst);
|
||||
|
||||
// 获取修正数据
|
||||
vector<FixData> getFixLst(int gratingNo);
|
||||
|
||||
//用曲线拟合计算元素ppm
|
||||
vector<SpectrumElementQuantity> calculateElementConcentrationByStandardCurve(vector<SpectrumElementPeak> peakLst);
|
||||
|
||||
//计算元素含量百分比
|
||||
vector<SpectrumElementQuantity> calculateElementConcentrationByCFLibs(vector<SpectrumChannelData> spectVector);
|
||||
|
||||
static vector<pair<int,vector<SpectrumChannelData>>> channelData;
|
||||
|
||||
// 温度
|
||||
double temprature = 0;
|
||||
// 密度
|
||||
double electroD = 0;
|
||||
|
||||
//删除重叠区域的峰值
|
||||
vector<SpectrumChannelData> selectWavelength(vector<pair<int, vector<SpectrumChannelData>>> peakVector, int inSpacing);
|
||||
|
||||
|
||||
//vector<double> background(vector<double> waveLst,vector<double> averageSpecLst);
|
||||
|
||||
|
||||
|
||||
bool isCompletedCalculateByCFLibs = false;
|
||||
bool isCompletedCalculateByStandardCurve = false;
|
||||
|
||||
std::string createErrorMessage(int errorCode, const std::string& message);
|
||||
|
||||
|
||||
|
||||
void save(vector<SpectrumChannelData> peakVector);
|
||||
|
||||
void saveSpecData(vector<SpecData> comparedLst);
|
||||
|
||||
void saveTTname(vector<TTname> Telementname);
|
||||
|
||||
void saveElementLst(vector<ElementData> elementLst);
|
||||
};
|
||||
|
||||
#endif // ELEMENTANALYSIS_H
|
||||
@@ -0,0 +1,884 @@
|
||||
#include "specpeakservice.h"
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <numeric>
|
||||
#include <set>
|
||||
#include <cmath>
|
||||
#include <time.h>
|
||||
#include <set>
|
||||
#include <QDebug>
|
||||
|
||||
SpecPeakService::SpecPeakService(){
|
||||
|
||||
|
||||
|
||||
}
|
||||
vector<int> SpecPeakService::SpecPeak(vector<SpectrumChannelData> channelData)
|
||||
{
|
||||
//qDebug() << "[Logic]-用SpecPeakService类寻峰" << endl;
|
||||
|
||||
background_ptp = 0.2;
|
||||
|
||||
//将获取的数据进行修改
|
||||
spectralPretreatment(channelData);
|
||||
|
||||
//获取峰值下标集合mergeIntervalIndexStruct
|
||||
getRes();
|
||||
|
||||
//这里必须使mergeIntervalIndexStruct不为空,为下一步提供变量
|
||||
//第一次为空就将最大波动值background_ptp下降0.03
|
||||
//最大波动值background_ptp下降到0.03还没计算出跳出
|
||||
while(mergeIntervalIndexStruct.merge_interval_info.empty() &&
|
||||
mergeIntervalIndexStruct.merge_interval_index.empty()){
|
||||
if(background_ptp < 0.03){
|
||||
break;
|
||||
}
|
||||
background_ptp = background_ptp - 0.03;
|
||||
getRes();
|
||||
|
||||
}
|
||||
|
||||
//最大波动值background_ptp直接赋值0.005得到峰值下标
|
||||
//或者再次计算获取峰值下标
|
||||
if(mergeIntervalIndexStruct.merge_interval_info.empty() &&
|
||||
mergeIntervalIndexStruct.merge_interval_index.empty()){
|
||||
background_ptp = 0.005;
|
||||
getRes();
|
||||
vector<int> mergedIndex = mapInvertInt(mergeIntervalIndexStruct);
|
||||
return mergedIndex;
|
||||
}else {
|
||||
double temp = get_background_wave(mergeIntervalIndexStruct);
|
||||
background_ptp = temp;
|
||||
getRes();
|
||||
vector<int> mergedIndex = mapInvertInt(mergeIntervalIndexStruct);
|
||||
|
||||
return mergedIndex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//获取数据集合(测试所用)
|
||||
void SpecPeakService::initSpecWavelengthStrength()
|
||||
{
|
||||
string filename = "../algorithmDemo/yuan.txt";
|
||||
ifstream file(filename);
|
||||
if(!file.is_open()){
|
||||
cerr << "Error:File not found:" << filename <<endl;
|
||||
}
|
||||
|
||||
file.clear();
|
||||
file.seekg(0,ios::beg);
|
||||
|
||||
double wave = 0;
|
||||
double inten = 0;
|
||||
while (file >> wave >> inten)
|
||||
{
|
||||
wavelength.push_back(wave);
|
||||
intensity.push_back(inten);
|
||||
}
|
||||
|
||||
file.close();
|
||||
|
||||
wavelength.erase(wavelength.begin(), wavelength.begin() + 16);
|
||||
wavelength.erase(wavelength.end() - 16, wavelength.end());
|
||||
|
||||
intensity.erase(intensity.begin(), intensity.begin() + 16);
|
||||
intensity.erase(intensity.end() - 16, intensity.end());
|
||||
|
||||
}
|
||||
|
||||
//计算归一化的绝对值
|
||||
vector<double> SpecPeakService::absMaxNorm(vector<double> specList)
|
||||
{
|
||||
//qDebug() << "[Logic]-计算光谱数据归一化的绝对值" << endl;
|
||||
auto max_abs_iter = std::max_element(specList.begin(), specList.end(), [](double a, double b) {
|
||||
return std::abs(a) < std::abs(b);
|
||||
});
|
||||
double max_value = abs (*max_abs_iter);
|
||||
|
||||
vector<double> norm_res;
|
||||
norm_res.resize(specList.size());
|
||||
|
||||
std::transform(specList.begin(), specList.end(), norm_res.begin(), [&max_value](double x)
|
||||
{
|
||||
return x / max_value;
|
||||
});
|
||||
|
||||
return norm_res;
|
||||
}
|
||||
|
||||
//判断两条线是否相交
|
||||
bool SpecPeakService::isIntersect(double leftY1, double leftY2, double rightY1, double rightY2)
|
||||
{
|
||||
// qDebug() << "[Logic]-判断两条线是否相交" << endl;
|
||||
|
||||
if ((leftY1 < leftY2) && (rightY1 < rightY2))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if ((leftY1 > leftY2) && (rightY1 > rightY2))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//求导数以及绝对值 归一化
|
||||
void SpecPeakService::spectralPretreatment(vector<SpectrumChannelData> channelData)
|
||||
{
|
||||
qDebug() << "[Logic]-求导数以及绝对值" << endl;
|
||||
wavelength.clear();
|
||||
intensity.clear();
|
||||
|
||||
for (int i = 0; i < channelData.size() ; i++) {
|
||||
wavelength.push_back(channelData[i].wavelength);
|
||||
intensity.push_back(channelData[i].intensity);
|
||||
}
|
||||
d1 = diff(wavelength, intensity, 1);
|
||||
d1_2.resize(d1.size());
|
||||
std::transform(d1.begin(), d1.end(), d1_2.begin(), [](double x) {
|
||||
return x * x;
|
||||
});
|
||||
|
||||
d1_abs.resize(d1.size());
|
||||
std::transform(d1.begin(), d1.end(), d1_abs.begin(), [](double x) { return abs(x); });
|
||||
|
||||
d2 = diff(wavelength, intensity, 2);
|
||||
d2_2.resize(d1.size());
|
||||
std::transform(d2.begin(), d2.end(), d2_2.begin(), [](double x) { return x * x; });
|
||||
|
||||
d2_abs.resize(d1.size());
|
||||
std::transform(d2.begin(), d2.end(), d2_abs.begin(), [](double x) { return abs(x); });
|
||||
|
||||
//
|
||||
intensityNorm = absMaxNorm(intensity);
|
||||
d1_norm = absMaxNorm(d1);
|
||||
d1_2_norm = absMaxNorm(d1_2);
|
||||
d1_abs_norm = absMaxNorm(d1_abs);
|
||||
d2_norm = absMaxNorm(d2);
|
||||
d2_2_norm = absMaxNorm(d2_2);
|
||||
d2_abs_norm = absMaxNorm(d2_abs);
|
||||
intensity_smooth = smooth(intensityNorm, 5);
|
||||
}
|
||||
|
||||
//差分
|
||||
vector<double> SpecPeakService::diff(vector<double> xValueList, vector<double> yValueList, int n )
|
||||
{
|
||||
//qDebug() << "[Logic]-计算差分" << endl;
|
||||
if (n == 0)
|
||||
{
|
||||
return yValueList;
|
||||
}
|
||||
else
|
||||
{
|
||||
vector<double> diff_res;
|
||||
for (int i = 0; i < xValueList.size(); i++)
|
||||
{
|
||||
double temp;
|
||||
if (i == 0)//
|
||||
{
|
||||
temp = (yValueList[i] - yValueList[i + 1]) * 1.0 / (xValueList[i] - xValueList[i + 1]);
|
||||
}
|
||||
else if (i == xValueList.size() - 1)//
|
||||
{
|
||||
temp = (yValueList[i - 1] - yValueList[i]) * 1.0 / (xValueList[i - 1] - xValueList[i]);
|
||||
}
|
||||
else //
|
||||
{
|
||||
temp = (yValueList[i - 1] - yValueList[i + 1]) * 1.0 / (xValueList[i - 1] - xValueList[i + 1]);
|
||||
}
|
||||
diff_res.push_back(temp);
|
||||
}
|
||||
|
||||
return diff(xValueList, diff_res, n - 1);
|
||||
}
|
||||
}
|
||||
|
||||
//找出特征波长节点
|
||||
vector<int> SpecPeakService::findFeatureIndex()
|
||||
{
|
||||
//qDebug() << "[Logic]-找出特征波长节点" << endl;
|
||||
|
||||
vector<int> features_index;
|
||||
for (int i = 0 ; i < d2_norm.size() - 1; i++)
|
||||
{
|
||||
if (d2_norm[i] * d2_norm[i + 1] < 0)
|
||||
{
|
||||
|
||||
if (intensityNorm[i] < intensityNorm[i + 1] )
|
||||
{
|
||||
if (!std::binary_search(features_index.begin(), features_index.end(), i))
|
||||
{
|
||||
features_index.push_back(i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
features_index.push_back(i + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return features_index;
|
||||
|
||||
}
|
||||
|
||||
//去除连续波长节点中中间的节点
|
||||
vector<int> SpecPeakService::featureIndexDelMiddle(vector<int> featuresIndexList)
|
||||
{
|
||||
//qDebug() << "[Logic]-去除连续波长节点中中间的节点" << endl;
|
||||
vector<double> del_index_1;
|
||||
vector<double> del_index_2;
|
||||
|
||||
|
||||
for (int i = 1 ; i < featuresIndexList.size() - 1; i++ )
|
||||
{
|
||||
|
||||
if (featuresIndexList[i] + 1 == featuresIndexList[i + 1])
|
||||
{
|
||||
del_index_1.push_back(featuresIndexList[i]);
|
||||
}
|
||||
|
||||
if (featuresIndexList[i] - 1 == featuresIndexList[i - 1])
|
||||
{
|
||||
del_index_2.push_back(featuresIndexList[i]);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
vector<double> del_common_index;
|
||||
//
|
||||
set<double> setDel_index_1(del_index_1.begin(), del_index_1.end());
|
||||
set<double> setDel_index_2(del_index_2.begin(), del_index_2.end());
|
||||
//
|
||||
std::set_intersection(setDel_index_1.begin(), setDel_index_1.end(),
|
||||
setDel_index_2.begin(), setDel_index_2.end(), std::inserter(del_common_index, del_common_index.begin()));
|
||||
|
||||
//
|
||||
vector<int> res_features_index(featuresIndexList.begin(), featuresIndexList.end());
|
||||
for (int i = 0 ; i < res_features_index.size(); i++)
|
||||
{
|
||||
if (binary_search(del_common_index.begin(), del_common_index.end(), res_features_index[i] ))
|
||||
{
|
||||
res_features_index[i] = -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
res_features_index.erase(remove(res_features_index.begin(), res_features_index.end(), -1), res_features_index.end());
|
||||
|
||||
|
||||
while (binary_search(res_features_index.begin(), res_features_index.end(), -1))
|
||||
{
|
||||
//
|
||||
}
|
||||
//
|
||||
std::sort(res_features_index.begin(), res_features_index.end());
|
||||
|
||||
return res_features_index;
|
||||
}
|
||||
|
||||
//从相邻的波长节点中选择一个
|
||||
vector<int> SpecPeakService::chooseFeatureIndex2of1(vector<int> featuresIndexList)
|
||||
{
|
||||
/*
|
||||
|
||||
Args:
|
||||
featuresIndexList
|
||||
Returns:
|
||||
|
||||
*/
|
||||
|
||||
//qDebug() << "[Logic]-从相邻的波长节点中选择一个" << endl;
|
||||
vector<int> res_features_index;
|
||||
bool pass_flag = false;
|
||||
for (int i = 0; i < featuresIndexList.size() - 1; i++)
|
||||
{
|
||||
if (pass_flag)
|
||||
{
|
||||
pass_flag = false;
|
||||
continue;
|
||||
}
|
||||
//
|
||||
if (featuresIndexList[i] + 1 == featuresIndexList[i + 1])
|
||||
{
|
||||
pass_flag = true;
|
||||
if (intensityNorm[featuresIndexList[i]] < intensityNorm[featuresIndexList[i + 1]])
|
||||
{
|
||||
res_features_index.push_back(featuresIndexList[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
res_features_index.push_back(featuresIndexList[i + 1]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
res_features_index.push_back(featuresIndexList[i]);
|
||||
}
|
||||
|
||||
}
|
||||
return res_features_index;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//将特征波长节点转化为区间索引,并进行条件判断,筛选出满足条件的区间
|
||||
IntervalIndexStruct SpecPeakService::getIntervalIndex(vector<int> featuresIndex)
|
||||
{
|
||||
//qDebug() << "[Logic]-将特征波长节点转化为区间索引,并进行条件判断,筛选出满足条件的区间" << endl;
|
||||
vector<vector<int>> init_interval_index; //
|
||||
vector<vector<string>> init_interval_info; //
|
||||
|
||||
vector<vector<int>> interval_index;
|
||||
//
|
||||
for (int i = 0; i < featuresIndex.size() - 1; i++)
|
||||
{
|
||||
int min_index = featuresIndex[i];
|
||||
int max_index = featuresIndex[i + 1];
|
||||
vector<double> temp_intensity_norm(intensityNorm.begin() + min_index, intensityNorm.begin() + max_index + 1);
|
||||
vector<double> temp_d1_norm(d1_norm.begin() + min_index, d1_norm.begin() + max_index + 1);
|
||||
vector<double> temp_d2_norm(d2_norm.begin() + min_index, d2_norm.begin() + max_index + 1);
|
||||
|
||||
// int temp_d1_min_index = std::distance(temp_d1_norm.begin(), std::min_element(temp_d1_norm.begin(), temp_d1_norm.end()));
|
||||
// int temp_d2_min_index = std::distance(temp_d2_norm.begin(), std::min_element(temp_d2_norm.begin(), temp_d2_norm.end()));
|
||||
int temp_max_index = std::distance(temp_intensity_norm.begin(), std::max_element(temp_intensity_norm.begin(), temp_intensity_norm.end()));
|
||||
int interval_max_index = min_index + temp_max_index;
|
||||
vector<string> info;
|
||||
|
||||
//一阶导的最小值在二阶导最小值的右侧
|
||||
// bool flag_1;
|
||||
// if (temp_d1_min_index > temp_d2_min_index)
|
||||
// {
|
||||
// flag_1 = true;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// flag_1 = false;
|
||||
// }
|
||||
|
||||
//最大值在区间的中间
|
||||
bool flag_2;
|
||||
if (0 < temp_max_index && temp_max_index < max_index - min_index)
|
||||
{
|
||||
flag_2 = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
flag_2 = false;
|
||||
}
|
||||
|
||||
//一阶导的最大值在二阶导最大值的右侧
|
||||
bool flag_3;
|
||||
if (temp_d1_norm[temp_max_index] > temp_d2_norm[temp_max_index])
|
||||
{
|
||||
flag_3 = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
flag_3 = true;
|
||||
}
|
||||
|
||||
//区间的点的数量必须大于3
|
||||
// bool flag_4;
|
||||
// if (max_index - min_index > 2)
|
||||
// {
|
||||
// flag_4 = true;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// flag_4 = false;
|
||||
// }
|
||||
|
||||
//曲线是不是先上升后下降
|
||||
bool flag_5;
|
||||
vector<double> temp_list(1, 1);
|
||||
|
||||
for(int k = 0; k < (max_index - min_index); k++ ){
|
||||
if(temp_intensity_norm[k] < temp_intensity_norm[k + 1]){
|
||||
if(temp_list.back() == 0){
|
||||
temp_list.push_back(1);
|
||||
}
|
||||
}else{
|
||||
if(temp_list.back() == 1){
|
||||
temp_list.push_back(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(temp_list.size() > 2){
|
||||
flag_5 = false;
|
||||
}else{
|
||||
flag_5 = true;
|
||||
}
|
||||
|
||||
// if (std::binary_search(kmeansIndex.begin(), kmeansIndex.end(), i))
|
||||
// {
|
||||
// flag_5 = true;
|
||||
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// flag_5 = false;
|
||||
// }
|
||||
|
||||
|
||||
//平滑前的点大于平滑后的点,且至少有三个点在平滑后的点的上方
|
||||
bool flag_6;
|
||||
|
||||
vector<int> condition;
|
||||
if(intensityNorm[interval_max_index] > intensity_smooth[interval_max_index])
|
||||
{
|
||||
condition.push_back(1);
|
||||
}else{
|
||||
condition.push_back(0);
|
||||
}
|
||||
for(int j = 1; j < 3; j++)
|
||||
{
|
||||
if(interval_max_index - j < 0 || interval_max_index + j > intensityNorm.size() - 1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if(intensityNorm[interval_max_index - j] < intensityNorm[interval_max_index])
|
||||
{
|
||||
condition.push_back(1);
|
||||
}else{
|
||||
condition.push_back(0);
|
||||
}
|
||||
|
||||
if(intensityNorm[interval_max_index + j] < intensityNorm[interval_max_index])
|
||||
{
|
||||
condition.push_back(1);
|
||||
}else{
|
||||
condition.push_back(0);
|
||||
}
|
||||
}
|
||||
int sum = accumulate(condition.begin(), condition.end(), 0);
|
||||
if(sum == condition.size())
|
||||
{
|
||||
flag_6 = true;
|
||||
}else{
|
||||
flag_6 = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//最大最小值只差大于 指定值
|
||||
bool flag_v1;
|
||||
|
||||
int left_offset = 0;
|
||||
while(true)
|
||||
{
|
||||
if(min_index - left_offset == 0){
|
||||
break;
|
||||
}
|
||||
if(intensityNorm[min_index - left_offset - 1] < intensityNorm[min_index - left_offset]){
|
||||
left_offset += 1;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int right_offset = 0;
|
||||
while(true)
|
||||
{
|
||||
if(max_index + right_offset == int(intensityNorm.size() - 1)){
|
||||
break;
|
||||
}
|
||||
if(intensityNorm[max_index + right_offset + 1] < intensityNorm[max_index + right_offset]){
|
||||
right_offset += 1;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
vector<double> temp_data1(intensityNorm.begin() + min_index - left_offset,
|
||||
intensityNorm.begin() + max_index + right_offset + 1);
|
||||
// for(int k = min_index - left_offset; k < max_index + right_offset + 1; k++)
|
||||
// {
|
||||
// temp_data1.push_back(intensityNorm[k]);
|
||||
// }
|
||||
|
||||
if (*std::max_element(temp_data1.begin(), temp_data1.end()) -
|
||||
*std::min_element(temp_data1.begin(), temp_data1.end()) > background_ptp)
|
||||
{
|
||||
flag_v1 = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
flag_v1 = false;
|
||||
}
|
||||
|
||||
|
||||
//一阶导数的方差大于 指定值
|
||||
// bool flag_v2;
|
||||
// if (std::sqrt(getStandardDeviation(temp_d1_norm)) > 0.1)
|
||||
// {
|
||||
// flag_v2 = true;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// flag_v2 = false;
|
||||
// }
|
||||
|
||||
|
||||
//峰的左侧或右侧的最小值不能大于峰高度的0.75倍
|
||||
// bool flag_v3;
|
||||
|
||||
// double temp_prpoprtion = 0.75;
|
||||
// double min_value = *min_element(temp_intensity_norm.begin(), temp_intensity_norm.end());
|
||||
// vector<double> temp_data(temp_intensity_norm.size());
|
||||
// std::transform(temp_intensity_norm.begin(), temp_intensity_norm.end(),temp_data.begin(),[min_value](double val){return val - min_value;});
|
||||
// auto max_it = max_element(temp_data.begin(), temp_data.end());
|
||||
// temp_max_index = std::distance(temp_data.begin(), max_it);
|
||||
// if(temp_max_index == 0 || temp_max_index == temp_data.size() - 1){
|
||||
// flag_v3 = false;
|
||||
// }else{
|
||||
// double temp_left_min = *min_element(temp_data.begin(), temp_data.begin() + temp_max_index);
|
||||
// double temp_right_min = * min_element(temp_data.begin() + temp_max_index, temp_data.end());
|
||||
// if((temp_left_min < temp_data[temp_max_index] * temp_prpoprtion) &&
|
||||
// (temp_right_min < temp_data[temp_max_index] * temp_prpoprtion)){
|
||||
// flag_v3 = true;
|
||||
// }else{
|
||||
// flag_v3 = false;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
init_interval_index.push_back({ min_index, max_index });
|
||||
|
||||
//
|
||||
info.push_back({ flag_2, flag_3, flag_5, flag_6, flag_v1});
|
||||
init_interval_info.push_back(info);
|
||||
|
||||
//
|
||||
if ( flag_2 && flag_3 && flag_5 && flag_6 && flag_v1)
|
||||
{
|
||||
interval_index.push_back({ min_index, max_index });
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return { interval_index, init_interval_index, init_interval_info };
|
||||
|
||||
}
|
||||
|
||||
|
||||
//扩展区间,使得区间包含整个峰
|
||||
//从峰值开始,向左扩展至一阶导和二阶导的第二个交点,向右扩展至一阶导和二阶导的第二个交点
|
||||
ExtendedIntervalIndexStruct SpecPeakService::extendedIntervalIndex(vector<vector<int>> interval_index)
|
||||
{
|
||||
//qDebug() << "[Logic]-展区间,使得区间包含整个峰" << endl;
|
||||
vector<vector<int>> extend_interval_index;
|
||||
vector<map<string,int>> extend_interval_info;
|
||||
|
||||
for (vector<int> v : interval_index)
|
||||
{
|
||||
int min_index = v[0];
|
||||
int max_index = v[1];
|
||||
|
||||
vector<double> temp_intensity_norm(intensityNorm.begin() + min_index, intensityNorm.begin() + max_index + 1);
|
||||
//
|
||||
int temp_intensity_max_index = std::distance(temp_intensity_norm.begin(), std::max_element(temp_intensity_norm.begin(), temp_intensity_norm.end()));
|
||||
//求出最大值在原始数据中的索引
|
||||
int intensity_temp_max_index = min_index + temp_intensity_max_index;
|
||||
|
||||
//向左扩展
|
||||
int intersections_num = 0;
|
||||
int new_min_index = intensity_temp_max_index;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if(new_min_index == 0){
|
||||
break;
|
||||
}
|
||||
double left_y1 = d1_norm[new_min_index - 1];
|
||||
double left_y2 = d2_norm[new_min_index - 1];
|
||||
double right_y1 = d1_norm[new_min_index];
|
||||
double right_y2 = d2_norm[new_min_index];
|
||||
if (isIntersect(left_y1, left_y2, right_y1, right_y2))
|
||||
{
|
||||
intersections_num += 1;
|
||||
}
|
||||
if (intersections_num == 2)
|
||||
{
|
||||
break;
|
||||
}
|
||||
new_min_index -= 1;
|
||||
|
||||
}
|
||||
|
||||
//向右扩展
|
||||
intersections_num = 0;
|
||||
int new_max_index = intensity_temp_max_index;
|
||||
while (true)
|
||||
{
|
||||
if(new_max_index == int(d1_norm.size())){
|
||||
break;
|
||||
}
|
||||
double left_y1 = d1_norm[new_max_index];
|
||||
double left_y2 = d2_norm[new_max_index];
|
||||
double right_y1 = d1_norm[new_max_index + 1];
|
||||
double right_y2 = d2_norm[new_max_index + 1];
|
||||
if (isIntersect(left_y1, left_y2, right_y1, right_y2))
|
||||
{
|
||||
intersections_num += 1;
|
||||
}
|
||||
if(intersections_num == 2) //交点数目为2时,说明已经到了峰的右侧,停止扩展
|
||||
{
|
||||
break;
|
||||
}
|
||||
new_max_index += 1;
|
||||
|
||||
}
|
||||
|
||||
extend_interval_index.push_back({ new_min_index, new_max_index });
|
||||
map<string, int> map;
|
||||
map["max_intensity_index"] = intensity_temp_max_index;
|
||||
extend_interval_info.push_back(map);
|
||||
|
||||
}
|
||||
return { extend_interval_index, extend_interval_info };
|
||||
}
|
||||
|
||||
//合并相交的区间
|
||||
MergeIntervalIndexStruct SpecPeakService::mergeIntervalIndex(vector<vector<int>> interval_index,
|
||||
vector<map<string, int>> interval_info)
|
||||
{
|
||||
//qDebug() << "[Logic]-合并相交的区间" << endl;
|
||||
vector<vector<int>> merge_interval_index;
|
||||
vector<vector<int>> merge_interval_info;
|
||||
|
||||
vector<int> temp_range;
|
||||
vector<int> temp_max_index_list;
|
||||
|
||||
for(int k = 0; k < interval_index.size(); ++k)
|
||||
{
|
||||
|
||||
if(merge_interval_index.empty()){
|
||||
merge_interval_index.push_back(interval_index[k]);
|
||||
merge_interval_info.push_back({interval_info[k].at("max_intensity_index")});
|
||||
continue;
|
||||
}
|
||||
|
||||
//如果 该区间的左侧索引 落在 添加到列表中的最后一个区间内,则合并
|
||||
if(interval_index[k][0] < merge_interval_index.back()[1]){
|
||||
merge_interval_index.back()[1] = interval_index[k][1];
|
||||
merge_interval_info.back().push_back({interval_info[k].at("max_intensity_index")});
|
||||
}else{
|
||||
merge_interval_index.push_back(interval_index[k]);
|
||||
merge_interval_info.push_back({interval_info[k].at("max_intensity_index")});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return { merge_interval_index, merge_interval_info };
|
||||
|
||||
}
|
||||
|
||||
//数据平滑
|
||||
vector<double> SpecPeakService::smooth(vector<double> intensity_data, int points_num)
|
||||
{
|
||||
//qDebug() << "[Logic]-数据平滑" << endl;
|
||||
if(points_num % 2 == 0){
|
||||
throw invalid_argument("points_num必须是奇数");
|
||||
}
|
||||
int half_points_num = points_num / 2;
|
||||
vector<double> new_intensity(intensity_data.size());
|
||||
for(int i = 0; i < intensity_data.size(); i++){
|
||||
double sum = 0.0;
|
||||
int start = max(0, i - half_points_num);
|
||||
int end = min(int(intensity_data.size()), i + half_points_num + 1);
|
||||
for(int j = start; j < end; j++){
|
||||
sum += intensity_data[j];
|
||||
}
|
||||
new_intensity[i] = sum / (end - start);
|
||||
}
|
||||
return new_intensity;
|
||||
}
|
||||
|
||||
|
||||
//将下标转为为波长与强度
|
||||
vector<SpectrumChannelData> SpecPeakService::mapInvertInt(vector<SpectrumChannelData> channelData, vector<int> mergedIndex)
|
||||
{
|
||||
//qDebug() << "[Logic]-将下标转为为波长与强度1" << endl;
|
||||
vector<SpectrumChannelData> findPeak;
|
||||
for(const auto& index : mergedIndex){
|
||||
findPeak.push_back(channelData[index]);
|
||||
}
|
||||
return findPeak;
|
||||
}
|
||||
|
||||
vector<int> SpecPeakService::mapInvertInt(MergeIntervalIndexStruct extendedIntervalIndexStruct)
|
||||
{
|
||||
//qDebug() << "[Logic]-map集合下标转化为vector" << endl;
|
||||
vector<int> mergedIndex;
|
||||
vector<vector<int>> merge_interval_info = extendedIntervalIndexStruct.merge_interval_info;
|
||||
for(const auto& indexMap : merge_interval_info){
|
||||
for(const auto& index : indexMap){
|
||||
mergedIndex.insert(mergedIndex.end(),index);
|
||||
}
|
||||
}
|
||||
return mergedIndex;
|
||||
|
||||
}
|
||||
|
||||
void SpecPeakService::getRes()
|
||||
{
|
||||
//qDebug() << "[Logic]-获取峰值下标" << endl;
|
||||
vector<int> features_index;
|
||||
// 1.找出特征波长节点
|
||||
features_index = findFeatureIndex();
|
||||
|
||||
// 2.去除相邻的波长节点
|
||||
features_index = featureIndexDelMiddle(features_index);
|
||||
|
||||
// 3.从相邻的波长节点中选择一个
|
||||
features_index = chooseFeatureIndex2of1(features_index);
|
||||
|
||||
// 4.将特征波长节点转化为区间索引
|
||||
IntervalIndexStruct intervalIndexStruct = getIntervalIndex(features_index);
|
||||
vector<vector<int>> interval_index = intervalIndexStruct.interval_index;
|
||||
vector<vector<int>> init_interval_index = intervalIndexStruct.init_interval_index;
|
||||
vector<vector<string>> init_interval_info = intervalIndexStruct.init_interval_info;
|
||||
|
||||
// 5.扩展区间
|
||||
ExtendedIntervalIndexStruct extendedIntervalIndexStruct = extendedIntervalIndex(interval_index);
|
||||
vector<vector<int>> extend_interval_index = extendedIntervalIndexStruct.extend_interval_index;
|
||||
vector<map<string, int>> extend_interval_info = extendedIntervalIndexStruct.extend_interval_info;
|
||||
|
||||
|
||||
// 6.合并区间
|
||||
MergeIntervalIndexStruct mergeIntervalIndexData = mergeIntervalIndex(extend_interval_index, extend_interval_info);
|
||||
vector<vector<int>> merge_interval_index = mergeIntervalIndexData.merge_interval_index;
|
||||
vector<vector<int>> merge_interval_info = mergeIntervalIndexData.merge_interval_info;
|
||||
|
||||
mergeIntervalIndexStruct.merge_interval_index = merge_interval_index;
|
||||
mergeIntervalIndexStruct.merge_interval_info = merge_interval_info;
|
||||
}
|
||||
|
||||
// 1. 获取扣掉强峰的区间(强峰区间对整个波长范围取补集)
|
||||
// 2. 筛选掉长度不足 50 个点的区间
|
||||
vector<int> SpecPeakService::get_background_interval(MergeIntervalIndexStruct mergeIntervalIndexStruct)
|
||||
{
|
||||
//qDebug() << "[Logic]-获取扣掉强峰的区间" << endl;
|
||||
vector<vector<int>> strength_peaks = mergeIntervalIndexStruct.merge_interval_index;
|
||||
vector<vector<int>> strength_info = mergeIntervalIndexStruct.merge_interval_info;
|
||||
vector<vector<int>> temp_list;
|
||||
//挑选背景特征区间
|
||||
int interval_length = 0;
|
||||
if(strength_peaks[0][0] != 0){
|
||||
temp_list = {{0, strength_peaks[0][0]}};
|
||||
} else{
|
||||
temp_list = {};
|
||||
}
|
||||
|
||||
for(int i = 1; i < strength_peaks.size() -1; i++)
|
||||
{
|
||||
temp_list.push_back({strength_peaks[i][1], strength_peaks[i + 1][0]});
|
||||
}
|
||||
|
||||
if (strength_peaks.back()[1] != intensityNorm.size() - 1) {
|
||||
temp_list.push_back({strength_peaks.back()[1],int(intensityNorm.size() - 1)});
|
||||
}
|
||||
|
||||
//other_interval_list: 区间索引
|
||||
//other_interval_length: 区间长度
|
||||
vector<vector<int>> other_interval_list;
|
||||
vector<int> other_interval_length;
|
||||
std::vector<int> temp_interval = temp_list[0];
|
||||
int temp_length = temp_list[0][1] - temp_list[0][0] + 1;
|
||||
|
||||
for(const auto& interval : temp_list)
|
||||
{
|
||||
int min_index = interval[0];
|
||||
int max_index = interval[1];
|
||||
int diffValue = max_index - min_index + 1;
|
||||
if(diffValue >= 60){
|
||||
other_interval_list.push_back({min_index, max_index});
|
||||
other_interval_length.push_back(diffValue);
|
||||
}
|
||||
if(diffValue > temp_length){
|
||||
temp_interval = {min_index, max_index};
|
||||
temp_length = diffValue;
|
||||
}
|
||||
}
|
||||
if(other_interval_list.empty() && other_interval_length.empty()){
|
||||
|
||||
other_interval_list.push_back(temp_interval);
|
||||
other_interval_length.push_back(temp_length);
|
||||
}
|
||||
|
||||
if (other_interval_list.size() == 1 && other_interval_length[0] <= 60) {
|
||||
// 只有一个区间
|
||||
if (other_interval_length[0] <= 30) {
|
||||
throw std::invalid_argument("区间长度太短,重新测量");
|
||||
}
|
||||
interval_length = other_interval_length[0] - 10;
|
||||
} else {
|
||||
interval_length = 50;
|
||||
}
|
||||
|
||||
|
||||
|
||||
vector<int> background_interval;
|
||||
double criterion = 100.0;
|
||||
|
||||
for(const auto& intervalList : other_interval_list)
|
||||
{
|
||||
int min_index = intervalList[0];
|
||||
int max_index = intervalList[1];
|
||||
|
||||
vector<double> temp_data_x(wavelength.begin() + min_index,
|
||||
wavelength.begin() + max_index + 1);
|
||||
vector<double> temp_data_y(intensityNorm.begin() + min_index,
|
||||
intensityNorm.begin() + max_index + 1);
|
||||
|
||||
double min_value = *min_element(temp_data_y.begin(), temp_data_y.end());
|
||||
transform(temp_data_y.begin(), temp_data_y.end(), temp_data_y.begin(), [min_value](double val) {
|
||||
return val - min_value;
|
||||
});
|
||||
|
||||
for(int i = 0; i < temp_data_y.size() - interval_length; i++)
|
||||
{
|
||||
double temp = accumulate(temp_data_y.begin() + i, temp_data_y.begin() + i + interval_length, 0.0);
|
||||
if(temp < criterion)
|
||||
{
|
||||
background_interval = {min_index + i, min_index + i + interval_length - 1};
|
||||
criterion = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
return background_interval;
|
||||
|
||||
}
|
||||
|
||||
//获取波动值
|
||||
double SpecPeakService::get_background_wave(MergeIntervalIndexStruct mergeIntervalIndexStruct)
|
||||
{
|
||||
//qDebug() << "[Logic]-获取波动值" << endl;
|
||||
vector<int> interval = get_background_interval(mergeIntervalIndexStruct);
|
||||
double factor;
|
||||
auto minmax = minmax_element(intensityNorm.begin() + interval[0], intensityNorm.begin() + interval[1] + 1);
|
||||
double init_max_wave = *minmax.second - *minmax.first;
|
||||
if(init_max_wave <= 0.006){
|
||||
factor = 1.2;
|
||||
}else if(init_max_wave > 0.006 && init_max_wave <= 0.01){
|
||||
factor = 1.8;
|
||||
}else{
|
||||
factor = 2;
|
||||
}
|
||||
double max_wave = factor * init_max_wave;
|
||||
|
||||
return max_wave;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
|
||||
#ifndef SPECPEAKSERVICE_H
|
||||
#define SPECPEAKSERVICE_H
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include "algorithmanalysis.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
|
||||
/* init_interval_index = [] 原始区间
|
||||
init_interval_info = [] 未经过筛选的区间的信息
|
||||
interval_index = [] 最终的结果区间索引 */
|
||||
struct IntervalIndexStruct {
|
||||
vector<vector<int>> interval_index;
|
||||
vector<vector<int>> init_interval_index;
|
||||
vector<vector<string>> init_interval_info;
|
||||
};
|
||||
|
||||
|
||||
struct ExtendedIntervalIndexStruct {
|
||||
vector<vector<int>> extend_interval_index;
|
||||
vector<map<string, int>> extend_interval_info;
|
||||
};
|
||||
|
||||
struct MergeIntervalIndexStruct {
|
||||
vector<vector<int>> merge_interval_index;
|
||||
vector<vector<int>> merge_interval_info;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
class SpecPeakService
|
||||
{
|
||||
public:
|
||||
SpecPeakService();
|
||||
|
||||
vector<int> SpecPeak(vector<SpectrumChannelData> channelData);
|
||||
|
||||
//将下标转为为波长与强度
|
||||
vector<SpectrumChannelData> mapInvertInt(vector<SpectrumChannelData> channelData, vector<int> mergedIndex);
|
||||
|
||||
private:
|
||||
|
||||
//函数声明
|
||||
|
||||
//数据平滑
|
||||
vector<double> smooth(vector<double> intensity_data, int points_num = 5);
|
||||
|
||||
// 初始化波长强度
|
||||
void initSpecWavelengthStrength();
|
||||
|
||||
// 计算归一化的绝对值
|
||||
vector<double> absMaxNorm(vector<double> specList);
|
||||
|
||||
//判断两条线是否相交
|
||||
bool isIntersect(double leftY1, double leftY2, double rightY1, double rightY2);
|
||||
|
||||
// 求导数以及绝对值 归一化
|
||||
void spectralPretreatment(vector<SpectrumChannelData>);
|
||||
|
||||
// 差分
|
||||
vector<double> diff(vector<double> xValueList, vector<double> yValueList, int n = 1);
|
||||
|
||||
//找出特征波长节点
|
||||
vector<int> findFeatureIndex();
|
||||
|
||||
//去除连续波长节点中中间的节点
|
||||
vector<int> featureIndexDelMiddle(vector<int> featuresIndexList);
|
||||
|
||||
//从相邻的波长节点中选择一个
|
||||
vector<int> chooseFeatureIndex2of1(vector<int> featuresIndexList);
|
||||
|
||||
//将特征波长节点转化为区间索引,并进行条件判断,筛选出满足条件的区间
|
||||
IntervalIndexStruct getIntervalIndex(vector<int> featuresIndex);
|
||||
|
||||
//扩展区间,使得区间包含整个峰
|
||||
//从峰值开始,向左扩展至一阶导和二阶导的第二个交点,向右扩展至一阶导和二阶导的第二个交点
|
||||
ExtendedIntervalIndexStruct extendedIntervalIndex(vector<vector<int>>);
|
||||
|
||||
//合并相交的区间
|
||||
MergeIntervalIndexStruct mergeIntervalIndex(vector<vector<int>> intervalIndex, vector<map<string, int>> interval_info);
|
||||
|
||||
//将集合下标转化为vector集合
|
||||
vector<int> mapInvertInt(MergeIntervalIndexStruct extendedIntervalIndexStruct);
|
||||
|
||||
// 1. 获取扣掉强峰的区间(强峰区间对整个波长范围取补集)
|
||||
// 2. 筛选掉长度不足 50 个点的区间
|
||||
vector<int> get_background_interval(MergeIntervalIndexStruct mergeIntervalIndexStruct);
|
||||
|
||||
//获取波动值
|
||||
double get_background_wave(MergeIntervalIndexStruct mergeIntervalIndexStruct);
|
||||
|
||||
void getRes();
|
||||
|
||||
|
||||
//变量声明
|
||||
int specPointNum;
|
||||
|
||||
vector<double> intensityNorm;
|
||||
|
||||
vector<double> intensity_smooth;
|
||||
// 最大波动值
|
||||
double background_ptp = 0.2;
|
||||
|
||||
vector<double> wavelength;
|
||||
vector<double> intensity;
|
||||
|
||||
//一阶导二阶导以及其绝对值
|
||||
vector<double> d1;
|
||||
vector<double> d1_2;
|
||||
vector<double> d1_abs;
|
||||
vector<double> d2;
|
||||
vector<double> d2_2;
|
||||
vector<double> d2_abs;
|
||||
|
||||
//一阶导二阶导归一化
|
||||
vector<double> d1_norm;
|
||||
vector<double> d1_2_norm;
|
||||
vector<double> d1_abs_norm;
|
||||
vector<double> d2_norm;
|
||||
vector<double> d2_2_norm;
|
||||
vector<double> d2_abs_norm;
|
||||
|
||||
MergeIntervalIndexStruct mergeIntervalIndexStruct;
|
||||
};
|
||||
|
||||
#endif // SPECPEAKSERVICE_H
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "spectrometer.h"
|
||||
|
||||
Spectrometer::Spectrometer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool Spectrometer::ReceiveScopeData()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Spectrometer::SetReady(bool bReady)
|
||||
{
|
||||
return bReady;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef SPECTROMETER_H
|
||||
#define SPECTROMETER_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
光谱仪
|
||||
* @brief The spectrometer class
|
||||
*/
|
||||
class Spectrometer
|
||||
{
|
||||
public:
|
||||
Spectrometer();
|
||||
|
||||
short m_handle ;
|
||||
std::string m_serialNumber; //设备序列号
|
||||
unsigned short m_pixelsCount; //像素数
|
||||
unsigned short m_startPixels; //开始像素
|
||||
unsigned short m_stopPixels; //停止像素
|
||||
std::vector<double> m_wavelengthList;
|
||||
std::vector<double> m_intensityList;
|
||||
|
||||
bool ReceiveScopeData();
|
||||
|
||||
bool SetReady(bool bReady);
|
||||
|
||||
};
|
||||
|
||||
#endif // SPECTROMETER_H
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef SPECTROMETERCALLBACK_H
|
||||
#define SPECTROMETERCALLBACK_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
|
||||
class SpectrometerCallback
|
||||
{
|
||||
public:
|
||||
virtual ~SpectrometerCallback() = default;
|
||||
|
||||
// 回调函数,当光谱数据准备好时被调用
|
||||
virtual void onDataReceived(std::string serialNumber,const std::vector<float> &wavelength,
|
||||
const std::vector<float>& spectrum) = 0;
|
||||
|
||||
// 批量采集回调(ARIS 0411 版 SDK 的虚函数表里有这一项,
|
||||
// 必须保持同样的声明顺序,否则软件和 libaris.dll 的 vtable 不一致)
|
||||
virtual void onDataReceivedBanch(std::string serialNumber,const std::vector<float> &wavelength,
|
||||
const std::vector<std::vector<float>>& spectrum) = 0;
|
||||
|
||||
// 回调函数,当发生错误时被调用
|
||||
virtual void onError(std::string serialNumber,
|
||||
const std::string& error) = 0;
|
||||
};
|
||||
|
||||
#endif // SPECTROMETERCALLBACK_H
|
||||
@@ -0,0 +1,130 @@
|
||||
#include "spectrumalgorithm.h"
|
||||
#include <cmath>
|
||||
|
||||
SpectrumAlgorithm::SpectrumAlgorithm()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
vector<pair<double,double>> SpectrumAlgorithm::handleRawSpectrumList(
|
||||
std::vector<map<int,vector<pair<double,double>>>> rawSpectrumList)
|
||||
{
|
||||
|
||||
vector<pair<int,vector<SpectrumChannelData>>> ospecV = baseHandle(rawSpectrumList);
|
||||
for(auto &oneOspecV : ospecV){
|
||||
AlgorithmAnalysis::background(oneOspecV.second);
|
||||
}
|
||||
vector<SpectrumChannelData> cutSpec = AlgorithmAnalysis::cutRepeatSpecture(ospecV, 30);
|
||||
// 提取处理后的光谱数据
|
||||
std::vector<std::pair<double, double>> result;
|
||||
for (const auto &data : cutSpec) {
|
||||
result.push_back(std::make_pair(data.wavelength, data.intensity));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::tuple<std::string, double, double>> SpectrumAlgorithm::getElementQuantitytList(std::vector<map<int,vector<pair<double,double>>>> rawSpectrumList)
|
||||
{
|
||||
vector<pair<int,vector<SpectrumChannelData>>> ospecV = baseHandle(rawSpectrumList);
|
||||
|
||||
ElementAnalysis elementAnalysis;
|
||||
vector<SpectrumElementQuantity> quantityList = elementAnalysis.analysisElementQuantity(ospecV);
|
||||
// 转换 ElementQuantity 到 std::tuple<std::string, double, double>
|
||||
std::vector<std::tuple<std::string, double, double>> result;
|
||||
result.reserve(quantityList.size());
|
||||
|
||||
for (const auto& eq : quantityList) {
|
||||
result.emplace_back(eq.name, eq.quantity, eq.percent);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
vector<pair<int,vector<SpectrumChannelData>>> SpectrumAlgorithm::baseHandle(std::vector<map<int,vector<pair<double,double>>>> rawSpectrumList)
|
||||
{
|
||||
// 将pair转换为ChannelData
|
||||
std::vector<std::map<int, std::vector<SpectrumChannelData>>> rawSpectrumListChannelData;
|
||||
|
||||
for (const auto& map : rawSpectrumList) {
|
||||
std::map<int, std::vector<SpectrumChannelData>> channelDataMap;
|
||||
for (const auto& pair : map) {
|
||||
int key = pair.first;
|
||||
const auto& vec = pair.second;
|
||||
std::vector<SpectrumChannelData> channelDataVec;
|
||||
for (const auto& p : vec) {
|
||||
channelDataVec.push_back({p.first, p.second});
|
||||
}
|
||||
channelDataMap[key] = channelDataVec;
|
||||
}
|
||||
rawSpectrumListChannelData.push_back(channelDataMap);
|
||||
}
|
||||
|
||||
std::map<int, std::vector<std::vector<SpectrumChannelData>>> deviceSpecMap;
|
||||
vector<pair<int,vector<SpectrumChannelData>>> ospecV;
|
||||
|
||||
//获取通道数据
|
||||
for(int i = 0 ; i < rawSpectrumListChannelData.size(); i++){
|
||||
|
||||
map<int,vector<SpectrumChannelData>> channelDataMap = rawSpectrumListChannelData[i];
|
||||
|
||||
for (auto it = channelDataMap.begin(); it != channelDataMap.end(); it++) {
|
||||
// 获取当前元素的值(即 vector<SpectrumChannelData>)
|
||||
int deviceNum = it->first;
|
||||
|
||||
std::vector<SpectrumChannelData> channelData = it->second;
|
||||
|
||||
// 动态地将 channelData 添加到对应设备编号的 SpecLst 列表中
|
||||
deviceSpecMap[deviceNum].push_back(channelData);
|
||||
|
||||
}
|
||||
}
|
||||
for (auto oneDeviceSpec : deviceSpecMap) {
|
||||
int firstDeviceNum = oneDeviceSpec.first;
|
||||
std::vector<std::vector<SpectrumChannelData>> firstDeviceSpecLst = oneDeviceSpec.second;
|
||||
//将各个通道的数据进行平均
|
||||
vector<SpectrumChannelData> deviceSpec = AlgorithmAnalysis::average(firstDeviceSpecLst);
|
||||
ospecV.push_back(std::make_pair(firstDeviceNum,deviceSpec)) ;
|
||||
}
|
||||
//将通道数据进行排序
|
||||
sort(ospecV.begin(),ospecV.end(),AlgorithmAnalysis::cmp);
|
||||
// 波长修正
|
||||
// fixedWavelength(ospecV);
|
||||
return ospecV;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 波长修正
|
||||
void SpectrumAlgorithm::fixedWavelength(vector<pair<int,vector<SpectrumChannelData>>> &ospecV)
|
||||
{
|
||||
for(int i = 0 ; i < ospecV.size(); i++)
|
||||
{
|
||||
vector<SpectrumChannelData>& wavelengthList = ospecV[i].second;
|
||||
|
||||
for(int j = 1 ; j <= wavelengthList.size(); j++)
|
||||
{
|
||||
if((i+1) == 1){
|
||||
wavelengthList[j - 1].wavelength = 186.89977 + 0.06665 * j + 0.00000614515*pow(j,2)
|
||||
- 0.000000006549699 * pow(j,3) + 0.00000000000145383 * pow(j,4);
|
||||
|
||||
}else if((i+1) == 2){
|
||||
wavelengthList[j - 1].wavelength = 306.73573 + 0.06379 * j + 0.000000656853 * pow(j,2)
|
||||
- 0.00000000450952 * pow(j,3) + 0.00000000000115411 * pow(j,4);
|
||||
|
||||
}else if((i+1) == 3){
|
||||
wavelengthList[j - 1].wavelength = 410.52275 + 0.03973 * j + 0.0000109683 * pow(j,2)
|
||||
- 0.0000000136351 * pow(j,3) + 0.00000000000510908 * pow(j,4);
|
||||
|
||||
}else if((i+1) == 4){
|
||||
wavelengthList[j - 1].wavelength = 543.41183 + 0.0787 * j - 0.0000120952 * pow(j,2)
|
||||
+ 0.00000000341057 * pow(j,3) - 0.000000000000380113 * pow(j,4);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef SPECTRUMALGORITHM_H
|
||||
#define SPECTRUMALGORITHM_H
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include "algorithmanalysis.h"
|
||||
#include "elementanalysis.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
class SpectrumAlgorithm
|
||||
{
|
||||
|
||||
public:
|
||||
SpectrumAlgorithm();
|
||||
|
||||
/**
|
||||
处理原始数据,包括平均,排序,去背景,裁剪
|
||||
* @brief handleRawSpectrumList
|
||||
* @param rawSpecturmList:多个map,键为光谱仪设备号,值为波长、强度的结构体
|
||||
* @return
|
||||
*/
|
||||
vector<pair<double,double>> handleRawSpectrumList(
|
||||
std::vector<map<int,vector<pair<double,double>>>> rawSpectrumList);
|
||||
|
||||
/**
|
||||
jisuan
|
||||
* @brief getElementQuantitytList
|
||||
* @param rawSpectrumList
|
||||
* @return
|
||||
*/
|
||||
std::vector<std::tuple<std::string, double, double>> getElementQuantitytList(std::vector<map<int,vector<pair<double,double>>>> rawSpectrumList);
|
||||
|
||||
private:
|
||||
/**
|
||||
处理原始数据,包括平均,排序
|
||||
* @brief handleRawSpectrumList
|
||||
* @param rawSpecturmList:多个map,键为光谱仪设备号,值为波长、强度的结构体
|
||||
* @return
|
||||
*/
|
||||
vector<pair<int,vector<SpectrumChannelData>>> baseHandle(std::vector<map<int,vector<pair<double,double>>>> rawSpectrumList);
|
||||
|
||||
// 波长修正
|
||||
void fixedWavelength(vector<pair<int,vector<SpectrumChannelData>>> &ospecV);
|
||||
};
|
||||
|
||||
#endif // SPECTRUMALGORITHM_H
|
||||
Reference in New Issue
Block a user