FastSineTransformer.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. /**
  30.  * Implements the Fast Sine Transform for transformation of one-dimensional real
  31.  * data sets. For reference, see James S. Walker, <em>Fast Fourier
  32.  * Transforms</em>, chapter 3 (ISBN 0849371635).
  33.  * <p>
  34.  * There are several variants of the discrete sine transform. The present
  35.  * implementation corresponds to DST-I, with various normalization conventions,
  36.  * which are specified by the parameter {@link DstNormalization}.
  37.  * <strong>It should be noted that regardless to the convention, the first
  38.  * element of the dataset to be transformed must be zero.</strong>
  39.  * <p>
  40.  * DST-I is equivalent to DFT of an <em>odd extension</em> of the data series.
  41.  * More precisely, if x<sub>0</sub>, &hellip;, x<sub>N-1</sub> is the data set
  42.  * to be sine transformed, the extended data set x<sub>0</sub><sup>&#35;</sup>,
  43.  * &hellip;, x<sub>2N-1</sub><sup>&#35;</sup> is defined as follows
  44.  * <ul>
  45.  * <li>x<sub>0</sub><sup>&#35;</sup> = x<sub>0</sub> = 0,</li>
  46.  * <li>x<sub>k</sub><sup>&#35;</sup> = x<sub>k</sub> if 1 &le; k &lt; N,</li>
  47.  * <li>x<sub>N</sub><sup>&#35;</sup> = 0,</li>
  48.  * <li>x<sub>k</sub><sup>&#35;</sup> = -x<sub>2N-k</sub> if N + 1 &le; k &lt;
  49.  * 2N.</li>
  50.  * </ul>
  51.  * <p>
  52.  * Then, the standard DST-I y<sub>0</sub>, &hellip;, y<sub>N-1</sub> of the real
  53.  * data set x<sub>0</sub>, &hellip;, x<sub>N-1</sub> is equal to <em>half</em>
  54.  * of i (the pure imaginary number) times the N first elements of the DFT of the
  55.  * extended data set x<sub>0</sub><sup>&#35;</sup>, &hellip;,
  56.  * x<sub>2N-1</sub><sup>&#35;</sup> <br>
  57.  * y<sub>n</sub> = (i / 2) &sum;<sub>k=0</sub><sup>2N-1</sup>
  58.  * x<sub>k</sub><sup>&#35;</sup> exp[-2&pi;i nk / (2N)]
  59.  * &nbsp;&nbsp;&nbsp;&nbsp;k = 0, &hellip;, N-1.
  60.  * <p>
  61.  * The present implementation of the discrete sine transform as a fast sine
  62.  * transform requires the length of the data to be a power of two. Besides,
  63.  * it implicitly assumes that the sampled function is odd. In particular, the
  64.  * first element of the data set must be 0, which is enforced in
  65.  * {@link #transform(UnivariateFunction, double, double, int, TransformType)},
  66.  * after sampling.
  67.  *
  68.  */
  69. public class FastSineTransformer implements RealTransformer, Serializable {

  70.     /** Serializable version identifier. */
  71.     static final long serialVersionUID = 20120211L;

  72.     /** The type of DST to be performed. */
  73.     private final DstNormalization normalization;

  74.     /**
  75.      * Creates a new instance of this class, with various normalization conventions.
  76.      *
  77.      * @param normalization the type of normalization to be applied to the transformed data
  78.      */
  79.     public FastSineTransformer(final DstNormalization normalization) {
  80.         this.normalization = normalization;
  81.     }

  82.     /**
  83.      * {@inheritDoc}
  84.      *
  85.      * The first element of the specified data set is required to be {@code 0}.
  86.      *
  87.      * @throws MathIllegalArgumentException if the length of the data array is
  88.      *   not a power of two, or the first element of the data array is not zero
  89.      */
  90.     @Override
  91.     public double[] transform(final double[] f, final TransformType type) {
  92.         if (normalization == DstNormalization.ORTHOGONAL_DST_I) {
  93.             final double s = FastMath.sqrt(2.0 / f.length);
  94.             return TransformUtils.scaleArray(fst(f), s);
  95.         }
  96.         if (type == TransformType.FORWARD) {
  97.             return fst(f);
  98.         }
  99.         final double s = 2.0 / f.length;
  100.         return TransformUtils.scaleArray(fst(f), s);
  101.     }

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

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

  121.     /**
  122.      * Perform the FST algorithm (including inverse). The first element of the
  123.      * data set is required to be {@code 0}.
  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, or the first element of the data array is not zero
  129.      */
  130.     protected double[] fst(double[] f) throws MathIllegalArgumentException {

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

  132.         if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
  133.             throw new MathIllegalArgumentException(
  134.                     LocalizedFFTFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
  135.                     f.length);
  136.         }
  137.         if (f[0] != 0.0) {
  138.             throw new MathIllegalArgumentException(
  139.                     LocalizedFFTFormats.FIRST_ELEMENT_NOT_ZERO,
  140.                     f[0]);
  141.         }
  142.         final int n = f.length;
  143.         if (n == 1) {       // trivial case
  144.             transformed[0] = 0.0;
  145.             return transformed;
  146.         }

  147.         // construct a new array and perform FFT on it
  148.         final double[] x = new double[n];
  149.         x[0] = 0.0;
  150.         x[n >> 1] = 2.0 * f[n >> 1];
  151.         for (int i = 1; i < (n >> 1); i++) {
  152.             final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
  153.             final double b = 0.5 * (f[i] - f[n - i]);
  154.             x[i]     = a + b;
  155.             x[n - i] = a - b;
  156.         }
  157.         FastFourierTransformer transformer;
  158.         transformer = new FastFourierTransformer(DftNormalization.STANDARD);
  159.         Complex[] y = transformer.transform(x, TransformType.FORWARD);

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

  167.         return transformed;
  168.     }
  169. }