Michael Thomas Flanagan's Java Scientific Library

SurfaceSmooth Class:     Surface Smoothing

     

Last update: 11 September 2012
Main Page of Michael Thomas Flanagan's Java Scientific Library

This class contains methods for smoothing two dimensional data, i.e. a surface, z = f(x, y). A choice of smoothing method is offered:
The class also contains methods for: See CurveSmooth for the one dimensional equivalent of this class, i.e. for curve smoothing
See ThreeDimensionalSmooth for the three dimensional equivalent of this class.

import directive: import flanagan.analysis.SurfaceSmooth;

SUMMARY OF METHODS

Constructors public SurfaceSmooth(double[] xData, double[] yData, double[][] zData)
public SurfaceSmooth(double[][] zData)
public SurfaceSmooth(double xData, double yData, Matrix zData)
public SurfaceSmooth(Matrix zData)
public SurfaceSmooth(float[] xData, float[] yData, float[][] zData)
public SurfaceSmooth(float[][] zData)
public SurfaceSmooth(long[] xData, long[] yData, long[][] zData)
public SurfaceSmooth(long[][] zData)
public SurfaceSmooth(int[] xData, int[] yData, int[][] zData)
public SurfaceSmooth(int[][] zData)
public SurfaceSmooth(BigDecimal[] xData, BigDecimal[] yData, BigDecimal[][] zData)
public SurfaceSmooth(BigDecimal[][] zData)
public SurfaceSmooth(BigInteger[] xData, BigInteger[] yData, BigInteger[][] zData)
public SurfaceSmooth(BigInteger[][] zData)
Smoothing Savitzky-Golay Smoothed Curves public double[][] savitzkyGolay(int sgFilterWidthx, int sgFilterWidthy)
public double[][] savitzkyGolay(int sgFilterWidth)
public double[][] getSavitzkyGolaySmoothedValues()
Smoothed Derivatives public double[][][] savitzkyGolay(int sgFilterWidthx, int sgFilterWidthy, int m, int n)
public double[][] getSavitzDerivatives()
The Filter public double[][] getSGcoefficients()
public void setSGpolyDegree(int degree)
public int getSGpolyDegree()
public int[][] getSGPolyIndices()
public static double[][] savitzkyGolayFilter(int nBackwardx, int nForwardx, int nBackwardy, int nForwardy, int degree)
public static int[][] filterIndices(int degree)
Moving Average Window public double[][] movingAverage(int sgWindowWidthx, int sgWindowWidthy)
public double[][] movingAverage(int sgWindowWidth)
public double[][] getMovingAverageValues()
public BigDecimal[][] movingAverageAsBigDecimal(int sgWindowWidthx, int sgWindowWidthy)
public BigDecimal[][] movingAverageAsBigDecimal(int sgWindowWidth)
public BigDecimal[][] getMovingAverageValuesAsBigDecimal()
Extent of smoothing public double extentMovingAverage()
public double extentSavitzlyGolay()
Interpolation Savitzky-Golay public double interpolateSavitzkyGolay(double xi, double yi)
Moving average public double interpolateMovingAverage(double xi, double yi)
Plot Savitzky-Golay x-direction section public double plotSavitzkyGolayX(double yValue)
public double plotSavitzkyGolayX(int yIndex)
y-direction section public double plotSavitzkyGolayY(double xValue)
public double plotSavitzkyGolayY(int xIndex)
Moving average x-direction section public double plotMovingAverageX(double yValue)
public double plotMovingAverageX(int yIndex)
y-direction section public double plotMovingAverageY(double xValue)
public double plotMovingAverageY(int xIndex)




CONSTRUCTORS

