001/* ***** BEGIN LICENSE BLOCK ***** 002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 003 * 004 * The contents of this file are subject to the Mozilla Public License Version 005 * 1.1 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * http://www.mozilla.org/MPL/ 008 * 009 * Software distributed under the License is distributed on an "AS IS" basis, 010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 011 * for the specific language governing rights and limitations under the 012 * License. 013 * 014 * The Original Code is JTransforms. 015 * 016 * The Initial Developer of the Original Code is 017 * Piotr Wendykier, Emory University. 018 * Portions created by the Initial Developer are Copyright (C) 2007-2009 019 * the Initial Developer. All Rights Reserved. 020 * 021 * Alternatively, the contents of this file may be used under the terms of 022 * either the GNU General Public License Version 2 or later (the "GPL"), or 023 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 024 * in which case the provisions of the GPL or the LGPL are applicable instead 025 * of those above. If you wish to allow use of your version of this file only 026 * under the terms of either the GPL or the LGPL, and not to allow others to 027 * use your version of this file under the terms of the MPL, indicate your 028 * decision by deleting the provisions above and replace them with the notice 029 * and other provisions required by the GPL or the LGPL. If you do not delete 030 * the provisions above, a recipient may use your version of this file under 031 * the terms of any one of the MPL, the GPL or the LGPL. 032 * 033 * ***** END LICENSE BLOCK ***** */ 034 035package edu.emory.mathcs.jtransforms.dht; 036 037import java.util.concurrent.Future; 038 039import edu.emory.mathcs.jtransforms.fft.DoubleFFT_1D; 040import edu.emory.mathcs.utils.ConcurrencyUtils; 041 042/** 043 * Computes 1D Discrete Hartley Transform (DHT) of real, double precision data. 044 * The size of the data can be an arbitrary number. It uses FFT algorithm. This 045 * is a parallel implementation optimized for SMP systems. 046 * 047 * @author Piotr Wendykier (piotr.wendykier@gmail.com) 048 */ 049public class DoubleDHT_1D { 050 private int n; 051 private DoubleFFT_1D fft; 052 053 /** 054 * Creates new instance of DoubleDHT_1D. 055 * 056 * @param n 057 * size of data 058 */ 059 public DoubleDHT_1D(int n) { 060 this.n = n; 061 fft = new DoubleFFT_1D(n); 062 } 063 064 /** 065 * Computes 1D real, forward DHT leaving the result in <code>a</code>. 066 * 067 * @param a 068 * data to transform 069 */ 070 public void forward(double[] a) { 071 forward(a, 0); 072 } 073 074 /** 075 * Computes 1D real, forward DHT leaving the result in <code>a</code>. 076 * 077 * @param a 078 * data to transform 079 * @param offa 080 * index of the first element in array <code>a</code> 081 */ 082 public void forward(final double[] a, final int offa) { 083 if (n == 1) 084 return; 085 fft.realForward(a, offa); 086 final double[] b = new double[n]; 087 System.arraycopy(a, offa, b, 0, n); 088 int nd2 = n / 2; 089 int nthreads = ConcurrencyUtils.getNumberOfThreads(); 090 if ((nthreads > 1) && (nd2 > ConcurrencyUtils.getThreadsBeginN_1D_FFT_2Threads())) { 091 nthreads = 2; 092 final int k1 = nd2 / nthreads; 093 Future<?>[] futures = new Future[nthreads]; 094 for (int i = 0; i < nthreads; i++) { 095 final int firstIdx = 1 + i * k1; 096 final int lastIdx = (i == (nthreads - 1)) ? nd2 : firstIdx + k1; 097 futures[i] = ConcurrencyUtils.submit(new Runnable() { 098 099 public void run() { 100 int idx1, idx2; 101 for (int i = firstIdx; i < lastIdx; i++) { 102 idx1 = 2 * i; 103 idx2 = idx1 + 1; 104 a[offa + i] = b[idx1] - b[idx2]; 105 a[offa + n - i] = b[idx1] + b[idx2]; 106 } 107 } 108 109 }); 110 } 111 ConcurrencyUtils.waitForCompletion(futures); 112 } else { 113 int idx1, idx2; 114 for (int i = 1; i < nd2; i++) { 115 idx1 = 2 * i; 116 idx2 = idx1 + 1; 117 a[offa + i] = b[idx1] - b[idx2]; 118 a[offa + n - i] = b[idx1] + b[idx2]; 119 } 120 } 121 if ((n % 2) == 0) { 122 a[offa + nd2] = b[1]; 123 } else { 124 a[offa + nd2] = b[n - 1] - b[1]; 125 a[offa + nd2 + 1] = b[n - 1] + b[1]; 126 } 127 128 } 129 130 /** 131 * Computes 1D real, inverse DHT leaving the result in <code>a</code>. 132 * 133 * @param a 134 * data to transform 135 * @param scale 136 * if true then scaling is performed 137 */ 138 public void inverse(double[] a, boolean scale) { 139 inverse(a, 0, scale); 140 } 141 142 /** 143 * Computes 1D real, inverse DHT leaving the result in <code>a</code>. 144 * 145 * @param a 146 * data to transform 147 * @param offa 148 * index of the first element in array <code>a</code> 149 * @param scale 150 * if true then scaling is performed 151 */ 152 public void inverse(final double[] a, final int offa, boolean scale) { 153 if (n == 1) 154 return; 155 forward(a, offa); 156 if (scale) { 157 scale(n, a, offa); 158 } 159 } 160 161 private void scale(final double m, final double[] a, int offa) { 162 final double norm = (1.0 / m); 163 int nthreads = ConcurrencyUtils.getNumberOfThreads(); 164 if ((nthreads > 1) && (n >= ConcurrencyUtils.getThreadsBeginN_1D_FFT_2Threads())) { 165 nthreads = 2; 166 final int k = n / nthreads; 167 Future<?>[] futures = new Future[nthreads]; 168 for (int i = 0; i < nthreads; i++) { 169 final int firstIdx = offa + i * k; 170 final int lastIdx = (i == (nthreads - 1)) ? offa + n : firstIdx + k; 171 futures[i] = ConcurrencyUtils.submit(new Runnable() { 172 173 public void run() { 174 for (int i = firstIdx; i < lastIdx; i++) { 175 a[i] *= norm; 176 } 177 } 178 }); 179 } 180 ConcurrencyUtils.waitForCompletion(futures); 181 } else { 182 int lastIdx = offa + n; 183 for (int i = offa; i < lastIdx; i++) { 184 a[i] *= norm; 185 } 186 187 } 188 } 189}