gov.nist.microanalysis.EPQLibrary
Class SpectrumUtils

java.lang.Object
  extended by gov.nist.microanalysis.EPQLibrary.SpectrumUtils

public final class SpectrumUtils
extends java.lang.Object

A set of utilities for handling common ISpectrumData related operations.

Copyright: Pursuant to title 17 Section 105 of the United States Code this software is not subject to copyright protection and is in the public domain

Company: National Institute of Standards and Technology

Version:
1.0
Author:
Nicholas W. M. Ritchie

Field Summary
static double E_MnKa
          E_MnKa - The energy of the Mn Ka line in eV.
 
Constructor Summary
SpectrumUtils()
           
 
Method Summary
static EditableSpectrum addNoiseToSpectrum(ISpectrumData src, double k)
          Creates a new spectrum from the specified source spectrum by adding Poissonian simulated counting statistic noise.
static boolean areCompatible(ISpectrumData sd1, ISpectrumData sd2)
          areCompatible - Are the channel count, channel width and zero offset on these two spectra equal?
static double avgCounts(ISpectrumData sd)
          avgCounts - Returns the average number of counts over all channels
static double avgEnergyForChannel(ISpectrumData sd, int ch)
           
static int bound(ISpectrumData sd, int ch)
          bound - Returns ch bounded so that it exists on the interval 0 to sd.getChannelCount()-1.
static int channelForEnergy(ISpectrumData sd, double e)
          channelForEnergy - Returns the index of the channel which contains the specified channel.
static double dotProduct(ISpectrumData spec1, ISpectrumData spec2)
          dotProduct - Returns the dot product of the channels of each specified spectrum.
static double DuaneHuntLimit(ISpectrumData spec)
          DuaneHuntLimit - Returns an estimate of the Duane-Hunt limit in eV.
static double[] energyArray(ISpectrumData sd)
          energyArray - Creates an array of doubles representing the energy axis.
static double energyToWavelength(double energy)
          energyToWavelength - Converts energy to wavelength
static double fwhmToGaussianWidth(double fwhm)
          fwhmToGaussianWidth - Converts between full width-half maximum and Gaussian width.
static double gaussian(double dE, double sigma)
          gaussian - Computes the Gaussian function.
static double getBeamEnergy(ISpectrumData sd)
          getBeamEnergy - Returns the nominal beam energy for the specified spectrum.
static double getFWHMAtMnKA(ISpectrumData src, double def)
          getFWHMAtMnKA - Extracts the FWHM at MnKa from the SpectrumProperties associated with the source ISpectrumData.
static double integrate(ISpectrumData sd, double minE, double maxE)
          Integrate the counts between minE and maxE taking into account the partial channels when minE and maxE don't exactly match channel boundaries.
static double linewidth_eV(double e, double fwhmAtE0, double e0)
          linewidth_eV - The line width measured by a SiLi detector depends upon the energy of the line.
static double linewidth(double e, double fwhmAtE0, double e0)
          linewidth - The line width measured by a SiLi detector depends upon the energy of the line.
static int maxChannel(ISpectrumData sd)
          maxChannel - Returns the highest channel that has the maximum count value.
static double maxEnergyForChannel(ISpectrumData sd, int ch)
           
static int minChannel(ISpectrumData sd)
          minChannel - Returns the highest channel that has the minimum count value.
static double minEnergyForChannel(ISpectrumData sd, int ch)
          energyForChannel - Compute the energy of the specified channel.
static ISpectrumData[] partition(ISpectrumData src, int nParts)
           Randomly subdivides the src spectrum into nParts different spectra.
static java.lang.String spectrumDataToText(ISpectrumData sd, double eLow, double eHigh, boolean withEnergies)
          spectrumDataToText - Convert the spectral data in the specified energy range into a String desscribing the raw channel data.
static java.lang.String spectrumPropertiesToText(SpectrumProperties sp)
          spectrumPropertiesToText - Converts a SpectrumProperties object into a String containing a property per line.
