Java Code Examples for org.apache.commons.math3.util.ArithmeticUtils#isPowerOfTwo()

The following examples show how to use org.apache.commons.math3.util.ArithmeticUtils#isPowerOfTwo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FastSineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FST algorithm (including inverse). The first element of the
 * data set is required to be {@code 0}.
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 *   not a power of two, or the first element of the data array is not zero
 */
protected double[] fst(double[] f) throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
                Integer.valueOf(f.length));
    }
    if (f[0] != 0.0) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.FIRST_ELEMENT_NOT_ZERO,
                Double.valueOf(f[0]));
    }
    final int n = f.length;
    if (n == 1) {       // trivial case
        transformed[0] = 0.0;
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.0;
    x[n >> 1] = 2.0 * f[n >> 1];
    for (int i = 1; i < (n >> 1); i++) {
        final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
        final double b = 0.5 * (f[i] - f[n - i]);
        x[i]     = a + b;
        x[n - i] = a - b;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FST result for the original array
    transformed[0] = 0.0;
    transformed[1] = 0.5 * y[0].getReal();
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = -y[i].getImaginary();
        transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1];
    }

    return transformed;
}
 
Example 2
Source File: FastCosineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FCT algorithm (including inverse).
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two plus one
 */
protected double[] fct(double[] f)
    throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(
            LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
            Integer.valueOf(f.length));
    }
    if (n == 1) {       // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    x[n >> 1] = f[n >> 1];
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);
        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);
        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);
        x[i] = a - b;
        x[n - i] = a + b;
        t1 += c;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();
    transformed[1] = t1;
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = y[i].getReal();
        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();
    }
    transformed[n] = y[n >> 1].getReal();

    return transformed;
}
 
Example 3
Source File: FastSineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FST algorithm (including inverse). The first element of the
 * data set is required to be {@code 0}.
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 *   not a power of two, or the first element of the data array is not zero
 */
protected double[] fst(double[] f) throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
                Integer.valueOf(f.length));
    }
    if (f[0] != 0.0) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.FIRST_ELEMENT_NOT_ZERO,
                Double.valueOf(f[0]));
    }
    final int n = f.length;
    if (n == 1) {       // trivial case
        transformed[0] = 0.0;
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.0;
    x[n >> 1] = 2.0 * f[n >> 1];
    for (int i = 1; i < (n >> 1); i++) {
        final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
        final double b = 0.5 * (f[i] - f[n - i]);
        x[i]     = a + b;
        x[n - i] = a - b;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FST result for the original array
    transformed[0] = 0.0;
    transformed[1] = 0.5 * y[0].getReal();
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = -y[i].getImaginary();
        transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1];
    }

    return transformed;
}
 
Example 4
Source File: FastCosineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FCT algorithm (including inverse).
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two plus one
 */
protected double[] fct(double[] f)
    throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(
            LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
            Integer.valueOf(f.length));
    }
    if (n == 1) {       // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    x[n >> 1] = f[n >> 1];
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);
        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);
        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);
        x[i] = a - b;
        x[n - i] = a + b;
        t1 += c;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();
    transformed[1] = t1;
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = y[i].getReal();
        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();
    }
    transformed[n] = y[n >> 1].getReal();

    return transformed;
}
 
Example 5
Source File: FastSineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FST algorithm (including inverse). The first element of the
 * data set is required to be {@code 0}.
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two, or the first element of the data array is not zero
 */
protected double[] fst(double[] f) throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
                Integer.valueOf(f.length));
    }
    if (f[0] != 0.0) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.FIRST_ELEMENT_NOT_ZERO,
                Double.valueOf(f[0]));
    }
    final int n = f.length;
    if (n == 1) {       // trivial case
        transformed[0] = 0.0;
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.0;
    x[n >> 1] = 2.0 * f[n >> 1];
    for (int i = 1; i < (n >> 1); i++) {
        final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
        final double b = 0.5 * (f[i] - f[n - i]);
        x[i]     = a + b;
        x[n - i] = a - b;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FST result for the original array
    transformed[0] = 0.0;
    transformed[1] = 0.5 * y[0].getReal();
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = -y[i].getImaginary();
        transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1];
    }

    return transformed;
}
 
