FastCosineTransformer.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.math3.transform;

  18. import java.io.Serializable;

  19. import org.apache.commons.math3.analysis.FunctionUtils;
  20. import org.apache.commons.math3.analysis.UnivariateFunction;
  21. import org.apache.commons.math3.complex.Complex;
  22. import org.apache.commons.math3.exception.MathIllegalArgumentException;
  23. import org.apache.commons.math3.exception.util.LocalizedFormats;
  24. import org.apache.commons.math3.util.ArithmeticUtils;
  25. import org.apache.commons.math3.util.FastMath;

  26. /**
  27.  * Implements the Fast Cosine Transform for transformation of one-dimensional
  28.  * real data sets. For reference, see James S. Walker, <em>Fast Fourier
  29.  * Transforms</em>, chapter 3 (ISBN 0849371635).
  30.  * <p>
  31.  * There are several variants of the discrete cosine transform. The present
  32.  * implementation corresponds to DCT-I, with various normalization conventions,
  33.  * which are specified by the parameter {@link DctNormalization}.
  34.  * <p>
  35.  * DCT-I is equivalent to DFT of an <em>even extension</em> of the data series.
  36.  * More precisely, if x<sub>0</sub>, &hellip;, x<sub>N-1</sub> is the data set
  37.  * to be cosine transformed, the extended data set
  38.  * x<sub>0</sub><sup>&#35;</sup>, &hellip;, x<sub>2N-3</sub><sup>&#35;</sup>
  39.  * is defined as follows
  40.  * <ul>
  41.  * <li>x<sub>k</sub><sup>&#35;</sup> = x<sub>k</sub> if 0 &le; k &lt; N,</li>
  42.  * <li>x<sub>k</sub><sup>&#35;</sup> = x<sub>2N-2-k</sub>
  43.  * if N &le; k &lt; 2N - 2.</li>
  44.  * </ul>
  45.  * <p>
  46.  * Then, the standard DCT-I y<sub>0</sub>, &hellip;, y<sub>N-1</sub> of the real
  47.  * data set x<sub>0</sub>, &hellip;, x<sub>N-1</sub> is equal to <em>half</em>
  48.  * of the N first elements of the DFT of the extended data set
  49.  * x<sub>0</sub><sup>&#35;</sup>, &hellip;, x<sub>2N-3</sub><sup>&#35;</sup>
  50.  * <br/>
  51.  * y<sub>n</sub> = (1 / 2) &sum;<sub>k=0</sub><sup>2N-3</sup>
  52.  * x<sub>k</sub><sup>&#35;</sup> exp[-2&pi;i nk / (2N - 2)]
  53.  * &nbsp;&nbsp;&nbsp;&nbsp;k = 0, &hellip;, N-1.
  54.  * <p>
  55.  * The present implementation of the discrete cosine transform as a fast cosine
  56.  * transform requires the length of the data set to be a power of two plus one
  57.  * (N&nbsp;=&nbsp;2<sup>n</sup>&nbsp;+&nbsp;1). Besides, it implicitly assumes
  58.  * that the sampled function is even.
  59.  *
  60.  * @since 1.2
  61.  */
  62. public class FastCosineTransformer implements RealTransformer, Serializable {

  63.     /** Serializable version identifier. */
  64.     static final long serialVersionUID = 20120212L;

  65.     /** The type of DCT to be performed. */
  66.     private final DctNormalization normalization;

  67.     /**
  68.      * Creates a new instance of this class, with various normalization
  69.      * conventions.
  70.      *
  71.      * @param normalization the type of normalization to be applied to the
  72.      * transformed data
  73.      */
  74.     public FastCosineTransformer(final DctNormalization normalization) {
  75.         this.normalization = normalization;
  76.     }

  77.     /**
  78.      * {@inheritDoc}
  79.      *
  80.      * @throws MathIllegalArgumentException if the length of the data array is
  81.      * not a power of two plus one
  82.      */
  83.     public double[] transform(final double[] f, final TransformType type)
  84.       throws MathIllegalArgumentException {
  85.         if (type == TransformType.FORWARD) {
  86.             if (normalization == DctNormalization.ORTHOGONAL_DCT_I) {
  87.                 final double s = FastMath.sqrt(2.0 / (f.length - 1));
  88.                 return TransformUtils.scaleArray(fct(f), s);
  89.             }
  90.             return fct(f);
  91.         }
  92.         final double s2 = 2.0 / (f.length - 1);
  93.         final double s1;
  94.         if (normalization == DctNormalization.ORTHOGONAL_DCT_I) {
  95.             s1 = FastMath.sqrt(s2);
  96.         } else {
  97.             s1 = s2;
  98.         }
  99.         return TransformUtils.scaleArray(fct(f), s1);
  100.     }

  101.     /**
  102.      * {@inheritDoc}
  103.      *
  104.      * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException
  105.      * if the lower bound is greater than, or equal to the upper bound
  106.      * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException
  107.      * if the number of sample points is negative
  108.      * @throws MathIllegalArgumentException if the number of sample points is
  109.      * not a power of two plus one
  110.      */
  111.     public double[] transform(final UnivariateFunction f,
  112.         final double min, final double max, final int n,
  113.         final TransformType type) throws MathIllegalArgumentException {

  114.         final double[] data = FunctionUtils.sample(f, min, max, n);
  115.         return transform(data, type);
  116.     }

  117.     /**
  118.      * Perform the FCT algorithm (including inverse).
  119.      *
  120.      * @param f the real data array to be transformed
  121.      * @return the real transformed array
  122.      * @throws MathIllegalArgumentException if the length of the data array is
  123.      * not a power of two plus one
  124.      */
  125.     protected double[] fct(double[] f)
  126.         throws MathIllegalArgumentException {

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

  128.         final int n = f.length - 1;
  129.         if (!ArithmeticUtils.isPowerOfTwo(n)) {
  130.             throw new MathIllegalArgumentException(
  131.                 LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
  132.                 Integer.valueOf(f.length));
  133.         }
  134.         if (n == 1) {       // trivial case
  135.             transformed[0] = 0.5 * (f[0] + f[1]);
  136.             transformed[1] = 0.5 * (f[0] - f[1]);
  137.             return transformed;
  138.         }

  139.         // construct a new array and perform FFT on it
  140.         final double[] x = new double[n];
  141.         x[0] = 0.5 * (f[0] + f[n]);
  142.         x[n >> 1] = f[n >> 1];
  143.         // temporary variable for transformed[1]
  144.         double t1 = 0.5 * (f[0] - f[n]);
  145.         for (int i = 1; i < (n >> 1); i++) {
  146.             final double a = 0.5 * (f[i] + f[n - i]);
  147.             final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);
  148.             final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);
  149.             x[i] = a - b;
  150.             x[n - i] = a + b;
  151.             t1 += c;
  152.         }
  153.         FastFourierTransformer transformer;
  154.         transformer = new FastFourierTransformer(DftNormalization.STANDARD);
  155.         Complex[] y = transformer.transform(x, TransformType.FORWARD);

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

  164.         return transformed;
  165.     }
  166. }