static EditableSpectrum subSampleSpectrum(ISpectrumData sd, double liveTime)
           Create a new ISpectrumData object containing a spectrum derived from the specified spectrum.
static double sumCounts(ISpectrumData sd, int minCh, int maxCh)
          sumCounts - Returns the number of counts in the channels in the range [minCh, maxCh)
static java.awt.Color toColor(ISpectrumData spec)
          toColor - Converts a spectrum to a color in a manner that is inspired by the way our eyes convert multiple wavelengths into three signals.
static double[] toDoubleArray(ISpectrumData spec)
          toDoubleArray - Returns the spectrum channel data as an array of doubles.
static java.awt.image.BufferedImage toImage(ISpectrumData spec, int height)
          toImage - Displays the spectrum as a grey-scale bitmap.
static int[] toIntArray(ISpectrumData spec)
          toIntArray - Returns the spectrum channel data as an array of int.
static double totalCounts(ISpectrumData sd)
          totalCounts - Returns the number of counts in all channels
static void verifyCompatibility(ISpectrumData sd1, ISpectrumData sd2)
          verifyCompatibility - Verifys that two spectra are compatible.
static double wavelengthToEnergy(double wavelength)
          wavelengthToEnergy - Converts wavelength to energy
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

E_MnKa

public static final double E_MnKa
E_MnKa - The energy of the Mn Ka line in eV.

Constructor Detail

SpectrumUtils

public SpectrumUtils()
Method Detail

areCompatible

public static boolean areCompatible(ISpectrumData sd1,
                                    ISpectrumData sd2)
areCompatible - Are the channel count, channel width and zero offset on these two spectra equal?

Parameters:
sd1 - ISpectrumData
sd2 - ISpectrumData
Returns:
boolean

verifyCompatibility

public static void verifyCompatibility(ISpectrumData sd1,
                                       ISpectrumData sd2)
                                throws EPQException
verifyCompatibility - Verifys that two spectra are compatible. If they are compatible the function performs no action. If they are not compatible the functions throws an EPQException specifying the reason.

Parameters:
sd1 - ISpectrumData
sd2 - ISpectrumData
Throws:
EPQException

channelForEnergy

public static int channelForEnergy(ISpectrumData sd,
                                   double e)
channelForEnergy - Returns the index of the channel which contains the specified channel. The channel index may be outside of the bounds of valid channel indices for this spectrum.

Parameters:
sd - ISpectrumData
e - double - the energy in eV
Returns:
int - the channel index

minEnergyForChannel

public static double minEnergyForChannel(ISpectrumData sd,
                                         int ch)
energyForChannel - Compute the energy of the specified channel. The zeroth channel runs from [zeroOffset,zeroOffset+getChannelWidth)]. The first from [zeroOffset+getChannelWidth(),zeroOffset+2*getChannelWidth()) etc. This function returns zeroOffset for channel zero, zeroOffset+sd.getChannelWidth for channel 1, ....

Parameters:
sd - ISpectrumData
ch - int - the channel index
Returns:
double - the energy in eV

avgEnergyForChannel

public static double avgEnergyForChannel(ISpectrumData sd,
                                         int ch)

maxEnergyForChannel

public static double maxEnergyForChannel(ISpectrumData sd,
                                         int ch)

bound

public static int bound(ISpectrumData sd,
                        int ch)
bound - Returns ch bounded so that it exists on the interval 0 to sd.getChannelCount()-1. If ch is less than zero, it returns 0. If ch is greater than or equal to sd.getChannelCount() then it returns sd.getChannelCount()-1.

Parameters:
sd - ISpectrumData
ch - int
Returns:
int

linewidth

public static double linewidth(double e,
                               double fwhmAtE0,
                               double e0)
linewidth - The line width measured by a SiLi detector depends upon the energy of the line. Given the width of a reference line at a known energy it is possible to calculate the approximate width of the line at another energy. This function does that using Fiori and Newbury's equation.

Parameters:
e - double - In Joules
fwhmAtE0 - double - In Joules
e0 - double - In Joules
Returns:
double - In Joules

linewidth_eV

public static double linewidth_eV(double e,
                                  double fwhmAtE0,
                                  double e0)