public SurfaceSmooth(double[] xData, double[] yData, double[][] zData)
public SurfaceSmooth(double[] xData, double[] yData, Matrix zData)
public SurfaceSmooth(float[] xData, float[] yData, float[][] zData)
public SurfaceSmooth(long[] xData, long[] yData, long[][] zData)
public SurfaceSmooth(int[] xData, int[] yData, int[][] zData)
public SurfaceSmooth(BigDecimal[] xData, BigDecimal[] yData, BigDecimal[][] zData)
public SurfaceSmooth(BigInteger[] xData, BigInteger[] yData, BigInteger[][] zData)
Usage:                      SurfaceSmooth ssm = new SurfaceSmooth(xData, yData, zData);
Creates an instance of SurfaceSmooth. The data is entered as arrays of the x, y and z values, arguments xData, yData and zData, for a surface, z = f(x, y). The data may be entered as type double, float, long, int, BigDecimal, BigInteger or as any appropriate data type through Matrix. All data, except types BigDecimal and BigInteger, are converted to type double before processing. BigInteger is converted to BigDecimal. Moving average window smoothing is performed in arbitrary precision arithmetic for BigDecimal and BigInteger entered data. Savitzky-Golay filtering is performed in double precision arithmetic for BigDecimal and BigInteger entered data. The data are ordered as ascending x and ascending y values before processing.

The ordering of the data in the matrix z [argument zData] should be as:

    f(x[0], y[0]),    f(x[1], y[0]),    f(x[2], y[0]) . . . . .    f(x[m-1], y[0])
    f(x[0], y[1]),    f(x[1], y[1]),    f(x[2], y[1]) . . . . .    f(x[m-1], y[1])
    f(x[0], y[2]),    f(x[1], y[2]),    f(x[2], y[2]) . . . . .    f(x[m-1], y[2])
    . . . .
    . . . .
    . . . .
    f(x[0], y[n-1]),    f(x[1], y[n-1]),    f(x[2], y[n-1]) . . . . .    f(x[m-1], y[n-1])

where m is the number of x values in argument xData and n is the number of y values in argument yData, i.e. the 2D array z [argument zData] should be dimensioned as zData[n][m] where n is the number of y [yData] values and m is the number of x [xData] values.

public SurfaceSmooth(double[][] zData)
public SurfaceSmooth(Matrix zData)
public SurfaceSmooth(float[][] zData)
public SurfaceSmooth(long[][] zData)
public SurfaceSmooth(int[][] zData)
public SurfaceSmooth(BigDecimal[][] zData)
public SurfaceSmooth(BigInteger[][] zData)
Usage:                      SurfaceSmooth ssm = new SurfaceSmooth(zData);
Creates an instance of SurfaceSmooth. The data is entered as an array of z values, argument zData, for a surface, z = f(x, y). As no x or y values are entered the data is treated as data sampled at equal x value and y value intervals. Values of 0, 1, 2 ... to m-1 are assigned to the m x-values. Values of 0, 1, 2 ... to n-1 are assigned to the n y-values. The data may be entered as type double, float, long, int, BigDecimal, BigInteger or as any appropriate data type through Matrix. All data, except types BigDecimal and BigInteger, are converted to type double before processing. BigInteger is converted to BigDecimal. Moving average window smoothing is performed in arbitrary precision arithmetic for BigDecimal and BigInteger entered data. Savitzky-Golay filtering is performed in double precision arithmetic for BigDecimal and BigInteger entered data.



SMOOTHING METHODS

SAVITZKY-GOLAY FILTER METHODS

Savitzky-Golay Smoothed Curves
public double[][] savitzkyGolay(int sgFilterWidthx, int sgFilterWidthy)
public double[][] savitzkyGolay(int sgFilterWidth)
public double[][] getsavitzkyGolaySmoothedValues()
Usage:                      smoothedData = ssm.savitzkyGolay(sgFilterWidthx, sgFilterWidthy);
This method returns the smoothed z values, for the data entered via the constructor arguments, using a two dimensional Savitzky-Golay filter of width sgFilterWidthx points in the x dimension and of width sgFilterWidthy points in the y dimension. The default value of the degree of the fitting polynomial is 4. This value may be reset using the setSGpolyDeg method.

