private void calculateNoise(double[] arrayIn,int nx,int ny){
  DoubleArrayPointer array=new DoubleArrayPointer(arrayIn);
  initializeNoise();
  if (nx < MINIMUM_PIXEL_WIDTH) {
    nx=nx * ny;
    ny=1;
  }
  if (calculateNoiseShortRow(array,nx,ny)) {
    return;
  }
  DoubleArrayPointer rowpix;
  int nrows=0, nrows2=0;
  long ngoodpix=0;
  double[] differences2=new double[nx];
  double[] differences3=new double[nx];
  double[] differences5=new double[nx];
  double[] diffs2=new double[ny];
  double[] diffs3=new double[ny];
  double[] diffs5=new double[ny];
  for (int jj=0; jj < ny; jj++) {
    rowpix=array.copy(jj * nx);
    int ii=0;
    ii=findNextValidPixelWithNullCheck(nx,rowpix,ii);
    if (ii == nx) {
      continue;
    }
    double v1=getNextPixelAndCheckMinMax(rowpix,ii);
    ngoodpix++;
    ii=findNextValidPixelWithNullCheck(nx,rowpix,++ii);
    if (ii == nx) {
      continue;
    }
    double v2=getNextPixelAndCheckMinMax(rowpix,ii);
    ngoodpix++;
    ii=findNextValidPixelWithNullCheck(nx,rowpix,++ii);
    if (ii == nx) {
      continue;
    }
    double v3=getNextPixelAndCheckMinMax(rowpix,ii);
    ngoodpix++;
    ii=findNextValidPixelWithNullCheck(nx,rowpix,++ii);
    if (ii == nx) {
      continue;
    }
    double v4=getNextPixelAndCheckMinMax(rowpix,ii);
    ngoodpix++;
    ii=findNextValidPixelWithNullCheck(nx,rowpix,++ii);
    if (ii == nx) {
      continue;
    }
    double v5=getNextPixelAndCheckMinMax(rowpix,ii);
    ngoodpix++;
    ii=findNextValidPixelWithNullCheck(nx,rowpix,++ii);
    if (ii == nx) {
      continue;
    }
    double v6=getNextPixelAndCheckMinMax(rowpix,ii);
    ngoodpix++;
    ii=findNextValidPixelWithNullCheck(nx,rowpix,++ii);
    if (ii == nx) {
      continue;
    }
    double v7=getNextPixelAndCheckMinMax(rowpix,ii);
    ngoodpix++;
    ii=findNextValidPixelWithNullCheck(nx,rowpix,++ii);
    if (ii == nx) {
      continue;
    }
    double v8=getNextPixelAndCheckMinMax(rowpix,ii);
    ngoodpix++;
    int nvals=0;
    int nvals2=0;
    for (ii++; ii < nx; ii++) {
      ii=findNextValidPixelWithNullCheck(nx,rowpix,ii);
      if (ii == nx) {
        continue;
      }
      double v9=getNextPixelAndCheckMinMax(rowpix,ii);
      if (!(v5 == v6 && v6 == v7)) {
        differences2[nvals2]=Math.abs(v5 - v7);
        nvals2++;
      }
      if (!(v3 == v4 && v4 == v5 && v5 == v6 && v6 == v7)) {
        differences3[nvals]=Math.abs(2 * v5 - v3 - v7);
        differences5[nvals]=Math.abs(N6 * v5 - N4 * v3 - N4 * v7 + v1 + v9);
        nvals++;
      }
 else {
        ngoodpix++;
      }
      v1=v2;
      v2=v3;
      v3=v4;
      v4=v5;
      v5=v6;
      v6=v7;
      v7=v8;
      v8=v9;
    }
    ngoodpix+=nvals;
    if (nvals == 0) {
      continue;
    }
 else     if (nvals == 1) {
      if (nvals2 == 1) {
        diffs2[nrows2]=differences2[0];
        nrows2++;
      }
      diffs3[nrows]=differences3[0];
      diffs5[nrows]=differences5[0];
    }
 else {
      if (nvals2 > 1) {
        diffs2[nrows2]=quickSelect(differences2,nvals);
        nrows2++;
      }
      diffs3[nrows]=quickSelect(differences3,nvals);
      diffs5[nrows]=quickSelect(differences5,nvals);
    }
    nrows++;
  }
  computeMedianOfValuesEachRow(nrows,nrows2,diffs2,diffs3,diffs5);
  setNoiseResult(ngoodpix);
}

