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

  68.     /** Serializable version identifier. */
  69.     static final long serialVersionUID = 20120211L;

  70.     /** The type of DST to be performed. */
  71.     private final DstNormalization normalization;

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

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

  99.     /**
  100.      * {@inheritDoc}
  101.      *
  102.      * This implementation enforces {@code f(x) = 0.0} at {@code x = 0.0}.
  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 not a power of two
  109.      */
  110.     public double[] transform(final UnivariateFunction f,
  111.         final double min, final double max, final int n,
  112.         final TransformType type) {

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

  117.     /**
  118.      * Perform the FST algorithm (including inverse). The first element of the
  119.      * data set is required to be {@code 0}.
  120.      *
  121.      * @param f the real data array to be transformed
  122.      * @return the real transformed array
  123.      * @throws MathIllegalArgumentException if the length of the data array is
  124.      *   not a power of two, or the first element of the data array is not zero
  125.      */
  126.     protected double[] fst(double[] f) throws MathIllegalArgumentException {

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

  128.         if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
  129.             throw new MathIllegalArgumentException(
  130.                     LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
  131.                     Integer.valueOf(f.length));
  132.         }
  133.         if (f[0] != 0.0) {
  134.             throw new MathIllegalArgumentException(
  135.                     LocalizedFormats.FIRST_ELEMENT_NOT_ZERO,
  136.                     Double.valueOf(f[0]));
  137.         }
  138.         final int n = f.length;
  139.         if (n == 1) {       // trivial case
  140.             transformed[0] = 0.0;
  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.0;
  146.         x[n >> 1] = 2.0 * f[n >> 1];
  147.         for (int i = 1; i < (n >> 1); i++) {
  148.             final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
  149.             final double b = 0.5 * (f[i] - f[n - i]);
  150.             x[i]     = a + b;
  151.             x[n - i] = a - b;
  152.         }
  153.         FastFourierTransformer transformer;
  154.         transformer = new FastFourierTransformer(DftNormalization.STANDARD);
  155.         Complex[] y = transformer.transform(x, TransformType.FORWARD);

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

  163.         return transformed;
  164.     }
  165. }