Example 6
Source File: FastCosineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FCT algorithm (including inverse).
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two plus one
 */
protected double[] fct(double[] f)
    throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(
            LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
            Integer.valueOf(f.length));
    }
    if (n == 1) {       // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    x[n >> 1] = f[n >> 1];
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);
        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);
        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);
        x[i] = a - b;
        x[n - i] = a + b;
        t1 += c;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();
    transformed[1] = t1;
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = y[i].getReal();
        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();
    }
    transformed[n] = y[n >> 1].getReal();

    return transformed;
}
 
Example 7
Source File: FastSineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FST algorithm (including inverse). The first element of the
 * data set is required to be {@code 0}.
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 *   not a power of two, or the first element of the data array is not zero
 */
protected double[] fst(double[] f) throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
                Integer.valueOf(f.length));
    }
    if (f[0] != 0.0) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.FIRST_ELEMENT_NOT_ZERO,
                Double.valueOf(f[0]));
    }
    final int n = f.length;
    if (n == 1) {       // trivial case
        transformed[0] = 0.0;
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.0;
    x[n >> 1] = 2.0 * f[n >> 1];
    for (int i = 1; i < (n >> 1); i++) {
        final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
        final double b = 0.5 * (f[i] - f[n - i]);
        x[i]     = a + b;
        x[n - i] = a - b;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FST result for the original array
    transformed[0] = 0.0;
    transformed[1] = 0.5 * y[0].getReal();
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = -y[i].getImaginary();
        transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1];
    }

    return transformed;
}
 
Example 8
Source File: FastCosineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FCT algorithm (including inverse).
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two plus one
 */
protected double[] fct(double[] f)
    throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(
            LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
            Integer.valueOf(f.length));
    }
    if (n == 1) {       // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    x[n >> 1] = f[n >> 1];
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);
        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);
        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);
        x[i] = a - b;
        x[n - i] = a + b;
        t1 += c;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();
    transformed[1] = t1;
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = y[i].getReal();
        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();
    }
    transformed[n] = y[n >> 1].getReal();

    return transformed;
}
 
Example 9
Source File: FastSineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FST algorithm (including inverse). The first element of the
 * data set is required to be {@code 0}.
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two, or the first element of the data array is not zero
 */
protected double[] fst(double[] f) throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
                Integer.valueOf(f.length));
    }
    if (f[0] != 0.0) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.FIRST_ELEMENT_NOT_ZERO,
                Double.valueOf(f[0]));
    }
    final int n = f.length;
    if (n == 1) {       // trivial case
        transformed[0] = 0.0;
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.0;
    x[n >> 1] = 2.0 * f[n >> 1];
    for (int i = 1; i < (n >> 1); i++) {
        final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
        final double b = 0.5 * (f[i] - f[n - i]);
        x[i]     = a + b;
        x[n - i] = a - b;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FST result for the original array
    transformed[0] = 0.0;
    transformed[1] = 0.5 * y[0].getReal();
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = -y[i].getImaginary();
        transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1];
    }

    return transformed;
}
 
Example 10
Source File: FastCosineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FCT algorithm (including inverse).
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two plus one
 */
protected double[] fct(double[] f)
    throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(
            LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
            Integer.valueOf(f.length));
    }
    if (n == 1) {       // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    x[n >> 1] = f[n >> 1];
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);
        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);
        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);
        x[i] = a - b;
        x[n - i] = a + b;
        t1 += c;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();
    transformed[1] = t1;
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = y[i].getReal();
        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();
    }
    transformed[n] = y[n >> 1].getReal();

    return transformed;
}
 
Example 11
Source File: FastSineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FST algorithm (including inverse). The first element of the
 * data set is required to be {@code 0}.
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 *   not a power of two, or the first element of the data array is not zero
 */
protected double[] fst(double[] f) throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
                Integer.valueOf(f.length));
    }
    if (f[0] != 0.0) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.FIRST_ELEMENT_NOT_ZERO,
                Double.valueOf(f[0]));
    }
    final int n = f.length;
    if (n == 1) {       // trivial case
        transformed[0] = 0.0;
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.0;
    x[n >> 1] = 2.0 * f[n >> 1];
    for (int i = 1; i < (n >> 1); i++) {
        final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
        final double b = 0.5 * (f[i] - f[n - i]);
        x[i]     = a + b;
        x[n - i] = a - b;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FST result for the original array
    transformed[0] = 0.0;
    transformed[1] = 0.5 * y[0].getReal();
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = -y[i].getImaginary();
        transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1];
    }

    return transformed;
}
 
