|
Commons Math example source code file (FastHadamardTransformer.java)
The Commons Math FastHadamardTransformer.java source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.math.transform; import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.analysis.UnivariateRealFunction; /** * Implements the <a href="http://www.archive.chipcenter.com/dsp/DSP000517F1.html">Fast Hadamard Transform (FHT). * Transformation of an input vector x to the output vector y. * <p>In addition to transformation of real vectors, the Hadamard transform can * transform integer vectors into integer vectors. However, this integer transform * cannot be inverted directly. Due to a scaling factor it may lead to rational results. * As an example, the inverse transform of integer vector (0, 1, 0, 1) is rational * vector (1/2, -1/2, 0, 0).</p> * @version $Revision: 811685 $ $Date: 2009-09-05 13:36:48 -0400 (Sat, 05 Sep 2009) $ * @since 2.0 */ public class FastHadamardTransformer implements RealTransformer { /** {@inheritDoc} */ public double[] transform(double f[]) throws IllegalArgumentException { return fht(f); } /** {@inheritDoc} */ public double[] transform(UnivariateRealFunction f, double min, double max, int n) throws FunctionEvaluationException, IllegalArgumentException { return fht(FastFourierTransformer.sample(f, min, max, n)); } /** {@inheritDoc} */ public double[] inversetransform(double f[]) throws IllegalArgumentException { return FastFourierTransformer.scaleArray(fht(f), 1.0 / f.length); } /** {@inheritDoc} */ public double[] inversetransform(UnivariateRealFunction f, double min, double max, int n) throws FunctionEvaluationException, IllegalArgumentException { final double[] unscaled = fht(FastFourierTransformer.sample(f, min, max, n)); return FastFourierTransformer.scaleArray(unscaled, 1.0 / n); } /** * Transform the given real data set. * <p>The integer transform cannot be inverted directly, due to a scaling * factor it may lead to double results.</p> * @param f the integer data array to be transformed (signal) * @return the integer transformed array (spectrum) * @throws IllegalArgumentException if any parameters are invalid */ public int[] transform(int f[]) throws IllegalArgumentException { return fht(f); } /** * The FHT (Fast Hadamard Transformation) which uses only subtraction and addition. * <br> * Requires <b>Nlog2N = n2n additions. * <br> * <br> * <b>Short Table of manual calculation for N=8: * <ol> * <li>x is the input vector we want to transform * <li>y is the output vector which is our desired result * <li>a and b are just helper rows * </ol> * <pre> * <code> * +----+----------+---------+----------+ * | <b>x | a | b | y | * +----+----------+---------+----------+ * | x<sub>0 | a0=x0+x1 | b0=a0+a1 | y0=b0+b1 | * +----+----------+---------+----------+ * | x<sub>1 | a1=x2+x3 | b0=a2+a3 | y0=b2+b3 | * +----+----------+---------+----------+ * | x<sub>2 | a2=x4+x5 | b0=a4+a5 | y0=b4+b5 | * +----+----------+---------+----------+ * | x<sub>3 | a3=x6+x7 | b0=a6+a7 | y0=b6+b7 | * +----+----------+---------+----------+ * | x<sub>4 | a0=x0-x1 | b0=a0-a1 | y0=b0-b1 | * +----+----------+---------+----------+ * | x<sub>5 | a1=x2-x3 | b0=a2-a3 | y0=b2-b3 | * +----+----------+---------+----------+ * | x<sub>6 | a2=x4-x5 | b0=a4-a5 | y0=b4-b5 | * +----+----------+---------+----------+ * | x<sub>7 | a3=x6-x7 | b0=a6-a7 | y0=b6-b7 | * +----+----------+---------+----------+ * </code> * </pre> * * <b>How it works * <ol> * <li>Construct a matrix with N rows and n+1 columns Other Commons Math examples (source code examples)Here is a short list of links related to this Commons Math FastHadamardTransformer.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.