Exemplo n.º 1
0
        public void NormalizeLM(double iterNumber)
        {
            Spline31D spline;

            double[] x;
            double   maxLambda, minLambda;

            double[]   b;
            double[][] r;
            double[]   wm;
            double[]   syn;
            double[]   sigma;
            double[][] G;
            double[]   data;
            double[]   dw;

            this.polyPars = new double[this.polynomOrder + 1];

            spline = new Spline31D(this.tempSpectrum[0], this.tempSpectrum[1]);

            x         = new double[this.obsSpectrum[0].Length];
            maxLambda = this.obsSpectrum[0][this.obsSpectrum[0].Length - 1];
            minLambda = this.obsSpectrum[0][0];

            for (int i = 0; i < x.Length; i++)
            {
                x[i] = (this.obsSpectrum[0][i] - minLambda) / (maxLambda - minLambda) + 1;
            }

            b = new double[this.polynomOrder + 1];
            r = new double[this.polynomOrder + 1][];
            for (int i = 0; i < polynomOrder + 1; i++)
            {
                r[i] = new double[this.polynomOrder + 1];
            }

            wm = new double[this.obsSpectrum[0].Length];
            for (int i = 0; i < wm.Length; i++)
            {
                wm[i] = 1.0;
            }

            syn = new double[this.obsSpectrum[0].Length];
            for (int i = 0; i < syn.Length; i++)
            {
                syn[i] = spline.Interp(this.obsSpectrum[0][i]);
            }

            sigma = new double[this.obsSpectrum[0].Length];
            for (int i = 0; i < sigma.Length; i++)
            {
                sigma[i] = Math.Sqrt(this.obsSpectrum[1][i]);
            }

            G = new double[this.obsSpectrum[0].Length][];
            for (int i = 0; i < this.obsSpectrum[0].Length; i++)
            {
                G[i] = new double[this.polynomOrder + 1];
            }

            for (int i = 0; i < G.Length; i++)
            {
                for (int j = 0; j < G[0].Length; j++)
                {
                    G[i][j] = Math.Pow(x[i], j) * syn[i] / sigma[i];
                }
            }

            data = new double[this.obsSpectrum[1].Length];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = obsSpectrum[1][i] / sigma[i];
            }

            dw = new double[data.Length];


            for (int iter = 0; iter < iterNumber; iter++)
            {
                if (iter != 0)
                {
                    Basic.MA_mult_VB_minus_VC(ref G, ref polyPars, ref data, ref wm);
                    for (int i = 0; i < wm.Length; i++)
                    {
                        wm[i] = 1.0 / Math.Abs(wm[i]);
                    }
                }

                for (int l = 0; l < this.polynomOrder + 1; l++)
                {
                    double sum = 0;
                    for (int i = 0; i < obsSpectrum[0].Length; i++)
                    {
                        sum = sum + G[i][l] * wm[i] * data[i];
                    }
                    b[l] = sum;
                }

                for (int l = 0; l < this.polynomOrder + 1; l++)
                {
                    for (int k = 0; k <= l; k++)
                    {
                        double sum = 0;
                        for (int i = 0; i < obsSpectrum[0].Length; i++)
                        {
                            sum = sum + wm[i] * G[i][l] * G[i][k];
                        }
                        r[l][k] = sum;
                        r[k][l] = sum;
                    }
                }

                this.polyPars = LES_Solver.SolveWithGaussMethod(r, b);
            }
        }
Exemplo n.º 2
0
        public void Normalize()
        {
            double bias     = 1e30;
            int    sections = 20;

            double[] averanges = new double[sections];
            double[] biasCoeff = new double[this.obsSpectrum[0].Length];

            this.polyPars = new double[this.polynomOrder + 1];
            Spline31D spline = new Spline31D(this.tempSpectrum[0], this.tempSpectrum[1]);

            double[] x         = new double[this.obsSpectrum[0].Length];
            double   maxLambda = this.obsSpectrum[0][this.obsSpectrum[0].Length - 1];
            double   minLambda = this.obsSpectrum[0][0];

            for (int i = 0; i < x.Length; i++)
            {
                x[i] = (this.obsSpectrum[0][i] - minLambda) / (maxLambda - minLambda);
            }

            double[]   b = new double[this.polynomOrder + 1];
            double[][] r = new double[this.polynomOrder + 1][];
            for (int i = 0; i < polynomOrder + 1; i++)
            {
                r[i] = new double[this.polynomOrder + 1];
            }

            double[] syn = new double[this.obsSpectrum[0].Length];
            for (int i = 0; i < syn.Length; i++)
            {
                syn[i] = spline.Interp(this.obsSpectrum[0][i]);
            }

            for (int i = 0; i < averanges.Length; i++)
            {
                double sum = 0;
                double ave;
                int    size = syn.Length / averanges.Length;
                for (int j = 0; j < size; j++)
                {
                    sum += syn[j + i * size];
                }
                ave          = sum / size;
                averanges[i] = ave;
            }
            for (int i = 0; i < biasCoeff.Length; i++)
            {
                int secNum;
                secNum = i / (syn.Length / averanges.Length);
                if (syn[i] > averanges[secNum])
                {
                    biasCoeff[i] = bias * (syn[i] - averanges[secNum]);
                }
                else
                {
                    biasCoeff[i] = 0;
                }
            }


            for (int l = 0; l < this.polynomOrder + 1; l++)
            {
                double sum = 0;
                for (int i = 0; i < obsSpectrum[0].Length; i++)
                {
                    sum = sum + syn[i] * obsSpectrum[1][i] * Math.Pow(x[i], l) * biasCoeff[i] * biasCoeff[i];
                }
                b[l] = sum;
            }

            for (int l = 0; l < this.polynomOrder + 1; l++)
            {
                for (int k = 0; k <= l; k++)
                {
                    double sum = 0;
                    for (int i = 0; i < obsSpectrum[0].Length; i++)
                    {
                        sum = sum + Math.Pow(syn[i] * biasCoeff[i], 2) * Math.Pow(x[i], l) *
                              Math.Pow(x[i], k);
                    }
                    r[l][k] = sum;
                    r[k][l] = sum;
                }
            }

            this.polyPars = LES_Solver.SolveWithGaussMethod(r, b);
        }