linewidth_eV - The line width measured by a SiLi detector depends upon the energy of the line. Given the width of a reference line at a known energy it is possible to calculate the approximate width of the line at another energy. This function does that using Fiori and Newbury's equation.

Parameters:
e - double - In eV
fwhmAtE0 - double - In eV
e0 - double - In eV
Returns:
double - In eV

fwhmToGaussianWidth

public static double fwhmToGaussianWidth(double fwhm)
fwhmToGaussianWidth - Converts between full width-half maximum and Gaussian width. (i.e. 2*a such that exp(-0.5 (x/a)^2)=1/2))

Parameters:
fwhm - double
Returns:
double

gaussian

public static double gaussian(double dE,
                              double sigma)
gaussian - Computes the Gaussian function.

Parameters:
dE - double -
sigma - double - Gaussian width
Returns:
double

maxChannel

public static int maxChannel(ISpectrumData sd)
maxChannel - Returns the highest channel that has the maximum count value.

Parameters:
sd - ISpectrumData
Returns:
int

minChannel

public static int minChannel(ISpectrumData sd)
minChannel - Returns the highest channel that has the minimum count value.

Parameters:
sd - ISpectrumData
Returns:
int

sumCounts

public static double sumCounts(ISpectrumData sd,
                               int minCh,
                               int maxCh)
sumCounts - Returns the number of counts in the channels in the range [minCh, maxCh)

Parameters:
sd - ISpectrumData
minCh - int
maxCh - int
Returns:
double

integrate

public static double integrate(ISpectrumData sd,
                               double minE,
                               double maxE)
Integrate the counts between minE and maxE taking into account the partial channels when minE and maxE don't exactly match channel boundaries.

Parameters:
sd -
minE -
maxE -
Returns:
double

totalCounts

public static double totalCounts(ISpectrumData sd)
totalCounts - Returns the number of counts in all channels

Parameters:
sd - ISpectrumData
Returns:
double

avgCounts

public static double avgCounts(ISpectrumData sd)
avgCounts - Returns the average number of counts over all channels

Parameters:
sd - ISpectrumData
Returns:
double

getBeamEnergy

public static double getBeamEnergy(ISpectrumData sd)
getBeamEnergy - Returns the nominal beam energy for the specified spectrum. Usually this is the number in the SpectrumProprties.BeamEnergy. When this isn't available the beam energy is estimate from the spectrum data by observing the highest channel with non-zero counts. Finally, all else failing the beam energy is estimated from the number of channels in the spectrum. Use DuaneHunt for a better estimate.

Parameters:
sd - ISpectrumData
Returns:
double - in eV

getFWHMAtMnKA

public static double getFWHMAtMnKA(ISpectrumData src,
                                   double def)
getFWHMAtMnKA - Extracts the FWHM at MnKa from the SpectrumProperties associated with the source ISpectrumData. If the ISpectrumData does not define a resolution then the value in the argument def is returned.

Parameters:
src - ISpectrumData
def - double - The default value (in eV)
Returns:
double - in eV

dotProduct

public static double dotProduct(ISpectrumData spec1,
                                ISpectrumData spec2)
dotProduct - Returns the dot product of the channels of each specified spectrum.

Parameters:
spec1 - ISpectrumData
spec2 - ISpectrumData
Returns:
double

toDoubleArray

public static double[] toDoubleArray(ISpectrumData spec)
toDoubleArray - Returns the spectrum channel data as an array of doubles.

Parameters:
spec -
Returns:
double[]

toIntArray

public static int[] toIntArray(ISpectrumData spec)
toIntArray - Returns the spectrum channel data as an array of int.

Parameters:
spec -
Returns:
int[]

energyArray

public static double[] energyArray(ISpectrumData sd)
energyArray - Creates an array of doubles representing the energy axis.

Parameters:
sd - A spectrum
Returns:
double[] of energies in eV

toColor

public static java.awt.Color toColor(ISpectrumData spec)
toColor - Converts a spectrum to a color in a manner that is inspired by the way our eyes convert multiple wavelengths into three signals.

