Ejemplo n.º 1
0
        /// <summary>
        /// 使用光谱校正系数,校正光谱,包含插值(包含X轴数据)
        /// </summary>
        /// <param name="coefData">光谱校正系数(BYTE)</param>
        /// <param name="slaveData">光谱数据</param>
        /// <returns>校正后的光谱Y轴数据</returns>
        public static List <double[]> ApplyCoefficientWithXData(byte[] coefData, IList <double[]> slaveData)
        {
            try
            {
                if (coefData == null || slaveData == null || slaveData.Count == 1)
                {
                    throw new Exception("Invalid parameters");
                }

                //判断是否需要插值
                var para = GetStandardizingCoefParameter(coefData);
                if (para.datasize == 0)
                {
                    throw new Exception("Invalid parameters");
                }

                //插值
                if (!CommonMethod.IsSameXDatas(slaveData[0], para.firstX, para.lastX, para.specCols))
                {
                    slaveData = PreProcessor.SplineCubicInterpolation(slaveData, para.firstX, para.lastX, para.specCols);
                    if (slaveData == null)
                    {
                        throw new Exception(PreProcessor.ErrorString);
                    }
                }

                //保留xData
                var xData = slaveData[0];

                //标准化时不需要xData
                slaveData.RemoveAt(0);
                var retSpecs = ApplyCoefficient(coefData, slaveData);
                if (retSpecs != null && retSpecs.Count != slaveData.Count)
                {
                    throw new Exception(ErrorString);
                }

                //恢复xData
                retSpecs.Insert(0, xData);

                return(retSpecs);
            }
            catch (Exception ex)
            {
                ErrorString = ex.Message;
                return(null);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// AdvancedIdentify定性分析(光谱的数据点数量必须相同, X轴也必须相同)
        /// </summary>
        /// <param name="apiList">API原始光谱插值后,一列X轴,一列Y轴</param>
        /// <param name="inferList">干扰组分原光谱插值后,一列X轴,多列Y轴</param>
        /// <param name="sampleList">待分析物原始光谱插值后,一列X轴,一列Y轴</param>
        /// <param name="coefMethod">相关方法,0为标准,1为增强</param>
        /// <param name="baseline">基线校正方法</param>
        /// <param name="derivatives">导数平滑方法(有基线校正,忽略导数平滑)</param>
        /// <param name="smoothPoints">平滑窗口大小(5-99)</param>
        /// <param name="polyDegree">导数的阶数,2,3,4</param>
        /// <param name="firstX">起始计算X轴,-1表示使用API光谱的firstX</param>
        /// <param name="lastX">结束计算X轴,-1表示使用API光谱的lastX</param>
        /// <returns></returns>
        public static double Identify(List <double[]> apiList, List <double[]> inferList, List <double[]> sampleList, CoefficientMethod coefMethod,
                                      BaseLineMethod baseline = BaseLineMethod.None, DerivativeMethod derivatives = DerivativeMethod.None,
                                      int smoothPoints        = 7, int polyDegree = 2, double firstX = double.MinValue, double lastX = double.MaxValue)
        {
            if (apiList.Count != 2 || inferList.Count < 2 || sampleList.Count != 2)
            {
                ErrorString = "Invalid parameters";
                return(0);
            }

            if (smoothPoints > 99 || smoothPoints < 2 || polyDegree < 2 || polyDegree > 4)
            {
                ErrorString = "Invalid parameters";
                return(0);
            }

            //需要截取一部分光谱区间
            if (firstX != double.MinValue && lastX != double.MaxValue)
            {
                apiList    = CommonMethod.GetRangeData(apiList, firstX, lastX);
                inferList  = CommonMethod.GetRangeData(inferList, firstX, lastX);
                sampleList = CommonMethod.GetRangeData(sampleList, firstX, lastX);
            }

            firstX = apiList[0][0];
            lastX  = apiList[0][apiList[0].Length - 1];
            int dataCount = apiList[0].Length;

            //格式不相同,全部兼容到api光谱
            if (!CommonMethod.IsSameXDatas(inferList[0], firstX, lastX, dataCount))
            {
                inferList = Ai.Hong.Algorithm.PreProcessor.SplineCubicInterpolation(inferList, firstX, lastX, dataCount);
            }
            if (!CommonMethod.IsSameXDatas(sampleList[0], firstX, lastX, dataCount))
            {
                sampleList = Ai.Hong.Algorithm.PreProcessor.SplineCubicInterpolation(sampleList, firstX, lastX, dataCount);
            }

            //限定2种计算系数的方法,1=无权重, 2=强度
            coefMethod = coefMethod != CoefficientMethod.Normal ? CoefficientMethod.Weight : coefMethod;

            //拷贝为数组,好在Ai.Hong.Algorithm中处理
            var srcApiDatas = Ai.Hong.Algorithm.CommonMethod.CombineSpectrumDatas(apiList);
            var apiDatas    = Ai.Hong.Algorithm.CommonMethod.CombineSpectrumDatas(apiList);
            var inferDatas  = Ai.Hong.Algorithm.CommonMethod.CombineSpectrumDatas(inferList);
            var sampleDatas = Ai.Hong.Algorithm.CommonMethod.CombineSpectrumDatas(sampleList);

            if (apiDatas == null || inferDatas == null || sampleDatas == null)
            {
                ErrorString = CommonMethod.ErrorString;
                return(0);
            }

            //需要基线校正
            if (baseline != BaseLineMethod.None)
            {
                BaselineCorrect(apiDatas, apiList.Count, dataCount, baseline);
                BaselineCorrect(inferDatas, inferList.Count, dataCount, baseline);
                BaselineCorrect(sampleDatas, sampleList.Count, dataCount, baseline);
            }
            else if (derivatives != DerivativeMethod.None)                                                         //导数平滑
            {
                coefMethod = coefMethod == CoefficientMethod.Normal ? coefMethod : CoefficientMethod.SquareWeight; //纪南的程序里面,导数后,增强方法计算系数的方式不一样(使用强度平方)

                PreProcessor.SGSmoothAndDerivative(apiDatas, apiList.Count, dataCount, smoothPoints, polyDegree, (int)derivatives);
                PreProcessor.SGSmoothAndDerivative(inferDatas, inferList.Count, dataCount, smoothPoints, polyDegree, (int)derivatives);
                PreProcessor.SGSmoothAndDerivative(sampleDatas, sampleList.Count, dataCount, smoothPoints, polyDegree, (int)derivatives);

                //纪南的导数没有除以X轴步长
                double stepx = apiDatas[1] - apiDatas[0];
                for (int i = dataCount; i < apiDatas.Length; i++)
                {
                    apiDatas[i] *= stepx;
                }

                stepx = inferDatas[1] - inferDatas[0];
                for (int i = dataCount; i < inferDatas.Length; i++)
                {
                    inferDatas[i] *= stepx;
                }

                stepx = sampleDatas[1] - sampleDatas[0];
                for (int i = dataCount; i < sampleDatas.Length; i++)
                {
                    sampleDatas[i] *= stepx;
                }
            }

            return(Identify(srcApiDatas, apiDatas, inferDatas, inferList.Count, sampleDatas, coefMethod));
        }