vec_math
Class LinearSmoothing

java.lang.Object
  extended by vec_math.LinearSmoothing
Direct Known Subclasses:
LinearSmoothing.Average, SavitzkyGolaySmoothing

public abstract class LinearSmoothing
extends Object

Base class for linear digital smoothing. Linear in this sense means that it uses linear combination of the input values and output values. The most general formular for linear filtering runs as

 ynMckxn+kNdjyn+j
 

Both summations starts at some (normally negative) index K,J, which means that in filtering, data from the 'past' and 'future' can be taken into account. The M+1 ck and the N dk coefficients are fixed and define the filter response. Finite impulse response filter have an N of zero, while N≠0 characterizes infinite (recursive) impulse response filters. Recursive filters normally have a superior performance to FIR filters, but can suffer the problem of instability. This makes them more prone to runaway behaviour for all-purpose filtering than FIR filters. Generally, use IIR filters, if you know precisely, which kind of signal you expect.

Literature:
Numerical Recipies for C, p 558ff.


Nested Class Summary
private static class LinearSmoothing.Average
           
static class LinearSmoothing.File
          Reads the specified column of a data file and does a moving-average filtering.
 
Field Summary
protected  double[] cn
           
protected  double[] dn
           
static int FIR
          Used for nonrecursive filters.
static int IIR
          Used for recursive filters.
protected  int nj
           
protected  int nk
           
protected  int type
           
 
Constructor Summary
protected LinearSmoothing()
           
 
Method Summary
 int getM()
          Returns the number of coefficients used from the data side.
 int getN()
          Returns the number of coefficients used from the smoothened data side.
 int getNj()
          Returns the offset of smoothed data into the past.
 int getNk()
          Returns the offset of data into the past.
 int getType()
          Returns the type of this filter.
 boolean isValid()
          Returns true, if smoothing can be done using this linear filter.
static LinearSmoothing movingAverage(int window)
          Creates a simple running average smoothing with the half-length of the given argument.
protected abstract  void setType()
          Sets the type of this filter.
 double[] smoothAll(double[] input)
          Smoothes an entire data set.
 double[] smoothAll(double[] input, int start, int end)
          Smoothes an entire data set.
 double smoothAt(double[] input, double[] output, int ix, int ox)
          Smoothes one data point at the given indices.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FIR

public static final int FIR
Used for nonrecursive filters.

See Also:
Constant Field Values

IIR

public static final int IIR
Used for recursive filters.

See Also:
Constant Field Values

type

protected int type

cn

protected double[] cn

dn

protected double[] dn

nk

protected int nk

nj

protected int nj
Constructor Detail

LinearSmoothing

protected LinearSmoothing()
Method Detail

setType

protected abstract void setType()
Sets the type of this filter. Is generic to each filter, so no arguments can be passed to this method. The method should be protected, because we do not want anybody to manipulate it from outside.


getType

public int getType()
Returns the type of this filter. FIR filter return the value of FIR, while IIR filter return IIR. If the type is not set, an IllegalArgumentException is thrown.


isValid

public boolean isValid()
Returns true, if smoothing can be done using this linear filter. This method checks if at least one of the two coefficient arrays cn or dn is not equal null.


getNk

public int getNk()
Returns the offset of data into the past. If the measurements are not used (cn == null), zero is returned.

Returns:
A (negative) int, specifiying the look-back size.

getNj

public int getNj()
Returns the offset of smoothed data into the past. If the smoothed measurements are not used (dn == null), zero is returned.

Returns:
A (negative) int, specifiying the look-back size.

getM

public int getM()
Returns the number of coefficients used from the data side. Returns zero if cn == null.

Returns:
The length of the data coefficient array.

getN

public int getN()
Returns the number of coefficients used from the smoothened data side. Returns zero if dn == null.

Returns:
The length of the smoothened data coefficient array.

smoothAt

public double smoothAt(double[] input,
                       double[] output,
                       int ix,
                       int ox)
Smoothes one data point at the given indices. The calculation of the indices is a little bit complicated, since a single index k (negative/positve) may translate to different indices in input and output.
The first index in input that is used is input[Nk+ix], therefore ix must carry k plus the offset in the input array for k=0. In particular, if your data set is at the minimum size, so that you use all of your input data, ix = -Nk. The same is true for the output index ox.

The input and output data arrays must be large enough to contain all relevant smoothing parameters. In particular that means that ix+Nk is non-negative, ix+Nk+cn.length is a valid index in input, ox+Nj is also non-negative, and that ox+Nj+dn.length is a valid index in output. Otherwise, an IllegalArgumentException is thrown.

Parameters:
input - The array of input point
output - The array of (already calculated) output points.
ix - The index where to evaluate the input points.

smoothAll

public double[] smoothAll(double[] input)
Smoothes an entire data set. In the (you want use this in normal cases) very rare case, where future smoothed output data is used to construct the output data at index i (-Nk > dn.length, an iterative process is spawned, starting with output that is identically to the input, but processed further, until stability is reached. Note that the data set of the input data must still be larger than the output data set.

Parameters:
input - The input points
Returns:
A smoothed array of points, starting at index start.

smoothAll

public double[] smoothAll(double[] input,
                          int start,
                          int end)
Smoothes an entire data set. In the (you want use this in normal cases) very rare case, where future smoothed output data is used to construct the output data at index i (-Nk > dn.length, an iterative process is spawned, starting with output that is identically to the input, but processed further, until stability is reached. Note that the data set of the input data must still be larger than the output data set.

This method was never checked!

Parameters:
input - The input points
start - The index of the starting smooth.
end - The index of the end element to smooth.
Returns:
A smoothed array of points, starting at index start.

movingAverage

public static LinearSmoothing movingAverage(int window)
Creates a simple running average smoothing with the half-length of the given argument. Smoothing is done over 2*n+1 data points, it is always centered at the middle.