Parameters:
spec -
Returns:
Color

toImage

public static java.awt.image.BufferedImage toImage(ISpectrumData spec,
                                                   int height)
toImage - Displays the spectrum as a grey-scale bitmap. The width of the bitmap in pixel is the number of channels. The height is as specified.

Parameters:
spec -
height -
Returns:
BufferedImage

energyToWavelength

public static double energyToWavelength(double energy)
energyToWavelength - Converts energy to wavelength

Parameters:
energy - Joules
Returns:
In Meters

wavelengthToEnergy

public static double wavelengthToEnergy(double wavelength)
wavelengthToEnergy - Converts wavelength to energy

Parameters:
wavelength - - Meters
Returns:
Joules

spectrumDataToText

public static java.lang.String spectrumDataToText(ISpectrumData sd,
                                                  double eLow,
                                                  double eHigh,
                                                  boolean withEnergies)
spectrumDataToText - Convert the spectral data in the specified energy range into a String desscribing the raw channel data.

Parameters:
sd - - The spectrum data
eLow - - In eV
eHigh - - In eV
withEnergies - - Should the string contain the energy for each bin
Returns:
String

spectrumPropertiesToText

public static java.lang.String spectrumPropertiesToText(SpectrumProperties sp)
spectrumPropertiesToText - Converts a SpectrumProperties object into a String containing a property per line. Each line consists of the 'property name' = 'text description'

Parameters:
sp - The SpectrumProperties object
Returns:
String

DuaneHuntLimit

public static double DuaneHuntLimit(ISpectrumData spec)
DuaneHuntLimit - Returns an estimate of the Duane-Hunt limit in eV. Seems to work well on "real" spectra - less well on NISTMonte spectra in which the Bremsstrahlung seems to drop exponentially rather than linearly. Returns Double.NaN if the algorithm is does not find a suitable candidate.

Parameters:
spec -
Returns:
In eV

subSampleSpectrum

public static EditableSpectrum subSampleSpectrum(ISpectrumData sd,
                                                 double liveTime)
                                          throws EPQException

Create a new ISpectrumData object containing a spectrum derived from the specified spectrum. The specified spectrum must have the SpectrumProperties.LiveTime property defined and the argument liveTime must be less than or equal to the numeric value in SpectrumProperties.LiveTime

The strategy used by this method is not particularly CPU efficient but seems to be strictly statistically rigorous. Randomly assign a time to each x-ray event. If the time is less than liveTime then accept the event; otherwise reject it. This mechanims scales perfectly from infinitesimal acquisition times all the way to the full source spectrum live time.

Parameters:
sd -
liveTime -
Returns:
EditableSpectrum
Throws:
EPQException - If SpectrumProperties.LiveTime not defined or if liveTime too large.

addNoiseToSpectrum

public static EditableSpectrum addNoiseToSpectrum(ISpectrumData src,
                                                  double k)
Creates a new spectrum from the specified source spectrum by adding Poissonian simulated counting statistic noise. This method is appropriate for adding noise to a simulated spectrum such as computed by NISTMonte. If you apply this method to a real spectrum, the resulting spectrum will have too much noise. Use subSampleSpectrum instead to rescale a 'real' spectrum.

Parameters:
src - The source spectrum
k - A multipler used to linearly rescale the source spectrum data
Returns:
EditableSpectrum

partition

public static ISpectrumData[] partition(ISpectrumData src,
                                        int nParts)

Randomly subdivides the src spectrum into nParts different spectra. The counts in src are randomly assigned to the resulting spectra. This method is interesting because the resulting spectra are similar in character to the src spectrum at 1.0/nParts of the acquisition live time.

Consider if you partition a spectrum into 4 parts. If each of these parts remains in the same class as the source spectrum then the source spectrum is likely to be firmly within the class. However if one or more of the subdivided spectra are not in same class then it is likely the final spectrum is close to the edge of the class.

Parameters:
src - The source spectrum
nParts - The number of spectra into which to subdivide the source spectrum
Returns:
An array of spectra