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 Cosine Transform for transformation of one-dimensional 031 * real 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 cosine transform. The present 035 * implementation corresponds to DCT-I, with various normalization conventions, 036 * which are specified by the parameter {@link DctNormalization}. 037 * <p> 038 * DCT-I is equivalent to DFT of an <em>even extension</em> of the data series. 039 * More precisely, if x<sub>0</sub>, …, x<sub>N-1</sub> is the data set 040 * to be cosine transformed, the extended data set 041 * x<sub>0</sub><sup>#</sup>, …, x<sub>2N-3</sub><sup>#</sup> 042 * is defined as follows 043 * <ul> 044 * <li>x<sub>k</sub><sup>#</sup> = x<sub>k</sub> if 0 ≤ k < N,</li> 045 * <li>x<sub>k</sub><sup>#</sup> = x<sub>2N-2-k</sub> 046 * if N ≤ k < 2N - 2.</li> 047 * </ul> 048 * <p> 049 * Then, the standard DCT-I y<sub>0</sub>, …, y<sub>N-1</sub> of the real 050 * data set x<sub>0</sub>, …, x<sub>N-1</sub> is equal to <em>half</em> 051 * of the N first elements of the DFT of the extended data set 052 * x<sub>0</sub><sup>#</sup>, …, x<sub>2N-3</sub><sup>#</sup> 053 * <br/> 054 * y<sub>n</sub> = (1 / 2) ∑<sub>k=0</sub><sup>2N-3</sup> 055 * x<sub>k</sub><sup>#</sup> exp[-2πi nk / (2N - 2)] 056 * k = 0, …, N-1. 057 * <p> 058 * The present implementation of the discrete cosine transform as a fast cosine 059 * transform requires the length of the data set to be a power of two plus one 060 * (N = 2<sup>n</sup> + 1). Besides, it implicitly assumes 061 * that the sampled function is even. 062 * 063 * @since 1.2 064 */ 065public class FastCosineTransformer implements RealTransformer, Serializable { 066 067 /** Serializable version identifier. */ 068 static final long serialVersionUID = 20120212L; 069 070 /** The type of DCT to be performed. */ 071 private final DctNormalization normalization; 072 073 /** 074 * Creates a new instance of this class, with various normalization 075 * conventions. 076 * 077 * @param normalization the type of normalization to be applied to the 078 * transformed data 079 */ 080 public FastCosineTransformer(final DctNormalization normalization) { 081 this.normalization = normalization; 082 } 083 084 /** 085 * {@inheritDoc} 086 * 087 * @throws MathIllegalArgumentException if the length of the data array is 088 * not a power of two plus one 089 */ 090 public double[] transform(final double[] f, final TransformType type) 091 throws MathIllegalArgumentException { 092 if (type == TransformType.FORWARD) { 093 if (normalization == DctNormalization.ORTHOGONAL_DCT_I) { 094 final double s = FastMath.sqrt(2.0 / (f.length - 1)); 095 return TransformUtils.scaleArray(fct(f), s); 096 } 097 return fct(f); 098 } 099 final double s2 = 2.0 / (f.length - 1); 100 final double s1; 101 if (normalization == DctNormalization.ORTHOGONAL_DCT_I) { 102 s1 = FastMath.sqrt(s2); 103 } else { 104 s1 = s2; 105 } 106 return TransformUtils.scaleArray(fct(f), s1); 107 } 108 109 /** 110 * {@inheritDoc} 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 117 * not a power of two plus one 118 */ 119 public double[] transform(final UnivariateFunction f, 120 final double min, final double max, final int n, 121 final TransformType type) throws MathIllegalArgumentException { 122 123 final double[] data = FunctionUtils.sample(f, min, max, n); 124 return transform(data, type); 125 } 126 127 /** 128 * Perform the FCT algorithm (including inverse). 129 * 130 * @param f the real data array to be transformed 131 * @return the real transformed array 132 * @throws MathIllegalArgumentException if the length of the data array is 133 * not a power of two plus one 134 */ 135 protected double[] fct(double[] f) 136 throws MathIllegalArgumentException { 137 138 final double[] transformed = new double[f.length]; 139 140 final int n = f.length - 1; 141 if (!ArithmeticUtils.isPowerOfTwo(n)) { 142 throw new MathIllegalArgumentException( 143 LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE, 144 Integer.valueOf(f.length)); 145 } 146 if (n == 1) { // trivial case 147 transformed[0] = 0.5 * (f[0] + f[1]); 148 transformed[1] = 0.5 * (f[0] - f[1]); 149 return transformed; 150 } 151 152 // construct a new array and perform FFT on it 153 final double[] x = new double[n]; 154 x[0] = 0.5 * (f[0] + f[n]); 155 x[n >> 1] = f[n >> 1]; 156 // temporary variable for transformed[1] 157 double t1 = 0.5 * (f[0] - f[n]); 158 for (int i = 1; i < (n >> 1); i++) { 159 final double a = 0.5 * (f[i] + f[n - i]); 160 final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]); 161 final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]); 162 x[i] = a - b; 163 x[n - i] = a + b; 164 t1 += c; 165 } 166 FastFourierTransformer transformer; 167 transformer = new FastFourierTransformer(DftNormalization.STANDARD); 168 Complex[] y = transformer.transform(x, TransformType.FORWARD); 169 170 // reconstruct the FCT result for the original array 171 transformed[0] = y[0].getReal(); 172 transformed[1] = t1; 173 for (int i = 1; i < (n >> 1); i++) { 174 transformed[2 * i] = y[i].getReal(); 175 transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary(); 176 } 177 transformed[n] = y[n >> 1].getReal(); 178 179 return transformed; 180 } 181}