Example 12
Source File: FastCosineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FCT algorithm (including inverse).
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two plus one
 */
protected double[] fct(double[] f)
    throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(
            LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
            Integer.valueOf(f.length));
    }
    if (n == 1) {       // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    x[n >> 1] = f[n >> 1];
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);
        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);
        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);
        x[i] = a - b;
        x[n - i] = a + b;
        t1 += c;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();
    transformed[1] = t1;
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = y[i].getReal();
        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();
    }
    transformed[n] = y[n >> 1].getReal();

    return transformed;
}
 
Example 13
Source File: FastSineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FST algorithm (including inverse). The first element of the
 * data set is required to be {@code 0}.
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 *   not a power of two, or the first element of the data array is not zero
 */
protected double[] fst(double[] f) throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
                Integer.valueOf(f.length));
    }
    if (f[0] != 0.0) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.FIRST_ELEMENT_NOT_ZERO,
                Double.valueOf(f[0]));
    }
    final int n = f.length;
    if (n == 1) {       // trivial case
        transformed[0] = 0.0;
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.0;
    x[n >> 1] = 2.0 * f[n >> 1];
    for (int i = 1; i < (n >> 1); i++) {
        final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
        final double b = 0.5 * (f[i] - f[n - i]);
        x[i]     = a + b;
        x[n - i] = a - b;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FST result for the original array
    transformed[0] = 0.0;
    transformed[1] = 0.5 * y[0].getReal();
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = -y[i].getImaginary();
        transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1];
    }

    return transformed;
}
 
Example 14
Source File: FastCosineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FCT algorithm (including inverse).
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two plus one
 */
protected double[] fct(double[] f)
    throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(
            LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
            Integer.valueOf(f.length));
    }
    if (n == 1) {       // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    x[n >> 1] = f[n >> 1];
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);
        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);
        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);
        x[i] = a - b;
        x[n - i] = a + b;
        t1 += c;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();
    transformed[1] = t1;
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = y[i].getReal();
        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();
    }
    transformed[n] = y[n >> 1].getReal();

    return transformed;
}
 
Example 15
Source File: FastSineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FST algorithm (including inverse). The first element of the
 * data set is required to be {@code 0}.
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 *   not a power of two, or the first element of the data array is not zero
 */
protected double[] fst(double[] f) throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
                Integer.valueOf(f.length));
    }
    if (f[0] != 0.0) {
        throw new MathIllegalArgumentException(
                LocalizedFormats.FIRST_ELEMENT_NOT_ZERO,
                Double.valueOf(f[0]));
    }
    final int n = f.length;
    if (n == 1) {       // trivial case
        transformed[0] = 0.0;
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.0;
    x[n >> 1] = 2.0 * f[n >> 1];
    for (int i = 1; i < (n >> 1); i++) {
        final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
        final double b = 0.5 * (f[i] - f[n - i]);
        x[i]     = a + b;
        x[n - i] = a - b;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FST result for the original array
    transformed[0] = 0.0;
    transformed[1] = 0.5 * y[0].getReal();
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = -y[i].getImaginary();
        transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1];
    }

    return transformed;
}
 
Example 16
Source File: FastCosineTransformer.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Perform the FCT algorithm (including inverse).
 *
 * @param f the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException if the length of the data array is
 * not a power of two plus one
 */
protected double[] fct(double[] f)
    throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(
            LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
            Integer.valueOf(f.length));
    }
    if (n == 1) {       // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    x[n >> 1] = f[n >> 1];
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);
        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);
        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);
        x[i] = a - b;
        x[n - i] = a + b;
        t1 += c;
    }
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();
    transformed[1] = t1;
    for (int i = 1; i < (n >> 1); i++) {
        transformed[2 * i]     = y[i].getReal();
        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();
    }
    transformed[n] = y[n >> 1].getReal();

    return transformed;
}