peakml.math
Class Signal

java.lang.Object
  extended by peakml.math.Signal

public class Signal
extends java.lang.Object

Implementation of a approaches for interpreting and analysing signals. In the context of this project a signal is made up of mass spectrometry data or data from closely related technologies.


Nested Class Summary
 class Signal.Extremes
           
 
Constructor Summary
Signal()
          This constructor creates an empty signal (no elements).
Signal(double[] yvals)
          This constructor creates a signal out of the given y-values.
Signal(double[] xvals, double[] yvals)
          This constructor creates a signal out of the given x- and y-values.
Signal(int size)
          This constructor creates a signal of the given size.
Signal(Signal signal)
          Copy-constructor, which copies the contents of the given signal to this signal.
 
Method Summary
 double compareTo(Signal other)
          Compares the given signal to this signal.
 org.jfree.chart.JFreeChart createGraph(java.lang.String name, java.lang.String xlabel, java.lang.String ylabel)
           
 java.awt.image.BufferedImage createGraphImage(java.lang.String name, java.lang.String xlabel, java.lang.String ylabel, int width, int height)
           
 Signal.Extremes getLocalExtremes(int windowsize)
           
 java.util.Vector<java.lang.Double> getLocalMaxima(int windowsize)
           
 java.util.Vector<java.lang.Double> getLocalMinima(int windowsize)
           
 double getMaxX()
          Returns the maximum x-value.
 double getMaxY()
          Returns the maximum y-value.
 double getMeanY()
          Returns the mean y-value.
 double getMinX()
          Returns the minimum x-value.
 double getMinY()
          Returns the minimum y-value.
 double getPeakX()
          Returns the x-value where the signal reaches its maximum y-value.
 int getSize()
          Returns the number of data-points in this signal.
 double[] getX()
          Returns the internal array with all the x-values.
 double[][] getXY()
          Returns a matrix with the x- and y-values.
 double[] getY()
          Returns the internal array with all the y-values.
 double getY(double x)
          Returns the y-value at the given x-value.
 int indexMaxY()
           
 void Init(int size)
          Initializes the signal to the given size.
 Signal lowessSmooth()
          Applies a Lowess smoother to the signal and returns the resulting new signal.
 void normalize()
          This function scales the y-values between 0 and 1.
 void normalize(double max)
          This function scales the y-values between 0 and the given max.
 double[] pearsonsCorrelation(Signal other)
          Calculates the Pearson's correlation between this and the given signal.
 Signal savitzkyGolaySmooth()
          Applies a Savitzky-Golay smoother to the signal and returns the resulting new signal.
 Signal savitzkyGolaySmooth(SavitzkyGolayFilter.Points points)
          Applies a Savitzky-Golay smoother to the signal and returns the resulting new signal.
 Signal smooth(Filter filter)
          Applies the given filter to the y-values and returns the resulting new signal.
 double spearmanCorrelation(Signal other)
          Calculates the Spearman correlation between this and the given signal.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Signal

public Signal()
This constructor creates an empty signal (no elements).


Signal

public Signal(int size)
This constructor creates a signal of the given size. The x-values are initialized in ascending order and the y-values initialized to 0.

Parameters:
size - The size of the signal.

Signal

public Signal(double[] yvals)
This constructor creates a signal out of the given y-values. The size of the signal is set to the length of the given array. The x-values are initialized in ascending order and the contents of the given array is copied to the y-values.

Parameters:
yvals - The y-values to initialize the signal with.

Signal

public Signal(double[] xvals,
              double[] yvals)
       throws java.lang.NullPointerException,
              java.lang.IllegalArgumentException
This constructor creates a signal out of the given x- and y-values. Both the arrays need to have the same length, otherwise an exception is thrown.

Parameters:
xvals - The x-values.
yvals - The y-values.
Throws:
java.lang.NullPointerException - Thrown when null was passed to one of both of the parameters.
java.lang.IllegalArgumentException - Thrown when the size of the arrays is not equal.

Signal

public Signal(Signal signal)
Copy-constructor, which copies the contents of the given signal to this signal.

Parameters:
signal - The signal to copy.
Method Detail

Init

public void Init(int size)
Initializes the signal to the given size. Beware that the newly created arrays are not initialized.

Parameters:
size - The size to initialize the signal to.

getX

public double[] getX()
Returns the internal array with all the x-values. This is intended for accessing the x-values only and should not be changed unless the signal needs to be adapted.

Returns:
The array with all the x-values.

getY

public double[] getY()
Returns the internal array with all the y-values. This is intended for accessing the y-values only and should not be changed unless the signal needs to be adapted.

Returns:
The array with all the y-values.

getY

