Package wellen

Class FFT

java.lang.Object
wellen.FFT

public class FFT extends Object
implements the Fast Fourier Transform (FFT).

It is an efficient way to calculate the Complex Discrete Fourier Transform. There is not much to say about this class other than the fact that when you want to analyze the spectrum of an audio buffer you will almost always use this class. One restriction of this class is that the audio buffers you want to analyze must have a length that is a power of two. If you try to construct an FFT with a timeSize that is not a power of two, an IllegalArgumentException will be thrown.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    A constant indicating a Hamming window should be used on sample buffers.
    static final int
    A constant indicating no window should be used on sample buffers.
  • Constructor Summary

    Constructors
    Constructor
    Description
    FFT(int timeSize, float sampleRate)
    Constructs an FFT that will accept sample buffers that are timeSize long and have been recorded with a sample rate of sampleRate.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the number of averages currently being calculated.
    float
    calcAvg(float lowFreq, float hiFreq)
    Calculate the average amplitude of the frequency band bounded by lowFreq and hiFreq, inclusive.
    void
    forward(float[] buffer)
    Performs a forward transform on buffer.
    void
    forward(float[] buffReal, float[] buffImag)
    Performs a forward transform on the passed buffers.
    void
    forward(float[] buffer, int startAt)
    Performs a forward transform on values in buffer.
    int
    freqToIndex(float freq)
    Returns the index of the frequency band that contains the requested frequency.
    static float
    get_frequency(float pFrequency)
     
    static float[]
     
    float
    Returns the center frequency of the ith average band.
    float
    getAvg(int i)
    Gets the value of the ith average.
    float
    getBand(int i)
    Returns the amplitude of the requested frequency band.
    float
    Returns the width of each frequency band in the spectrum (in Hz).
    float
    getFreq(float freq)
    Gets the amplitude of the requested frequency in the spectrum.
    float[]
     
    float[]
     
    float[]
     
    float
    indexToFreq(int i)
    Returns the middle frequency of the ith band.
    static FFT
     
    void
    inverse(float[] buffer)
    Performs an inverse transform of the frequency spectrum and places the result in buffer.
    void
    inverse(float[] freqReal, float[] freqImag, float[] buffer)
    Performs an inverse transform of the frequency spectrum represented by freqReal and freqImag and places the result in buffer.
    void
    linAverages(int numAvg)
    Sets the number of averages used when computing the spectrum and spaces the averages in a linear manner.
    void
    logAverages(int minBandwidth, int bandsPerOctave)
    Sets the number of averages used when computing the spectrum based on the minimum bandwidth for an octave and the number of bands per octave.
    void
    Sets the object to not compute averages.
    static void
    perform_forward_transform(float[] pSignal)
     
    void
    scaleBand(int i, float s)
    Scales the amplitude of the ith frequency band by s.
    void
    scaleFreq(float freq, float s)
    Scales the amplitude of the requested frequency by a.
    void
    setBand(int i, float a)
    Sets the amplitude of the ith frequency band to a.
    void
    setFreq(float freq, float a)
    Sets the amplitude of the requested frequency in the spectrum to a.
    int
    Returns the size of the spectrum created by this transform.
    int
    Returns the length of the time domain signal expected by this transform.
    void
    window(int which)
    Sets the window to use on the samples before taking the forward transform.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HAMMING

      public static final int HAMMING
      A constant indicating a Hamming window should be used on sample buffers.
      See Also:
    • NONE

      public static final int NONE
      A constant indicating no window should be used on sample buffers.
      See Also:
  • Constructor Details

    • FFT

      public FFT(int timeSize, float sampleRate)
      Constructs an FFT that will accept sample buffers that are timeSize long and have been recorded with a sample rate of sampleRate. timeSize must be a power of two. This will throw an exception if it is not.
      Parameters:
      timeSize - the length of the sample buffers you will be analyzing
      sampleRate - the sample rate of the audio you will be analyzing
  • Method Details

    • get_frequency

      public static float get_frequency(float pFrequency)
      Parameters:
      pFrequency - frequency
      Returns:
      frequency
    • get_spectrum

      public static float[] get_spectrum()
      Returns:
      spectrum
    • instance

      public static FFT instance()
      Returns:
      instance
    • perform_forward_transform

      public static void perform_forward_transform(float[] pSignal)
      Parameters:
      pSignal - signal
    • forward

      public void forward(float[] buffReal, float[] buffImag)
      Performs a forward transform on the passed buffers.
      Parameters:
      buffReal - the real part of the time domain signal to transform
      buffImag - the imaginary part of the time domain signal to transform
    • setBand

      public void setBand(int i, float a)
      Sets the amplitude of the ith frequency band to a. You can use this to shape the spectrum before using inverse().
      Parameters:
      i - the frequency band to modify
      a - the new amplitude
    • scaleBand

      public void scaleBand(int i, float s)
      Scales the amplitude of the ith frequency band by s. You can use this to shape the spectrum before using inverse().
      Parameters:
      i - the frequency band to modify
      s - the scaling factor
    • forward

      public void forward(float[] buffer)
      Performs a forward transform on buffer.
      Parameters:
      buffer - the buffer to analyze
    • inverse

      public void inverse(float[] buffer)
      Performs an inverse transform of the frequency spectrum and places the result in buffer.
      Parameters:
      buffer - the buffer to place the result of the inverse transform in
    • noAverages

      public void noAverages()
      Sets the object to not compute averages.
    • linAverages

      public void linAverages(int numAvg)
      Sets the number of averages used when computing the spectrum and spaces the averages in a linear manner. In other words, each average band will be specSize() / numAvg bands wide.
      Parameters:
      numAvg - how many averages to compute
    • logAverages

      public void logAverages(int minBandwidth, int bandsPerOctave)
      Sets the number of averages used when computing the spectrum based on the minimum bandwidth for an octave and the number of bands per octave. For example, with audio that has a sample rate of 44100 Hz, logAverages(11, 1) will result in 12 averages, each corresponding to an octave, the first spanning 0 to 11 Hz. To ensure that each octave band is a full octave, the number of octaves is computed by dividing the Nyquist frequency by two, and then the result of that by two, and so on. This means that the actual bandwidth of the lowest octave may not be exactly the value specified.
      Parameters:
      minBandwidth - the minimum bandwidth used for an octave
      bandsPerOctave - how many bands to split each octave into
    • window

      public void window(int which)
      Sets the window to use on the samples before taking the forward transform. If an invalid window is asked for, an error will be reported and the current window will not be changed.
      Parameters:
      which - FourierTransform.HAMMING or FourierTransform.NONE
    • timeSize

      public int timeSize()
      Returns the length of the time domain signal expected by this transform.
      Returns:
      the length of the time domain signal expected by this transform
    • specSize

      public int specSize()
      Returns the size of the spectrum created by this transform. In other words, the number of frequency bands produced by this transform. This is typically equal to timeSize()/2 + 1, see above for an explanation.
      Returns:
      the size of the spectrum
    • getBand

      public float getBand(int i)
      Returns the amplitude of the requested frequency band.
      Parameters:
      i - the index of a frequency band
      Returns:
      the amplitude of the requested frequency band
    • getBandWidth

      public float getBandWidth()
      Returns the width of each frequency band in the spectrum (in Hz). It should be noted that the bandwidth of the first and last frequency bands is half as large as the value returned by this function.
      Returns:
      the width of each frequency band in Hz.
    • freqToIndex

      public int freqToIndex(float freq)
      Returns the index of the frequency band that contains the requested frequency.
      Parameters:
      freq - the frequency you want the index for (in Hz)
      Returns:
      the index of the frequency band that contains freq
    • indexToFreq

      public float indexToFreq(int i)
      Returns the middle frequency of the ith band.
      Parameters:
      i - the index of the band you want to middle frequency of
      Returns:
      Returns the middle frequency of the ith band.
    • getAverageCenterFrequency

      public float getAverageCenterFrequency(int i)
      Returns the center frequency of the ith average band.
      Parameters:
      i - which average band you want the center frequency of.
      Returns:
      Returns the center frequency of the ith average band.
    • getFreq

      public float getFreq(float freq)
      Gets the amplitude of the requested frequency in the spectrum.
      Parameters:
      freq - the frequency in Hz
      Returns:
      the amplitude of the frequency in the spectrum
    • setFreq

      public void setFreq(float freq, float a)
      Sets the amplitude of the requested frequency in the spectrum to a.
      Parameters:
      freq - the frequency in Hz
      a - the new amplitude
    • scaleFreq

      public void scaleFreq(float freq, float s)
      Scales the amplitude of the requested frequency by a.
      Parameters:
      freq - the frequency in Hz
      s - the scaling factor
    • avgSize

      public int avgSize()
      Returns the number of averages currently being calculated.
      Returns:
      the length of the averages array
    • getAvg

      public float getAvg(int i)
      Gets the value of the ith average.
      Parameters:
      i - the average you want the value of
      Returns:
      the value of the requested average band
    • calcAvg

      public float calcAvg(float lowFreq, float hiFreq)
      Calculate the average amplitude of the frequency band bounded by lowFreq and hiFreq, inclusive.
      Parameters:
      lowFreq - the lower bound of the band
      hiFreq - the upper bound of the band
      Returns:
      the average of all spectrum values within the bounds
    • forward

      public void forward(float[] buffer, int startAt)
      Performs a forward transform on values in buffer.
      Parameters:
      buffer - the buffer of samples
      startAt - the index to start at in the buffer. there must be at least timeSize() samples between the starting index and the end of the buffer. If there aren't, an error will be issued and the operation will not be performed.
    • inverse

      public void inverse(float[] freqReal, float[] freqImag, float[] buffer)
      Performs an inverse transform of the frequency spectrum represented by freqReal and freqImag and places the result in buffer.
      Parameters:
      freqReal - the real part of the frequency spectrum
      freqImag - the imaginary part the frequency spectrum
      buffer - the buffer to place the inverse transform in
    • getSpectrum

      public float[] getSpectrum()
      Returns:
      the spectrum of the last FourierTransform.forward() call.
    • getRealPart

      public float[] getRealPart()
      Returns:
      the real part of the last FourierTransform.forward() call.
    • getImaginaryPart

      public float[] getImaginaryPart()
      Returns:
      the imaginary part of the last FourierTransform.forward() call.