Package wellen
Class FFT
java.lang.Object
wellen.FFT
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:
-
FourierTransform
- The Fast Fourier Transform
-
Field Summary
-
Constructor Summary
ConstructorDescriptionFFT
(int timeSize, float sampleRate) Constructs an FFT that will accept sample buffers that aretimeSize
long and have been recorded with a sample rate ofsampleRate
. -
Method Summary
Modifier and TypeMethodDescriptionint
avgSize()
Returns the number of averages currently being calculated.float
calcAvg
(float lowFreq, float hiFreq) Calculate the average amplitude of the frequency band bounded bylowFreq
andhiFreq
, inclusive.void
forward
(float[] buffer) Performs a forward transform onbuffer
.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 inbuffer
.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
getAverageCenterFrequency
(int i) Returns the center frequency of the ith average band.float
getAvg
(int i) Gets the value of theith
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
instance()
void
inverse
(float[] buffer) Performs an inverse transform of the frequency spectrum and places the result inbuffer
.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 theith
frequency band bys
.void
scaleFreq
(float freq, float s) Scales the amplitude of the requested frequency bya
.void
setBand
(int i, float a) Sets the amplitude of theith
frequency band toa
.void
setFreq
(float freq, float a) Sets the amplitude of the requested frequency in the spectrum toa
.int
specSize()
Returns the size of the spectrum created by this transform.int
timeSize()
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.
-
Field Details
-
HAMMING
public static final int HAMMINGA constant indicating a Hamming window should be used on sample buffers.- See Also:
-
NONE
public static final int NONEA 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 aretimeSize
long and have been recorded with a sample rate ofsampleRate
.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 analyzingsampleRate
- 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
- 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 transformbuffImag
- the imaginary part of the time domain signal to transform
-
setBand
public void setBand(int i, float a) Sets the amplitude of theith
frequency band toa
. You can use this to shape the spectrum before usinginverse()
.- Parameters:
i
- the frequency band to modifya
- the new amplitude
-
scaleBand
public void scaleBand(int i, float s) Scales the amplitude of theith
frequency band bys
. You can use this to shape the spectrum before usinginverse()
.- Parameters:
i
- the frequency band to modifys
- the scaling factor
-
forward
public void forward(float[] buffer) Performs a forward transform onbuffer
.- Parameters:
buffer
- the buffer to analyze
-
inverse
public void inverse(float[] buffer) Performs an inverse transform of the frequency spectrum and places the result inbuffer
.- 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 bespecSize() / 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 octavebandsPerOctave
- 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 totimeSize()/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 toa
.- Parameters:
freq
- the frequency in Hza
- the new amplitude
-
scaleFreq
public void scaleFreq(float freq, float s) Scales the amplitude of the requested frequency bya
.- Parameters:
freq
- the frequency in Hzs
- 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 theith
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 bylowFreq
andhiFreq
, inclusive.- Parameters:
lowFreq
- the lower bound of the bandhiFreq
- 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 inbuffer
.- Parameters:
buffer
- the buffer of samplesstartAt
- 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 spectrumfreqImag
- the imaginary part the frequency spectrumbuffer
- 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.
-