//获取某元素波长的方程,前置约束为调用者已经完成所有所有标样的读取 //函数返回方程的同时,将返回方程进行计算时用到的标样点 public static LinearFit.LfReValue get_equation(spec_metadata spec_obj, int element_index, ref double[] concentration, ref double[] standards_integration_average_strenth, int bs) { double[] wave_all = spec_obj.read_wave_all; double[] env = spec_obj.env_spec; standard[] standards = spec_obj.standards; int standard_cnt = spec_obj.standard_cnt; select_element element = spec_obj.elements[element_index]; double[,,] spec_standard = spec_obj.read_standard_spec; //提取该元素列标样数据,得到方程 standards_integration_average_strenth = new double[standard_cnt]; //标样平均的积分平均强度 for (int i = 0; i < standard_cnt; i++) //每个标样 { double standard_element_interval_average = 0.0; //平均的平均 //如果点击的为标样,在计算方程的循环中直接获取多次积分平均值 double[] strenght_oneshot = get_oneshot_all_strength(spec_obj, i, element_index); for (int t = 0; t < standards[i].average_times; t++) { standard_element_interval_average += strenght_oneshot[t]; } standards_integration_average_strenth[i] = standard_element_interval_average / standards[i].average_times; } concentration = new double[standard_cnt]; //浓度 for (int j = 0; j < standard_cnt; j++) { concentration[j] = standards[j].standard_ppm[element.sequece_index - 1]; } LinearFit.LfReValue equation; //过空白或不过空白读取 if (bs == 0) { equation = LinearFit.linearFitFunc(concentration, standards_integration_average_strenth, standard_cnt); } else { equation = LinearFit.linearFitFunc_blank(concentration, standards_integration_average_strenth, standard_cnt); } return(equation); }
//根据实际测量数据spec_standard和spec_sample,选择的各元素,标样,样本,计算出将要显示的表格数据 //返回值standard_val和sample_val分别为计算后表格要显示的标样/样本的浓度或强度数据 private static void fill_datagrid_data(spec_metadata spec_obj, ref double[,] standard_val, ref double[,] sample_val, int bs) { select_element[] elements = spec_obj.elements; standard[] standards = spec_obj.standards; sample[] samples = spec_obj.samples; double[] wave_all = spec_obj.read_wave_all; double[] env = spec_obj.env_spec; double[,,] spec_standard = spec_obj.read_standard_spec; double[,,] spec_sample = spec_obj.read_sample_spec; int standard_cnt = spec_obj.standard_cnt; int sample_cnt = spec_obj.sample_cnt; int element_cnt = spec_obj.element_cnt; int pixel_cnt = spec_obj.read_wave_all.Length; double[,] standards_integration_average_strenth = new double[standard_cnt, element_cnt]; double[,] samples_integration_average_strenth = new double[sample_cnt, element_cnt]; //标样平均的积分平均强度 for (int i = 0; i < standard_cnt; i++) //每个标样 { if (!standards[i].is_readed) { continue; } for (int t = 0; t < standards[i].average_times; t++) { double[] spec_temp_all = new double[pixel_cnt]; for (int k = 0; k < pixel_cnt; k++) { spec_temp_all[k] = spec_standard[i, t, k]; } for (int j = 0; j < element_cnt; j++) //每一个元素 { standards_integration_average_strenth[i, j] += data_util.get_integration_average(wave_all, spec_temp_all, env, elements[j].interval_start, elements[j].interval_end); } } for (int j = 0; j < element_cnt; j++) { standards_integration_average_strenth[i, j] /= standards[i].average_times; } } //样本的平均积分平均强度 for (int i = 0; i < sample_cnt; i++) //每个标样 { if (!samples[i].is_read) { continue; } for (int t = 0; t < samples[i].average_times; t++) { double[] spec_temp_all = new double[pixel_cnt]; for (int k = 0; k < pixel_cnt; k++) { spec_temp_all[k] = spec_sample[i, t, k]; } for (int j = 0; j < element_cnt; j++) //每一个元素 { samples_integration_average_strenth[i, j] += data_util.get_integration_average(wave_all, spec_temp_all, env, elements[j].interval_start, elements[j].interval_end); } } for (int j = 0; j < element_cnt; j++) { samples_integration_average_strenth[i, j] /= samples[i].average_times; } } //显示强度,直接提取spec数组数据,根据元素(波长)积分区间来得到计算值 if (datagrid_control.show_strenth) { standard_val = standards_integration_average_strenth; sample_val = samples_integration_average_strenth; } // 当已经读取所有标样,则计算线性拟合方程,计算样本浓度 else { //所有标样都已经读取完,才需要计算样本浓度 bool is_need_calculate = true; for (int i = 0; i < standard_cnt; i++) { if (!standards[i].is_readed) { is_need_calculate = false; //标样没有全部读取完,则不需要计算方程 } for (int j = 0; j < element_cnt; j++) { standard_val[i, j] = standards[i].standard_ppm[j]; } } //样本需要根据线性方程进行计算 if (is_need_calculate) { // 根据标样浓度和对应强度计算 for (int i = 0; i < element_cnt; i++) { double[] concentration = new double[standard_cnt]; //浓度 double[] strenth = new double[standard_cnt]; for (int j = 0; j < standard_cnt; j++) { concentration[j] = standards[j].standard_ppm[i]; strenth[j] = standards_integration_average_strenth[j, i]; } LinearFit.LfReValue equation; if (bs == 0) { equation = LinearFit.linearFitFunc(concentration, strenth, standard_cnt); } else { equation = LinearFit.linearFitFunc_blank(concentration, strenth, standard_cnt); } //根据方程进行样本浓度推算 for (int j = 0; j < sample_cnt; j++) { if (!samples[j].is_read) { continue; } sample_val[j, i] = (samples_integration_average_strenth[j, i] - equation.getA()) / equation.getB(); } } } else //不需要计算时设置为0 { for (int i = 0; i < sample_cnt; i++) { for (int j = 0; j < element_cnt; j++) { sample_val[i, j] = 0; } } } } }