Esempio n. 1
0
    /// <summary>
    /// Processes the spectra in matrix xMatrix for prediction.
    /// </summary>
    /// <param name="xMatrix">The matrix of spectra. Each spectrum is a row of the matrix.</param>
    /// <param name="xMean">Not used.</param>
    /// <param name="xScale">Not used.</param>
    /// <param name="regionstart">Starting index of the region.</param>
    /// <param name="regionend">End index of the region (one behind the last region element).</param>
    public void Process(IMatrix xMatrix, IROVector xMean, IROVector xScale, int regionstart, int regionend)
    {
      int regionlength = regionend-regionstart;
      int currentorder = Math.Min(_order,regionlength);

      switch(currentorder)
      {
        case 0: // Detrending of order 0 - subtract mean
          for(int n=0;n<xMatrix.Rows;n++)
          {
            // 1.) Get the mean response of a spectrum
            double mean = 0;
            for(int i=regionstart;i<regionend;i++)
              mean += xMatrix[n,i];
            mean /= regionlength;

            for(int i=regionstart;i<regionend;i++)
              xMatrix[n,i] -= mean;
          }
          break;
        case 1: // Detrending of order 1 - subtract linear regression line
          for(int n=0;n<xMatrix.Rows;n++)
          {
            QuickLinearRegression regression = new QuickLinearRegression();
            for(int i=regionstart;i<regionend;i++)
              regression.Add(i,xMatrix[n,i]);

            double a0 = regression.GetA0();
            double a1 = regression.GetA1();

            for(int i=regionstart;i<regionend;i++)
              xMatrix[n,i] -= (a1*i+a0);
          }
          break;
        case 2: // Detrending of order 2 - subtract quadratic regression line
          for(int n=0;n<xMatrix.Rows;n++)
          {
            QuickQuadraticRegression regression = new QuickQuadraticRegression();
            for(int i=regionstart;i<regionend;i++)
              regression.Add(i,xMatrix[n,i]);

            double a0 = regression.GetA0();
            double a1 = regression.GetA1();
            double a2 = regression.GetA2();

            for(int i=regionstart;i<regionend;i++)
              xMatrix[n,i] -= (((a2*i)+a1)*i+a0);
          }
          break;

        default:
          throw new NotImplementedException(string.Format("Detrending of order {0} is not implemented yet",_order));
      }
    }
Esempio n. 2
0
    /// <summary>
    /// Processes the spectra in matrix xMatrix.
    /// </summary>
    /// <param name="xMatrix">The matrix of spectra. Each spectrum is a row of the matrix.</param>
    /// <param name="xMean">Output: On return, contains the ensemble mean of the spectra.</param>
    /// <param name="xScale">Not used.</param>
    /// <param name="regionstart">Starting index of the region to process.</param>
    /// <param name="regionend">End index of the region to process.</param>
    void ProcessForPrediction(IMatrix xMatrix, IROVector xMean, IROVector xScale, int regionstart, int regionend)
    {
      int regionlength = regionend - regionstart;

      for(int n=0;n<xMatrix.Rows;n++)
      {
        // 2.) Do linear regression of the current spectrum versus the mean spectrum
        QuickLinearRegression regression = new QuickLinearRegression();
        for(int i=regionstart;i<regionend;i++)
          regression.Add(xMean[i],xMatrix[n,i]);

        double intercept = regression.GetA0();
        double slope = regression.GetA1();

        // 3.) Subtract intercept and divide by slope
        for(int i=regionstart;i<regionend;i++)
          xMatrix[n,i] = (xMatrix[n,i]-intercept)/slope;
      }
    }
    /// <summary>
    /// Processes the spectra in matrix xMatrix for prediction.
    /// </summary>
    /// <param name="xMatrix">The matrix of spectra. Each spectrum is a row of the matrix.</param>
    /// <param name="xMean">Not used.</param>
    /// <param name="xScale">Not used.</param>
    /// <param name="regionstart">Starting index of the region.</param>
    /// <param name="regionend">End index of the region (one behind the last region element).</param>
    public void Process(IMatrix xMatrix, IROVector xMean, IROVector xScale, int regionstart, int regionend)
    {
      int regionlength = regionend-regionstart;
      int currentorder = Math.Min(_order,regionlength);

      switch(currentorder)
      {
        case 0: // Detrending of order 0 - subtract mean
          for(int n=0;n<xMatrix.Rows;n++)
          {
            // 1.) Get the mean response of a spectrum
            double mean = 0;
            for(int i=regionstart;i<regionend;i++)
              mean += xMatrix[n,i];
            mean /= regionlength;

            for(int i=regionstart;i<regionend;i++)
              xMatrix[n,i] -= mean;
          }
          break;
        case 1: // Detrending of order 1 - subtract linear regression line
          for(int n=0;n<xMatrix.Rows;n++)
          {
            QuickLinearRegression regression = new QuickLinearRegression();
            for(int i=regionstart;i<regionend;i++)
              regression.Add(i,xMatrix[n,i]);

            double a0 = regression.GetA0();
            double a1 = regression.GetA1();

            for(int i=regionstart;i<regionend;i++)
              xMatrix[n,i] -= (a1*i+a0);
          }
          break;
        case 2: // Detrending of order 2 - subtract quadratic regression line
          for(int n=0;n<xMatrix.Rows;n++)
          {
            QuickQuadraticRegression regression = new QuickQuadraticRegression();
            for(int i=regionstart;i<regionend;i++)
              regression.Add(i,xMatrix[n,i]);

            double a0 = regression.GetA0();
            double a1 = regression.GetA1();
            double a2 = regression.GetA2();

            for(int i=regionstart;i<regionend;i++)
              xMatrix[n,i] -= (((a2*i)+a1)*i+a0);
          }
          break;

        default:
          throw new NotImplementedException(string.Format("Detrending of order {0} is not implemented yet",_order));
      }
    }
    /// <summary>
    /// Processes the spectra in matrix xMatrix.
    /// </summary>
    /// <param name="xMatrix">The matrix of spectra. Each spectrum is a row of the matrix.</param>
    /// <param name="xMean">Output: On return, contains the ensemble mean of the spectra.</param>
    /// <param name="xScale">Not used.</param>
    /// <param name="regionstart">Starting index of the region to process.</param>
    /// <param name="regionend">End index of the region to process.</param>
    void ProcessForPrediction(IMatrix xMatrix, IROVector xMean, IROVector xScale, int regionstart, int regionend)
    {
      int regionlength = regionend - regionstart;

      for(int n=0;n<xMatrix.Rows;n++)
      {
        // 2.) Do linear regression of the current spectrum versus the mean spectrum
        QuickLinearRegression regression = new QuickLinearRegression();
        for(int i=regionstart;i<regionend;i++)
          regression.Add(xMean[i],xMatrix[n,i]);

        double intercept = regression.GetA0();
        double slope = regression.GetA1();

        // 3.) Subtract intercept and divide by slope
        for(int i=regionstart;i<regionend;i++)
          xMatrix[n,i] = (xMatrix[n,i]-intercept)/slope;
      }
    }