public double getY(double x)
Returns the y-value at the given x-value. This method tries to find the real value when it is available, otherwise the y-value is estimated by looking for the x-value smaller than the given x and the x-value larger than the given x and the mean of the two y-values is returned. If the x-value is smaller or larger than the minimum or maximum x-value 0 is returned.

Parameters:
x - The x-value.
Returns:
The y-value corresponding to the given x-value.

getXY

public double[][] getXY()
Returns a matrix with the x- and y-values. The x-values can be retrieved with matrix[0] and the y-values with matrix[1].

Returns:
A matrix with the x- and y-values.

getSize

public int getSize()
Returns the number of data-points in this signal. This is basically the size of both the x- and y-value arrays (which are required to be the same).

Returns:
The number of data-points in this signal.

getMinX

public double getMinX()
Returns the minimum x-value.

Returns:
The minimum x-value.

getMinY

public double getMinY()
Returns the minimum y-value. This value is calculated in this method.

Returns:
The minimum y-value.

getMaxX

public double getMaxX()
Returns the maximum x-value.

Returns:
The maximum x-value.

getMaxY

public double getMaxY()
Returns the maximum y-value. This value is calculated in this method.

Returns:
The maximum y-value.

indexMaxY

public int indexMaxY()
Returns:

getMeanY

public double getMeanY()
Returns the mean y-value. This value is calculated in this method.

Returns:
The mean y-value.

getPeakX

public double getPeakX()
Returns the x-value where the signal reaches its maximum y-value. If there is a tie between 2 or more values the smallest x-value is returned.

Returns:
The x-value of the maximum y-value.

normalize

public void normalize()
This function scales the y-values between 0 and 1. It calculates the maximum y-value and divides all the y-values with thus maximum, effectively scaling the data. Effectively getMaxY() and normalize() are called.


normalize

public void normalize(double max)
This function scales the y-values between 0 and the given max.

Parameters:
max - The maximum y-value to scale to.

savitzkyGolaySmooth

public Signal savitzkyGolaySmooth()
Applies a Savitzky-Golay smoother to the signal and returns the resulting new signal. The number of points is set to SavitzkyGolayFilter.Points#NINE, which seems to give overall good results without affecting the signal itself too much. For more information see SavitzkyGolayFilter.

Returns:
The smoothed version of this signal.

savitzkyGolaySmooth

public Signal savitzkyGolaySmooth(SavitzkyGolayFilter.Points points)
Applies a Savitzky-Golay smoother to the signal and returns the resulting new signal. For more information see SavitzkyGolayFilter.

Parameters:
points - The number of points to use in the smoother.
Returns:
The smoothed version of this signal.

lowessSmooth

public Signal lowessSmooth()
Applies a Lowess smoother to the signal and returns the resulting new signal. For more information see LoessFilter.

Returns:
The smoothed version of this signal.

smooth

public Signal smooth(Filter filter)
Applies the given filter to the y-values and returns the resulting new signal.

Returns:
The smoothed version of this signal.

compareTo

public double compareTo(Signal other)
Compares the given signal to this signal. Essentially the difference in area under the two curves is calculated, resulting in a single value which if small indicates the two signals are very similar and if large indicates the two signals are very unlike.

The method starts by copying both signals and normalizing them to 1. The minimum and maximum x-value is calculated for the combination of both the signals. By starting at the minimum value and incrementing by 1 all the y-value differences between both signals are calculated and summed. This is the returned value.

Parameters:
other - The signal to compare this signal to.
Returns:
The total difference in y-values between both the signals.

pearsonsCorrelation

public double[] pearsonsCorrelation(Signal other)
Calculates the Pearson's correlation between this and the given signal. This method does the book keeping in order to call Statistical.pearsonsCorrelation(double[], double[]). Only the correlation is returned, even though the called method also returns test values.

Parameters:
other - The signal to compare this signal to.
Returns:
The Pearson's correlation.

spearmanCorrelation

public double spearmanCorrelation(Signal other)
Calculates the Spearman correlation between this and the given signal. This method does the book keeping in order to call Statistical.spearmanCorrelation(double[], double[]). Only the correlation is returned, even though the called method also returns test values.

Parameters:
other - The signal to compare this signal to.
Returns:
The Pearson's correlation.

getLocalExtremes

public Signal.Extremes getLocalExtremes(int windowsize)

getLocalMinima

public java.util.Vector<java.lang.Double> getLocalMinima(int windowsize)

getLocalMaxima

public java.util.Vector<java.lang.Double> getLocalMaxima(int windowsize)

createGraph

public org.jfree.chart.JFreeChart createGraph(java.lang.String name,
                                              java.lang.String xlabel,
                                              java.lang.String ylabel)

createGraphImage

public java.awt.image.BufferedImage createGraphImage(java.lang.String name,
                                                     java.lang.String xlabel,
                                                     java.lang.String ylabel,
                                                     int width,
                                                     int height)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object