001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.math3.transform; 018 019import java.io.Serializable; 020 021import org.apache.commons.math3.analysis.FunctionUtils; 022import org.apache.commons.math3.analysis.UnivariateFunction; 023import org.apache.commons.math3.complex.Complex; 024import org.apache.commons.math3.exception.MathIllegalArgumentException; 025import org.apache.commons.math3.exception.util.LocalizedFormats; 026import org.apache.commons.math3.util.ArithmeticUtils; 027import org.apache.commons.math3.util.FastMath; 028 029/** 030 * Implements the Fast Sine Transform for transformation of one-dimensional real 031 * data sets. For reference, see James S. Walker, <em>Fast Fourier 032 * Transforms</em>, chapter 3 (ISBN 0849371635). 033 * <p> 034 * There are several variants of the discrete sine transform. The present 035 * implementation corresponds to DST-I, with various normalization conventions, 036 * which are specified by the parameter {@link DstNormalization}. 037 * <strong>It should be noted that regardless to the convention, the first 038 * element of the dataset to be transformed must be zero.</strong> 039 * <p> 040 * DST-I is equivalent to DFT of an <em>odd extension</em> of the data series. 041 * More precisely, if x<sub>0</sub>, …, x<sub>N-1</sub> is the data set 042 * to be sine transformed, the extended data set x<sub>0</sub><sup>#</sup>, 043 * …, x<sub>2N-1</sub><sup>#</sup> is defined as follows 044 * <ul> 045 * <li>x<sub>0</sub><sup>#</sup> = x<sub>0</sub> = 0,</li> 046 * <li>x<sub>k</sub><sup>#</sup> = x<sub>k</sub> if 1 ≤ k < N,</li> 047 * <li>x<sub>N</sub><sup>#</sup> = 0,</li> 048 * <li>x<sub>k</sub><sup>#</sup> = -x<sub>2N-k</sub> if N + 1 ≤ k < 049 * 2N.</li> 050 * </ul> 051 * <p> 052 * Then, the standard DST-I y<sub>0</sub>, …, y<sub>N-1</sub> of the real 053 * data set x<sub>0</sub>, …, x<sub>N-1</sub> is equal to <em>half</em> 054 * of i (the pure imaginary number) times the N first elements of the DFT of the 055 * extended data set x<sub>0</sub><sup>#</sup>, …, 056 * x<sub>2N-1</sub><sup>#</sup> <br /> 057 * y<sub>n</sub> = (i / 2) ∑<sub>k=0</sub><sup>2N-1</sup> 058 * x<sub>k</sub><sup>#</sup> exp[-2πi nk / (2N)] 059 * k = 0, …, N-1. 060 * <p> 061 * The present implementation of the discrete sine transform as a fast sine 062 * transform requires the length of the data to be a power of two. Besides, 063 * it implicitly assumes that the sampled function is odd. In particular, the 064 * first element of the data set must be 0, which is enforced in 065 * {@link #transform(UnivariateFunction, double, double, int, TransformType)}, 066 * after sampling. 067 * 068 * @since 1.2 069 */ 070public class FastSineTransformer implements RealTransformer, Serializable { 071 072 /** Serializable version identifier. */ 073 static final long serialVersionUID = 20120211L; 074 075 /** The type of DST to be performed. */ 076 private final DstNormalization normalization; 077 078 /** 079 * Creates a new instance of this class, with various normalization conventions. 080 * 081 * @param normalization the type of normalization to be applied to the transformed data 082 */ 083 public FastSineTransformer(final DstNormalization normalization) { 084 this.normalization = normalization; 085 } 086 087 /** 088 * {@inheritDoc} 089 * 090 * The first element of the specified data set is required to be {@code 0}. 091 * 092 * @throws MathIllegalArgumentException if the length of the data array is 093 * not a power of two, or the first element of the data array is not zero 094 */ 095 public double[] transform(final double[] f, final TransformType type) { 096 if (normalization == DstNormalization.ORTHOGONAL_DST_I) { 097 final double s = FastMath.sqrt(2.0 / f.length); 098 return TransformUtils.scaleArray(fst(f), s); 099 } 100 if (type == TransformType.FORWARD) { 101 return fst(f); 102 } 103 final double s = 2.0 / f.length; 104 return TransformUtils.scaleArray(fst(f), s); 105 } 106 107 /** 108 * {@inheritDoc} 109 * 110 * This implementation enforces {@code f(x) = 0.0} at {@code x = 0.0}. 111 * 112 * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException 113 * if the lower bound is greater than, or equal to the upper bound 114 * @throws org.apache.commons.math3.exception.NotStrictlyPositiveException 115 * if the number of sample points is negative 116 * @throws MathIllegalArgumentException if the number of sample points is not a power of two 117 */ 118 public double[] transform(final UnivariateFunction f, 119 final double min, final double max, final int n, 120 final TransformType type) { 121 122 final double[] data = FunctionUtils.sample(f, min, max, n); 123 data[0] = 0.0; 124 return transform(data, type); 125 } 126 127 /** 128 * Perform the FST algorithm (including inverse). The first element of the 129 * data set is required to be {@code 0}. 130 * 131 * @param f the real data array to be transformed 132 * @return the real transformed array 133 * @throws MathIllegalArgumentException if the length of the data array is 134 * not a power of two, or the first element of the data array is not zero 135 */ 136 protected double[] fst(double[] f) throws MathIllegalArgumentException { 137 138 final double[] transformed = new double[f.length]; 139 140 if (!ArithmeticUtils.isPowerOfTwo(f.length)) { 141 throw new MathIllegalArgumentException( 142 LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING, 143 Integer.valueOf(f.length)); 144 } 145 if (f[0] != 0.0) { 146 throw new MathIllegalArgumentException( 147 LocalizedFormats.FIRST_ELEMENT_NOT_ZERO, 148 Double.valueOf(f[0])); 149 } 150 final int n = f.length; 151 if (n == 1) { // trivial case 152 transformed[0] = 0.0; 153 return transformed; 154 } 155 156 // construct a new array and perform FFT on it 157 final double[] x = new double[n]; 158 x[0] = 0.0; 159 x[n >> 1] = 2.0 * f[n >> 1]; 160 for (int i = 1; i < (n >> 1); i++) { 161 final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]); 162 final double b = 0.5 * (f[i] - f[n - i]); 163 x[i] = a + b; 164 x[n - i] = a - b; 165 } 166 FastFourierTransformer transformer; 167 transformer = new FastFourierTransformer(DftNormalization.STANDARD); 168 Complex[] y = transformer.transform(x, TransformType.FORWARD); 169 170 // reconstruct the FST result for the original array 171 transformed[0] = 0.0; 172 transformed[1] = 0.5 * y[0].getReal(); 173 for (int i = 1; i < (n >> 1); i++) { 174 transformed[2 * i] = -y[i].getImaginary(); 175 transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1]; 176 } 177 178 return transformed; 179 } 180}