Usage:                      smoothedData = ssm.savitzkyGolay(sgFilterWidth);
This method returns the smoothed z values, for the data entered via the constructor arguments, using a two dimensional Savitzky-Golay filter of width sgFilterWidth points in the x dimension and of the same width, sgFilterWidth points, in the y dimension. The default value of the degree of the fitting polynomial is 4. This value may be reset using the setSGpolyDeg method.

Usage:                      smoothedData = ssm.getSavitzkyGolaySmoothedValues();
This method returns the smoothed z values if the above Savitzky-Golay filter method has already been called.


Savitzky-Golay Smoothed Derivatives
public double[][][] savitzkyGolay(int sgFilterWidthx, int sgFilterWidthy, int m, int n)
public double[][] getSavitzkyGolayDerivatives()
Usage:                      smoothedDataPlusDeriv = ssm.savitzkyGolay(sgFilterWidthx, sgFilterWidthy, m, n);
This method returns the smoothed z values and the smoothed derivatives , for the data entered via the constructor arguments, using a two dimensional Savitzky-Golay filter of width sgFilterWidthx points in the x dimension and of width sgFilterWidthy points in the y dimension. The arguments m and n contains the values of the required orders of the derivative, m and n. The smoothed z values are returned, in the above usage, in smoothedDataPlusDeriv[0], the derivatives are returned in smoothedDataPlusDeriv[1].
The sum of m [m] and n [n] must be less than or equal to the degree of the fitting polynomial. The default value of this degree is 4. This value may be reset using the method setSGpolyDegree.

Usage:                      smoothedData = ssm.getSavitzkyDerivatives();
This method returns the Savitzky-Golay smoothed derivatives . The Savitzky-Golay derivative method must already have been called as the last derivative method and the values of m and n will be that used in this last call.

The Savitzky-Golay Filter
public double[][] getSGcoefficients()
Usage:                      sgCoefficients = ssm.getSGcoefficients();
This method returns the Savitzky-Golay filter coefficients, c, used in the smoothing

mw and nw are the lengths of the filter in the y and x directions respectively, ml and nl are the number of points preceeding the data point on which the filter is operating, zk,l, in the y and x directions respectively and mu and nu are the number of points following the data point, zk,l,in the y and x directions respectively. If the filter is used as a smoothing filter sk,l is the smoothed value of the data point zk,l and the c values used are those stored in the zeroth row of the returned matrix sgCoefficients. Each row of sgCoefficients is the array of coefficients c[mw][nw] arranged linearly as the mw blocks of rows each of length nw. Each row, when applied to the data gives the smoothed m,nth derivatives. The zeroth row, indices 0,0, gives the smoothed zeroth derivative, i.e. the smoothed data values, the second row, indices 0,1, gives the derivatives . The pair of indices associated with each row may be obtained by calling the method
getSGpolyIndices() and they correspond to the indices of the coefficients of the fitting polynomial (see below). The default value of the degree of the fitting polynomial is 4:

This value may be reset using the setSGpolyDegree method.
The smoothing method, savitzkyGolay, uses a symmetrical filter, i.e. ml = mu and nl = nu.