Exemplo n.º 3
0
        private void btn_NormInterp_Click(object sender, EventArgs e)
        {
            if (this.tempSpectrum == null)
            {
                MessageBox.Show("Reference spectrum is't loaded...", "Error...");
                return;
            }

            try
            {
            }
            catch
            {
                MessageBox.Show("Error in input data format...", "Error...");
                return;
            }


            // trimming of the template spectra;
            double lambdaMin = this.obsSpectrum.LambdaSet.Min();
            double lambdaMax = this.obsSpectrum.LambdaSet.Max();

            int index1_cut = 0, index2_cut = 0;

            for (int i = 0; i < this.tempSpectrum.Size; i++)
            {
                if (this.tempSpectrum.LambdaSet[i] >= lambdaMin)
                {
                    index1_cut = i;
                    for (int j = i; j < this.tempSpectrum.Size; j++)
                    {
                        if (this.tempSpectrum.LambdaSet[j] >= lambdaMax)
                        {
                            index2_cut = j;
                            break;
                        }
                    }
                    break;
                }
            }

            int size = index2_cut - index1_cut + 1;

            double[] tempCuteLambda = new double[size];
            double[] tempCuteFlux   = new double[size];
            Array.Copy(this.tempSpectrum.LambdaSet, index1_cut, tempCuteLambda, 0, size);
            Array.Copy(this.tempSpectrum.FluxSet, index1_cut, tempCuteFlux, 0, size);
            this.temCutSpectrum = new Spectrum(tempCuteLambda, tempCuteFlux);
            // end of trimming of the template spectra;

            this.cutMask.Sort();

            //
            double[] xx = new double[this.cutMask.Size()];
            double[] yy = new double[this.cutMask.Size()];

            for (int i = 0; i < xx.Length; i++)
            {
                double x1 = 0;
                double y1 = 0;

                double x0 = 0;
                double y0 = 0;

                int size1 = 0;
                for (int j = 0; j < obsSpectrum.LambdaSet.Length; j++)
                {
                    if (cutMask.GetLeftBound(i) <= obsSpectrum.LambdaSet[j] &&
                        cutMask.GetRightBound(i) >= obsSpectrum.LambdaSet[j])
                    {
                        x1 += obsSpectrum.LambdaSet[j];
                        y1 += obsSpectrum.FluxSet[j];
                        size1++;
                    }
                }
                xx[i] = x1 / size1;
                yy[i] = y1 / size1;

                int size0 = 0;
                for (int j = 0; j < temCutSpectrum.LambdaSet.Length; j++)
                {
                    if (cutMask.GetLeftBound(i) <= temCutSpectrum.LambdaSet[j] &&
                        cutMask.GetRightBound(i) >= temCutSpectrum.LambdaSet[j])
                    {
                        x0 += temCutSpectrum.LambdaSet[j];
                        y0 += temCutSpectrum.FluxSet[j];
                        size0++;
                    }
                }
                x0 = x0 / size0;
                y0 = y0 / size0;

                yy[i] = yy[i] / y0;
            }

            Spline31D spline = new Spline31D(xx, yy);

            double[] contFluxes = new double[obsSpectrum.LambdaSet.Length];
            double[] normFluxes = new double[obsSpectrum.LambdaSet.Length];

            for (int i = 0; i < obsSpectrum.LambdaSet.Length; i++)
            {
                contFluxes[i] = spline.Interp(obsSpectrum.LambdaSet[i]);
                normFluxes[i] = obsSpectrum.FluxSet[i] / contFluxes[i];
            }

            this.normSpectrum = new Spectrum(this.obsSpectrum.LambdaSet, normFluxes);
            this.contSpectrum = new Spectrum(this.obsSpectrum.LambdaSet, contFluxes);

            this.DrawObsSpecGraph();
            this.DrawNormSpecGraph();
        }