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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.transform;

  22. import java.io.Serializable;

  23. import org.hipparchus.analysis.FunctionUtils;
  24. import org.hipparchus.analysis.UnivariateFunction;
  25. import org.hipparchus.complex.Complex;
  26. import org.hipparchus.exception.MathIllegalArgumentException;
  27. import org.hipparchus.util.ArithmeticUtils;
  28. import org.hipparchus.util.FastMath;
  29. import org.hipparchus.util.SinCos;

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

  66.     /** Serializable version identifier. */
  67.     static final long serialVersionUID = 20120212L;

  68.     /** The type of DCT to be performed. */
  69.     private final DctNormalization normalization;

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

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

  105.     /**
  106.      * {@inheritDoc}
  107.      *
  108.      * @throws org.hipparchus.exception.MathIllegalArgumentException
  109.      * if the lower bound is greater than, or equal to the upper bound
  110.      * @throws org.hipparchus.exception.MathIllegalArgumentException
  111.      * if the number of sample points is negative
  112.      * @throws MathIllegalArgumentException if the number of sample points is
  113.      * not a power of two plus one
  114.      */
  115.     @Override
  116.     public double[] transform(final UnivariateFunction f,
  117.         final double min, final double max, final int n,
  118.         final TransformType type) throws MathIllegalArgumentException {

  119.         final double[] data = FunctionUtils.sample(f, min, max, n);
  120.         return transform(data, type);
  121.     }

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

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

  133.         final int n = f.length - 1;
  134.         if (!ArithmeticUtils.isPowerOfTwo(n)) {
  135.             throw new MathIllegalArgumentException(LocalizedFFTFormats.NOT_POWER_OF_TWO_PLUS_ONE,
  136.                     f.length);
  137.         }
  138.         if (n == 1) {       // trivial case
  139.             transformed[0] = 0.5 * (f[0] + f[1]);
  140.             transformed[1] = 0.5 * (f[0] - f[1]);
  141.             return transformed;
  142.         }

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

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

  169.         return transformed;
  170.     }
  171. }