public int[] getSGpolyIndices()
Usage:                      indicesPairs = ssm.getSGpolyIndices();
This method returns the pairs of indices of the fitting polynomial (see immediately above). The value in indices[i][0] is the first index of the ith coefficient of the polynomial and is also the power of y in the ith term. The value in indices[i][1] is the second index of the ith coefficient of the polynomial and is also the power of x in the ith term. These returned pairs are also ordered to match the rows in the returned c matrix (See
savitzkyGolayFilter method above.

public void setSGpolyDegree(int degree)
public int getSGpolyDegree()
Usage:                      ssm.setSGpolyDegree(degree);
This method resets the degree of the Savitzky-Golay fitting polynomial. The default value is 4 if this method is not called.

Usage:                      deg = ssm.getSGpolyDegree();
This method returns the degree of the Savitzky-Golay fitting polynomial. The default value is 4.

Static methods for returning a Savitzky-Golay filter
public static double[][] savitzkyGolayFilter(int nBackwardx, int nForwardx, int nBackwardy, int nForwardy, int polyDegree)
Usage:                      sgCoefficients = SurfaceSmooth.savitzkyGolayFilter(nBackwardx, nForwardx, nBackwardx, nForwardx, polyDegree);
This method returns the coefficients, c, of a two dimensional Savitzky-Golay filter of x dimension length, mw [ = nBackWardx + nForWardx + 1], and y dimension length, nw [= nBackWardy + nForWardy + 1], with a fitting polynomial of degree, polyDegree. A description of the coefficients, c, their application and their ordering within the returned two dimensional array, sgCoefficients. may be found
above. The arguments nBackWardx [ml] and nBackWardy[nl] are the number of points preceeding the data point, upon which the filter is operating, in the x and y dimensions respectively and nForWardx [mu] and nForWardy [nu] are the number of points following the data point in the x and y dimensions respectively.

public static int[][] filterIndices(int degree)
Usage:                      indicesPairs = SurfaceSmooth.filterIndices(deg);
This method returns the pairs of indices of a fitting polynomial of degree deg. The value in indices[i][0] is the first index of the ith coefficient of the polynomial and is also the power of y in the ith term. The value in indices[i][1] is the second index of the ith coefficient of the polynomial and is also the power of x in the ith term. An example of such a fitting polynomial and its coefficients is shown above. These returned pairs are also ordered to match the rows in the corresponding c matrix.



MOVING AVERAGE WINDOW SMOOTHING
public double[][] movingAverage(int windowWidthx, int windowWidthy)
public double[][] movingAverage(int windowWidth)
public BigDecimal[][] movingAverageAsBigDecimal(int windowWidthx, int windowWidthy)
public BigDecimal[][] movingAverageAsBigDecimal(int windowWidth)
public double[][] getMovingAverageValues()
public BigDecimal[][] getMovingAverageValuesAsBigDecimal()
Usage:                      smoothedData = ssm.movingAverage(windowWidthx, windowWidthy);
This method returns the smoothed z values, for the data entered via the constructor arguments, using a moving average window of nw [argument windowWidthx] points in the x dimension and of mw [argument windowWidthy] points in the y dimension:

sk,l is the smoothed value of the data point zk,l. The entered value for the number of points in the windows are adjusted to the next higher odd number if an even number has been entered. The values of nl, nu, ml and mu are truncated appropriately if, when close to the data extremes, they fall below or above the first or last data point respectively. The smoothed data is returned as type double.

Usage:                      smoothedData = ssm.movingAverage(windowWidth);
As immediately above for movingAverage(windowWidthx, windowWidthy) with a square window, i.e. windowWidthx = windowWidth and windowWidthy = windowWidth.

Usage:                      smoothedData = ssm.movingAverageAsBigDecimal(windowWidthx, windowWidthy);
As above for movingAverage(windowWidthx, windowWidthy) with the exception that the smoothed data is returned as type BigDecimal. If the data was entered as type BigDecimal or BigInteger the smoothing will have been performed in arbitrary arithmetic.

Usage:                      smoothedData = ssm.movingAverageAsBigDecimal(windowWidth);
As above for movingAverage(windowWidthx, windowWidthy) with a square window, i.e. windowWidthx = windowWidth and windowWidthy = windowWidth, and with the exception that the smoothed data is returned as type BigDecimal. If the data was entered as type BigDecimal or BigInteger the smoothing will have been performed in arbitrary arithmetic.

Usage:                      smoothedData = ssm.getMovingAverageValues();
This method returns the smoothed z values if the above moving average window method has already been called.

Usage:                      smoothedData = ssm.getMovingAverageValuesAsBigDecimal();
This method returns the smoothed z values if the above moving average window method has already been called. The smoothed values are returned as type BigDecimal.



EXTENT OF SMOOTHING

public double extentSavitzkyGolay()
public double extentMovingAverage()
These methods return the value of the function
    
where zi,j is the original z value of the ith, jth data point, si,j is its smoothed value, zmin is the minimum value of the zi,j, zmax is the maximum value of the zi,j and n is the number of x coordinates and m is the number of y coordinates.

Usage:                      extent = ssm.extentSavitzkyGolay();
In this method si,j is the Savitzky-Golay smoothed value.

Usage:                      extent = ssm.extentMovingAverage();
In this method si,j is its moving average smoothed value.



INTERPOLATION

public double interpolateSavitzkyGolay(double xi, double yi)
public double interpolateMovingAverage(double xi, double yi)
Usage:                      zi = ssm.interpolateSavitzkyGolay(xi, yi);
This method returns the interpolated value of z [zi] for the supplied x [argument xi] and y [argument yi] values for the Savitzky-Golay smoothed data. The interpolation procedure uses the
BiCubicSpline class. A Savitzky-Golay smoothing method must have been called previously.

Usage:                      zi = ssm.interpolateMovingAverage(xi, yi);
This method returns the interpolated value of z [zi] for the supplied x [argument xi] and y [argument yi] values for the moving average smoothed data. The interpolation procedure uses the BiCubicSpline class. A moving average smoothing method must have been called previously.



PLOT

Savitzky-Golay
public double plotSavitzkyGolayX(double yValue)
public double plotSavitzkyGolayX(int yIndex)
public double plotSavitzkyGolayY(double xValue)
public double plotSavitzkyGolayY(int xIndex)
Usage:                      ssm.plotSavitzkyGolayX(yValue);
This method displays a plot of both the original data and the Savitzky-Golay smoothed data for a section through the surface parallel to the x-axis at a value of y supplied as the argument yValue. The value, yValue, must be one of the values supplied in the array yData via a
Constructor.

Usage:                      ssm.plotSavitzkyGolayX(yIndex);
This method displays a plot of both the original data and the Savitzky-Golay smoothed data for a section through the surface parallel to the x-axis at the value of y whose index in the array yData entered via a Constructor is the integer supplied as the argument yIndex. NB indices start at 0.

Usage:                      ssm.plotSavitzkyGolayY(xValue);
This method displays a plot of both the original data and the Savitzky-Golay smoothed data for a section through the surface parallel to the y-axis at a value of x supplied as the argument xValue. The value, xValue, must be one of the values supplied in the array xData via a Constructor.

Usage:                      ssm.plotSavitzkyGolayY(xIndex);
This method displays a plot of both the original data and the Savitzky-Golay smoothed data for a section through the surface parallel to the y-axis at the value of x whose index in the array xData entered via a Constructor is the integer supplied as the argument xIndex. NB indices start at 0.

Moving average
public double plotMovingAverageX(double yValue)
public double plotMovingAverageX(int yIndex)
public double plotMovingAverageY(double xValue)
public double plotMovingAverageY(int xIndex)
Usage:                      ssm.plotMovingAverageX(yValue);
This method displays a plot of both the original data and the Moving Average smoothed data for a section through the surface parallel to the x-axis at a value of y supplied as the argument yValue. The value, yValue, must be one of the values supplied in the array yData via a Constructor.

Usage:                      ssm.plotMovingAverageX(yIndex);
This method displays a plot of both the original data and the Moving Average smoothed data for a section through the surface parallel to the x-axis at the value of y whose index in the array yData entered via a Constructor is the integer supplied as the argument yIndex. NB indices start at 0.

Usage:                      ssm.plotMovingAverageY(xValue);
This method displays a plot of both the original data and the Moving Average smoothed data for a section through the surface parallel to the y-axis at a value of x supplied as the argument xValue. The value, xValue, must be one of the values supplied in the array xData via a Constructor.

Usage:                      ssm.plotMovingAverageY(xIndex);
This method displays a plot of both the original data and the Moving Average smoothed data for a section through the surface parallel to the y-axis at the value of x whose index in the array xData entered via a Constructor is the integer supplied as the argument xIndex. NB indices start at 0.



OTHER CLASSES USED BY THIS CLASS

This class uses the following classes in this library:


This page was prepared by Dr Michael Thomas Flanagan