private void calculateNoise ( double [ ] arrayIn , int nx , int ny ) { DoubleArrayPointer array = new DoubleArrayPointer ( arrayIn ) ; initializeNoise ( ) ; if ( nx < MINIMUM_PIXEL_WIDTH ) { nx = nx * ny ; ny = 1 ; } if ( calculateNoiseShortRow ( array , nx , ny ) ) { return ; } DoubleArrayPointer rowpix ; int nrows = 0 , nrows2 = 0 ; long ngoodpix = 0 ; double [ ] differences2 = new double [ nx ] ; double [ ] differences3 = new double [ nx ] ; double [ ] differences5 = new double [ nx ] ; double [ ] diffs2 = new double [ ny ] ; double [ ] diffs3 = new double [ ny ] ; double [ ] diffs5 = new double [ ny ] ; for ( int jj = 0 ; jj < ny ; jj ++ ) { rowpix = array . copy ( jj * nx ) ; int ii = 0 ; ii = findNextValidPixelWithNullCheck ( nx , rowpix , ii ) ; if ( ii == nx ) { continue ; } double v1 = getNextPixelAndCheckMinMax ( rowpix , ii ) ; ngoodpix ++ ; ii = findNextValidPixelWithNullCheck ( nx , rowpix , ++ ii ) ; if ( ii == nx ) { continue ; } double v2 = getNextPixelAndCheckMinMax ( rowpix , ii ) ; ngoodpix ++ ; ii = findNextValidPixelWithNullCheck ( nx , rowpix , ++ ii ) ; if ( ii == nx ) { continue ; } double v3 = getNextPixelAndCheckMinMax ( rowpix , ii ) ; ngoodpix ++ ; ii = findNextValidPixelWithNullCheck ( nx , rowpix , ++ ii ) ; if ( ii == nx ) { continue ; } double v4 = getNextPixelAndCheckMinMax ( rowpix , ii ) ; ngoodpix ++ ; ii = findNextValidPixelWithNullCheck ( nx , rowpix , ++ ii ) ; if ( ii == nx ) { continue ; } double v5 = getNextPixelAndCheckMinMax ( rowpix , ii ) ; ngoodpix ++ ; ii = findNextValidPixelWithNullCheck ( nx , rowpix , ++ ii ) ; if ( ii == nx ) { continue ; } double v6 = getNextPixelAndCheckMinMax ( rowpix , ii ) ; ngoodpix ++ ; ii = findNextValidPixelWithNullCheck ( nx , rowpix , ++ ii ) ; if ( ii == nx ) { continue ; } double v7 = getNextPixelAndCheckMinMax ( rowpix , ii ) ; ngoodpix ++ ; ii = findNextValidPixelWithNullCheck ( nx , rowpix , ++ ii ) ; if ( ii == nx ) { continue ; } double v8 = getNextPixelAndCheckMinMax ( rowpix , ii ) ; ngoodpix ++ ; int nvals = 0 ; int nvals2 = 0 ; for ( ii ++ ; ii < nx ; ii ++ ) { ii = findNextValidPixelWithNullCheck ( nx , rowpix , ii ) ; if ( ii == nx ) { continue ; } double v9 = getNextPixelAndCheckMinMax ( rowpix , ii ) ; if ( ! ( v5 == v6 && v6 == v7 ) ) { differences2 [ nvals2 ] = Math . abs ( v5 - v7 ) ; nvals2 ++ ; } if ( ! ( v3 == v4 && v4 == v5 && v5 == v6 && v6 == v7 ) ) { differences3 [ nvals ] = Math . abs ( 2 * v5 - v3 - v7 ) ; differences5 [ nvals ] = Math . abs ( N6 * v5 - N4 * v3 - N4 * v7 + v1 + v9 ) ; nvals ++ ; } else { ngoodpix ++ ; } v1 = v2 ; v2 = v3 ; v3 = v4 ; v4 = v5 ; v5 = v6 ; v6 = v7 ; v7 = v8 ; v8 = v9 ; } ngoodpix += nvals ; if ( nvals == 0 ) { continue ; } else if ( nvals == 1 ) { if ( nvals2 == 1 ) { diffs2 [ nrows2 ] = differences2 [ 0 ] ; nrows2 ++ ; } diffs3 [ nrows ] = differences3 [ 0 ] ; diffs5 [ nrows ] = differences5 [ 0 ] ; } else { if ( nvals2 > 1 ) { diffs2 [ nrows2 ] = quickSelect ( differences2 , nvals ) ; nrows2 ++ ; } diffs3 [ nrows ] = quickSelect ( differences3 , nvals ) ; diffs5 [ nrows ] = quickSelect ( differences5 , nvals ) ; } nrows ++ ; } computeMedianOfValuesEachRow ( nrows , nrows2 , diffs2 , diffs3 , diffs5 ) ; setNoiseResult ( ngoodpix ) ; }
estimate the median and background noise in the input image using 2nd , 3rd and 5th order median absolute differences . the noise in the background of the image is calculated using the mad algorithms developed for deriving the signal to noise ratio in spectra ( see issue # 42 of the st - ecf newsletter , http : / / www . stecf . org / documents / newsletter / ) 3rd order : noise = 1 . 482602 / sqrt ( 6 ) * median ( abs ( 2 * flux ( i ) - flux ( i - 2 ) - flux ( i + 2 ) ) ) the returned estimates are the median of the values that are computed for each row of the image .
