Class SpectrumTools


  • public class SpectrumTools
    extends java.lang.Object
    Class implements frequency interpolation of spectral peaks. The main idea behind these algorithm is: The resolution of frequency estimates that are solely based on the value of the highest bin of an FFT or DTFT spectrum is limited by the binning of the frequency spectrum, which assuming an FFT spectrum is determined by the number of samples N that have been used to compute the spectra (resolution = 1/N). The functions in this class provide algorithms that fit this frequency by taking the value not only of the highest but also adjacent bins into account. While the peak shape strongly depends on the underlying process, the gaussian interpolation gives typically the best results in most cases.
    Author:
    rstein
    See Also:
    reference: R.J. Steinhagen, "Tune and Chromaticity Diagnostics", in: Proceedings of the CERN Accelerator School, Dourdan, France, 2005
    • Constructor Summary

      Constructors 
      Constructor Description
      SpectrumTools()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double[] computeFrequencyScale​(int nMag)
      compute equidistant frequency axis based on the length of the magnitude spectrum
      static float[] computeFrequencyScaleFloat​(int nMag)
      compute equidistant frequency axis based on the length of the magnitude spectrum
      static double[] computeMagnitudeSpectrum​(double[] data)
      compute magnitude power spectra
      static double[] computeMagnitudeSpectrum​(double[] data, boolean truncateDCNyq)
      compute magnitude power spectra
      static void computeMagnitudeSpectrum​(double[] data, int fromPos, int length, double[] ret, int toPos, boolean truncateDCNyq)
      compute magnitude power spectra
      static float[] computeMagnitudeSpectrum​(float[] data)
      compute magnitude power spectra
      static float[] computeMagnitudeSpectrum​(float[] data, boolean truncateDCNyq)
      compute magnitude power spectra
      static void computeMagnitudeSpectrum​(float[] data, int fromPos, int length, float[] ret, int toPos, boolean truncateDCNyq)
      compute magnitude power spectra
      static double[] computeMagnitudeSpectrum_dB​(double[] data, boolean truncateDCNyq)
      compute magnitude power spectra in decibel
      static void computeMagnitudeSpectrum_dB​(double[] data, int fromPos, int length, double[] ret, int toPos, boolean truncateDCNyq)
      compute magnitude power spectra in decibel
      static float[] computeMagnitudeSpectrum_dB​(float[] data, boolean truncateDCNyq)
      compute magnitude power spectra in decibel
      static void computeMagnitudeSpectrum_dB​(float[] data, int fromPos, int length, float[] ret, int toPos, boolean truncateDCNyq)
      compute magnitude power spectra in decibel
      static double[][] computeMaxima​(double[] data)
      compute maxima
      static double[][] computeMinima​(double[] data)
      compute minima
      static double[] computePhaseSpectrum​(double[] data)
      compute phase spectra ([-PI,+PI])
      static float[] computePhaseSpectrum​(float[] data)
      compute phase spectra ([-PI,+PI])
      static double[][] filterPeaksHarmonics​(double[][] peaks, double[] magnitude, double estimate, boolean useRealAmplitudes)  
      static double[][] filterPeaksSignalToNoise​(double[][] peaks, double snRatio, boolean dBScale)  
      static double interpolateBaryCentre​(double[] data, int index)
      interpolation using a bary-centre approach
      static double interpolateGaussian​(double[] data, int index)
      interpolation using a Gaussian interpolation
      static double interpolateNAFF​(double[] data, int index)
      interpolation using a NAFF/SUSSIX based approach
      static double interpolateParabolic​(double[] data, int index)
      interpolation using a parabolic interpolation
      static double[] interpolateSpectrum​(double[] data, int noversampling)  
      static void main​(java.lang.String[] args)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SpectrumTools

        public SpectrumTools()
    • Method Detail

      • computeFrequencyScale

        public static double[] computeFrequencyScale​(int nMag)
        compute equidistant frequency axis based on the length of the magnitude spectrum
        Parameters:
        nMag - requested length of output vector
        Returns:
        computed [0.0, 0.5] frequency scale
      • computeFrequencyScaleFloat

        public static float[] computeFrequencyScaleFloat​(int nMag)
        compute equidistant frequency axis based on the length of the magnitude spectrum
        Parameters:
        nMag - requested length of output vector
        Returns:
        computed [0.0, 0.5] frequency scale
      • computeMagnitudeSpectrum

        public static double[] computeMagnitudeSpectrum​(double[] data)
        compute magnitude power spectra
        Parameters:
        data - the input data Since due to intrinsic uncertainties the DC and Nyquist frequency components are less representative for the given spectrum, their values are set to their adjacent frequency bins.
        Returns:
        computed magnitude spectrum
        See Also:
        for the expected spectra layout
      • computeMagnitudeSpectrum

        public static double[] computeMagnitudeSpectrum​(double[] data,
                                                        boolean truncateDCNyq)
        compute magnitude power spectra
        Parameters:
        data - the input data
        truncateDCNyq - true: whether to smooth spectra and to ZOH the DC and Nyquist frequencies
        Returns:
        computed magnitude spectrum
        See Also:
        for the expected spectra layout
      • computeMagnitudeSpectrum

        public static void computeMagnitudeSpectrum​(double[] data,
                                                    int fromPos,
                                                    int length,
                                                    double[] ret,
                                                    int toPos,
                                                    boolean truncateDCNyq)
        compute magnitude power spectra
        Parameters:
        data - the input data
        fromPos - start of the data to read
        length - length to read of the input data
        ret - the output data
        toPos - the position to start writing the result to
        truncateDCNyq - true: whether to smooth spectra and to ZOH the DC and Nyquist frequencies
        See Also:
        for the expected spectra layout
      • computeMagnitudeSpectrum

        public static float[] computeMagnitudeSpectrum​(float[] data)
        compute magnitude power spectra
        Parameters:
        data - the input data Since due to intrinsic uncertainties the DC and Nyquist frequency components are less representative for the given spectrum, their values are set to their adjacent frequency bins.
        Returns:
        computed magnitude spectrum
        See Also:
        for the expected spectra layout
      • computeMagnitudeSpectrum

        public static float[] computeMagnitudeSpectrum​(float[] data,
                                                       boolean truncateDCNyq)
        compute magnitude power spectra
        Parameters:
        data - the input data
        truncateDCNyq - true: whether to smooth spectra and to ZOH the DC and Nyquist frequencies
        Returns:
        computed magnitude spectrum
        See Also:
        for the expected spectra layout
      • computeMagnitudeSpectrum

        public static void computeMagnitudeSpectrum​(float[] data,
                                                    int fromPos,
                                                    int length,
                                                    float[] ret,
                                                    int toPos,
                                                    boolean truncateDCNyq)
        compute magnitude power spectra
        Parameters:
        data - the input data
        fromPos - starting position of the data
        length - length of the data
        ret - the output data, should be an array of data.length/2
        toPos - position to write to in the output array
        truncateDCNyq - true: whether to smooth spectra and to ZOH the DC and Nyquist frequencies
        See Also:
        for the expected spectra layout
      • computeMagnitudeSpectrum_dB

        public static double[] computeMagnitudeSpectrum_dB​(double[] data,
                                                           boolean truncateDCNyq)
        compute magnitude power spectra in decibel
        Parameters:
        data - the input data
        truncateDCNyq - true: whether to smooth spectra and to ZOH the DC and Nyquist frequencies
        Returns:
        computed magnitude spectrum in [dB]
        See Also:
        for the expected spectra layout
      • computeMagnitudeSpectrum_dB

        public static void computeMagnitudeSpectrum_dB​(double[] data,
                                                       int fromPos,
                                                       int length,
                                                       double[] ret,
                                                       int toPos,
                                                       boolean truncateDCNyq)
        compute magnitude power spectra in decibel
        Parameters:
        data - the input data
        fromPos - starting position of the data
        length - length of the data
        ret - the output data, should be an array of data.length/2
        toPos - position to write to in the output array
        truncateDCNyq - true: whether to smooth spectra and to ZOH the DC and Nyquist frequencies
        See Also:
        for the expected spectra layout
      • computeMagnitudeSpectrum_dB

        public static float[] computeMagnitudeSpectrum_dB​(float[] data,
                                                          boolean truncateDCNyq)
        compute magnitude power spectra in decibel
        Parameters:
        data - the input data
        truncateDCNyq - true: whether to smooth spectra and to ZOH the DC and Nyquist frequencies
        Returns:
        computed magnitude spectrum in [dB]
        See Also:
        for the expected spectra layout
      • computeMagnitudeSpectrum_dB

        public static void computeMagnitudeSpectrum_dB​(float[] data,
                                                       int fromPos,
                                                       int length,
                                                       float[] ret,
                                                       int toPos,
                                                       boolean truncateDCNyq)
        compute magnitude power spectra in decibel
        Parameters:
        data - the input data
        fromPos - starting position of the data
        length - length of the data
        ret - the output data, should be an array of data.length/2
        toPos - position to write to in the output array
        truncateDCNyq - true: whether to smooth spectra and to ZOH the DC and Nyquist frequencies
        See Also:
        for the expected spectra layout
      • computeMaxima

        public static double[][] computeMaxima​(double[] data)
        compute maxima
        Parameters:
        data - input data
        Returns:
        maxima locations
      • computeMinima

        public static double[][] computeMinima​(double[] data)
        compute minima
        Parameters:
        data - input data
        Returns:
        computed minima
      • computePhaseSpectrum

        public static double[] computePhaseSpectrum​(double[] data)
        compute phase spectra ([-PI,+PI])
        Parameters:
        data - the input data
        Returns:
        computed phase spectrum
        See Also:
        for the expected spectra layout
      • computePhaseSpectrum

        public static float[] computePhaseSpectrum​(float[] data)
        compute phase spectra ([-PI,+PI])
        Parameters:
        data - the input data
        Returns:
        phase spectrum
        See Also:
        for the expected spectra layout
      • filterPeaksHarmonics

        public static double[][] filterPeaksHarmonics​(double[][] peaks,
                                                      double[] magnitude,
                                                      double estimate,
                                                      boolean useRealAmplitudes)
      • filterPeaksSignalToNoise

        public static double[][] filterPeaksSignalToNoise​(double[][] peaks,
                                                          double snRatio,
                                                          boolean dBScale)
      • interpolateBaryCentre

        public static double interpolateBaryCentre​(double[] data,
                                                   int index)
        interpolation using a bary-centre approach
        Parameters:
        data - data array
        index - 0< index < data.length, location of the to be interpolated peak
        Returns:
        interpolated bary centre
      • interpolateGaussian

        public static double interpolateGaussian​(double[] data,
                                                 int index)
        interpolation using a Gaussian interpolation
        Parameters:
        data - data array
        index - 0< index < data.length, location of the to be interpolated peak
        Returns:
        interpolated gauss index
      • interpolateNAFF

        public static double interpolateNAFF​(double[] data,
                                             int index)
        interpolation using a NAFF/SUSSIX based approach
        Parameters:
        data - data array
        index - 0< index < data.length, location of the to be interpolated peak
        Returns:
        NAFF-interpolated peak position
      • interpolateParabolic

        public static double interpolateParabolic​(double[] data,
                                                  int index)
        interpolation using a parabolic interpolation
        Parameters:
        data - data array
        index - 0< index < data.length, location of the to be interpolated peak
        Returns:
        parabolic-interpolated peak position
      • interpolateSpectrum

        public static double[] interpolateSpectrum​(double[] data,
                                                   int noversampling)
      • main

        public static void main​(java.lang.String[] args)