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 /* 19 * This is not the original file distributed by the Apache Software Foundation 20 * It has been modified by the Hipparchus project 21 */ 22 package org.hipparchus.transform; 23 24 import java.io.Serializable; 25 26 import org.hipparchus.analysis.FunctionUtils; 27 import org.hipparchus.analysis.UnivariateFunction; 28 import org.hipparchus.complex.Complex; 29 import org.hipparchus.exception.MathIllegalArgumentException; 30 import org.hipparchus.util.ArithmeticUtils; 31 import org.hipparchus.util.FastMath; 32 33 /** 34 * Implements the Fast Sine Transform for transformation of one-dimensional real 35 * data sets. For reference, see James S. Walker, <em>Fast Fourier 36 * Transforms</em>, chapter 3 (ISBN 0849371635). 37 * <p> 38 * There are several variants of the discrete sine transform. The present 39 * implementation corresponds to DST-I, with various normalization conventions, 40 * which are specified by the parameter {@link DstNormalization}. 41 * <strong>It should be noted that regardless to the convention, the first 42 * element of the dataset to be transformed must be zero.</strong> 43 * <p> 44 * DST-I is equivalent to DFT of an <em>odd extension</em> of the data series. 45 * More precisely, if x<sub>0</sub>, …, x<sub>N-1</sub> is the data set 46 * to be sine transformed, the extended data set x<sub>0</sub><sup>#</sup>, 47 * …, x<sub>2N-1</sub><sup>#</sup> is defined as follows 48 * <ul> 49 * <li>x<sub>0</sub><sup>#</sup> = x<sub>0</sub> = 0,</li> 50 * <li>x<sub>k</sub><sup>#</sup> = x<sub>k</sub> if 1 ≤ k < N,</li> 51 * <li>x<sub>N</sub><sup>#</sup> = 0,</li> 52 * <li>x<sub>k</sub><sup>#</sup> = -x<sub>2N-k</sub> if N + 1 ≤ k < 53 * 2N.</li> 54 * </ul> 55 * <p> 56 * Then, the standard DST-I y<sub>0</sub>, …, y<sub>N-1</sub> of the real 57 * data set x<sub>0</sub>, …, x<sub>N-1</sub> is equal to <em>half</em> 58 * of i (the pure imaginary number) times the N first elements of the DFT of the 59 * extended data set x<sub>0</sub><sup>#</sup>, …, 60 * x<sub>2N-1</sub><sup>#</sup> <br> 61 * y<sub>n</sub> = (i / 2) ∑<sub>k=0</sub><sup>2N-1</sup> 62 * x<sub>k</sub><sup>#</sup> exp[-2πi nk / (2N)] 63 * k = 0, …, N-1. 64 * <p> 65 * The present implementation of the discrete sine transform as a fast sine 66 * transform requires the length of the data to be a power of two. Besides, 67 * it implicitly assumes that the sampled function is odd. In particular, the 68 * first element of the data set must be 0, which is enforced in 69 * {@link #transform(UnivariateFunction, double, double, int, TransformType)}, 70 * after sampling. 71 * 72 */ 73 public class FastSineTransformer implements RealTransformer, Serializable { 74 75 /** Serializable version identifier. */ 76 static final long serialVersionUID = 20120211L; 77 78 /** The type of DST to be performed. */ 79 private final DstNormalization normalization; 80 81 /** 82 * Creates a new instance of this class, with various normalization conventions. 83 * 84 * @param normalization the type of normalization to be applied to the transformed data 85 */ 86 public FastSineTransformer(final DstNormalization normalization) { 87 this.normalization = normalization; 88 } 89 90 /** 91 * {@inheritDoc} 92 * 93 * The first element of the specified data set is required to be {@code 0}. 94 * 95 * @throws MathIllegalArgumentException if the length of the data array is 96 * not a power of two, or the first element of the data array is not zero 97 */ 98 @Override 99 public double[] transform(final double[] f, final TransformType type) { 100 if (normalization == DstNormalization.ORTHOGONAL_DST_I) { 101 final double s = FastMath.sqrt(2.0 / f.length); 102 return TransformUtils.scaleArray(fst(f), s); 103 } 104 if (type == TransformType.FORWARD) { 105 return fst(f); 106 } 107 final double s = 2.0 / f.length; 108 return TransformUtils.scaleArray(fst(f), s); 109 } 110 111 /** 112 * {@inheritDoc} 113 * 114 * This implementation enforces {@code f(x) = 0.0} at {@code x = 0.0}. 115 * 116 * @throws org.hipparchus.exception.MathIllegalArgumentException 117 * if the lower bound is greater than, or equal to the upper bound 118 * @throws org.hipparchus.exception.MathIllegalArgumentException 119 * if the number of sample points is negative 120 * @throws MathIllegalArgumentException if the number of sample points is not a power of two 121 */ 122 @Override 123 public double[] transform(final UnivariateFunction f, 124 final double min, final double max, final int n, 125 final TransformType type) { 126 127 final double[] data = FunctionUtils.sample(f, min, max, n); 128 data[0] = 0.0; 129 return transform(data, type); 130 } 131 132 /** 133 * Perform the FST algorithm (including inverse). The first element of the 134 * data set is required to be {@code 0}. 135 * 136 * @param f the real data array to be transformed 137 * @return the real transformed array 138 * @throws MathIllegalArgumentException if the length of the data array is 139 * not a power of two, or the first element of the data array is not zero 140 */ 141 protected double[] fst(double[] f) throws MathIllegalArgumentException { 142 143 final double[] transformed = new double[f.length]; 144 145 if (!ArithmeticUtils.isPowerOfTwo(f.length)) { 146 throw new MathIllegalArgumentException( 147 LocalizedFFTFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, 148 Integer.valueOf(f.length)); 149 } 150 if (f[0] != 0.0) { 151 throw new MathIllegalArgumentException( 152 LocalizedFFTFormats.FIRST_ELEMENT_NOT_ZERO, 153 Double.valueOf(f[0])); 154 } 155 final int n = f.length; 156 if (n == 1) { // trivial case 157 transformed[0] = 0.0; 158 return transformed; 159 } 160 161 // construct a new array and perform FFT on it 162 final double[] x = new double[n]; 163 x[0] = 0.0; 164 x[n >> 1] = 2.0 * f[n >> 1]; 165 for (int i = 1; i < (n >> 1); i++) { 166 final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]); 167 final double b = 0.5 * (f[i] - f[n - i]); 168 x[i] = a + b; 169 x[n - i] = a - b; 170 } 171 FastFourierTransformer transformer; 172 transformer = new FastFourierTransformer(DftNormalization.STANDARD); 173 Complex[] y = transformer.transform(x, TransformType.FORWARD); 174 175 // reconstruct the FST result for the original array 176 transformed[0] = 0.0; 177 transformed[1] = 0.5 * y[0].getReal(); 178 for (int i = 1; i < (n >> 1); i++) { 179 transformed[2 * i] = -y[i].getImaginary(); 180 transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1]; 181 } 182 183 return transformed; 184 } 185 }