📄 fastfouriertransformer.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.math.transform;import java.io.Serializable;import org.apache.commons.math.analysis.*;import org.apache.commons.math.complex.*;import org.apache.commons.math.MathException;/** * Implements the <a href="http://mathworld.wolfram.com/FastFourierTransform.html"> * Fast Fourier Transform</a> for transformation of one-dimensional data sets. * For reference, see <b>Applied Numerical Linear Algebra</b>, ISBN 0898713897, * chapter 6. * <p> * There are several conventions for the definition of FFT and inverse FFT, * mainly on different coefficient and exponent. Here the equations are listed * in the comments of the corresponding methods.</p> * <p> * We require the length of data set to be power of 2, this greatly simplifies * and speeds up the code. Users can pad the data with zeros to meet this * requirement. There are other flavors of FFT, for reference, see S. Winograd, * <i>On computing the discrete Fourier transform</i>, Mathematics of Computation, * 32 (1978), 175 - 199.</p> * * @version $Revision: 620312 $ $Date: 2008-02-10 12:28:59 -0700 (Sun, 10 Feb 2008) $ * @since 1.2 */public class FastFourierTransformer implements Serializable { /** serializable version identifier */ static final long serialVersionUID = 5138259215438106000L; /** array of the roots of unity */ private Complex omega[] = new Complex[0]; /** * |omegaCount| is the length of lasted computed omega[]. omegaCount * is positive for forward transform and negative for inverse transform. */ private int omegaCount = 0; /** * Construct a default transformer. */ public FastFourierTransformer() { super(); } /** * Transform the given real data set. * <p> * The formula is $ y_n = \Sigma_{k=0}^{N-1} e^{-2 \pi i nk/N} x_k $ * </p> * * @param f the real data array to be transformed * @return the complex transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] transform(double f[]) throws MathException, IllegalArgumentException { return fft(f, false); } /** * Transform the given real function, sampled on the given interval. * <p> * The formula is $ y_n = \Sigma_{k=0}^{N-1} e^{-2 \pi i nk/N} x_k $ * </p> * * @param f the function to be sampled and transformed * @param min the lower bound for the interval * @param max the upper bound for the interval * @param n the number of sample points * @return the complex transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] transform( UnivariateRealFunction f, double min, double max, int n) throws MathException, IllegalArgumentException { double data[] = sample(f, min, max, n); return fft(data, false); } /** * Transform the given complex data set. * <p> * The formula is $ y_n = \Sigma_{k=0}^{N-1} e^{-2 \pi i nk/N} x_k $ * </p> * * @param f the complex data array to be transformed * @return the complex transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] transform(Complex f[]) throws MathException, IllegalArgumentException { computeOmega(f.length); return fft(f); } /** * Transform the given real data set. * <p> * The formula is $y_n = (1/\sqrt{N}) \Sigma_{k=0}^{N-1} e^{-2 \pi i nk/N} x_k$ * </p> * * @param f the real data array to be transformed * @return the complex transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] transform2(double f[]) throws MathException, IllegalArgumentException { double scaling_coefficient = 1.0 / Math.sqrt(f.length); return scaleArray(fft(f, false), scaling_coefficient); } /** * Transform the given real function, sampled on the given interval. * <p> * The formula is $y_n = (1/\sqrt{N}) \Sigma_{k=0}^{N-1} e^{-2 \pi i nk/N} x_k$ * </p> * * @param f the function to be sampled and transformed * @param min the lower bound for the interval * @param max the upper bound for the interval * @param n the number of sample points * @return the complex transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] transform2( UnivariateRealFunction f, double min, double max, int n) throws MathException, IllegalArgumentException { double data[] = sample(f, min, max, n); double scaling_coefficient = 1.0 / Math.sqrt(n); return scaleArray(fft(data, false), scaling_coefficient); } /** * Transform the given complex data set. * <p> * The formula is $y_n = (1/\sqrt{N}) \Sigma_{k=0}^{N-1} e^{-2 \pi i nk/N} x_k$ * </p> * * @param f the complex data array to be transformed * @return the complex transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] transform2(Complex f[]) throws MathException, IllegalArgumentException { computeOmega(f.length); double scaling_coefficient = 1.0 / Math.sqrt(f.length); return scaleArray(fft(f), scaling_coefficient); } /** * Inversely transform the given real data set. * <p> * The formula is $ x_k = (1/N) \Sigma_{n=0}^{N-1} e^{2 \pi i nk/N} y_n $ * </p> * * @param f the real data array to be inversely transformed * @return the complex inversely transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] inversetransform(double f[]) throws MathException, IllegalArgumentException { double scaling_coefficient = 1.0 / f.length; return scaleArray(fft(f, true), scaling_coefficient); } /** * Inversely transform the given real function, sampled on the given interval. * <p> * The formula is $ x_k = (1/N) \Sigma_{n=0}^{N-1} e^{2 \pi i nk/N} y_n $ * </p> * * @param f the function to be sampled and inversely transformed * @param min the lower bound for the interval * @param max the upper bound for the interval * @param n the number of sample points * @return the complex inversely transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] inversetransform( UnivariateRealFunction f, double min, double max, int n) throws MathException, IllegalArgumentException { double data[] = sample(f, min, max, n); double scaling_coefficient = 1.0 / n; return scaleArray(fft(data, true), scaling_coefficient); } /** * Inversely transform the given complex data set. * <p> * The formula is $ x_k = (1/N) \Sigma_{n=0}^{N-1} e^{2 \pi i nk/N} y_n $ * </p> * * @param f the complex data array to be inversely transformed * @return the complex inversely transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] inversetransform(Complex f[]) throws MathException, IllegalArgumentException { computeOmega(-f.length); // pass negative argument double scaling_coefficient = 1.0 / f.length; return scaleArray(fft(f), scaling_coefficient); } /** * Inversely transform the given real data set. * <p> * The formula is $x_k = (1/\sqrt{N}) \Sigma_{n=0}^{N-1} e^{2 \pi i nk/N} y_n$ * </p> * * @param f the real data array to be inversely transformed * @return the complex inversely transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] inversetransform2(double f[]) throws MathException, IllegalArgumentException { double scaling_coefficient = 1.0 / Math.sqrt(f.length); return scaleArray(fft(f, true), scaling_coefficient); } /** * Inversely transform the given real function, sampled on the given interval. * <p> * The formula is $x_k = (1/\sqrt{N}) \Sigma_{n=0}^{N-1} e^{2 \pi i nk/N} y_n$ * </p> * * @param f the function to be sampled and inversely transformed * @param min the lower bound for the interval * @param max the upper bound for the interval * @param n the number of sample points * @return the complex inversely transformed array * @throws MathException if any math-related errors occur * @throws IllegalArgumentException if any parameters are invalid */ public Complex[] inversetransform2( UnivariateRealFunction f, double min, double max, int n) throws MathException, IllegalArgumentException { double data[] = sample(f, min, max, n); double scaling_coefficient = 1.0 / Math.sqrt(n); return scaleArray(fft(data, true), scaling_coefficient); } /** * Inversely transform the given complex data set.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -