5215a3a25928ad645869be3c2eeadba9e9b2d875
[ctsim.git] / include / fourier.h
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          fourier.h
5 **   Purpose:       Header for Fourier transform functions
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  Dec 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (c) 1983-2001 Kevin Rosenberg
11 **
12 **  $Id$
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #include <complex>
29 #ifdef HAVE_FFTW
30 #include <fftw3.h>
31 #endif
32
33 class ImageFile;
34
35 class Fourier {
36 public:
37     static void shuffleFourierToNaturalOrder (ImageFile& im);
38     static void shuffleNaturalToFourierOrder (ImageFile& im);
39
40 #ifdef HAVE_FFTW
41     static void shuffleFourierToNaturalOrder (fftw_complex* pc, const int n);
42     static void shuffleNaturalToFourierOrder (fftw_complex* pc, const int n);
43 #endif
44     
45 // Odd Number of Points
46 //   Natural Frequency Order: -(n-1)/2...-1,0,1...(n-1)/2
47 //   Fourier Frequency Order: 0, 1..(n-1)/2,-(n-1)/2...-1
48 // Even Number of Points
49 //   Natural Frequency Order: -n/2...-1,0,1...((n/2)-1)
50 //   Fourier Frequency Order: 0,1...((n/2)-1),-n/2...-1
51     template<class T>
52     static void shuffleNaturalToFourierOrder (T* pVector, const int n) 
53     {
54       T* pTemp = new T [n];
55       int i;
56       if (isOdd(n)) { // Odd
57         int iHalfN = (n - 1) / 2;
58
59         pTemp[0] = pVector[iHalfN];
60         for (i = 0; i < iHalfN; i++)
61           pTemp[i + 1] = pVector[i + 1 + iHalfN];
62         for (i = 0; i < iHalfN; i++)
63           pTemp[i + iHalfN + 1] = pVector[i];
64       } else {     // Even
65         int iHalfN = n / 2;
66         pTemp[0] = pVector[iHalfN];
67         for (i = 0; i < iHalfN - 1; i++)
68         pTemp[i + 1] = pVector[i + iHalfN + 1];
69         for (i = 0; i < iHalfN; i++)
70           pTemp[i + iHalfN] = pVector[i];
71       }
72
73     for (i = 0; i < n; i++)
74       pVector[i] = pTemp[i];
75     delete pTemp;
76   }
77
78   template<class T>
79   static void shuffleFourierToNaturalOrder (T* pVector, const int n)
80   {
81     T* pTemp = new T [n];
82     int i;
83     if (isOdd(n)) { // Odd
84       int iHalfN = (n - 1) / 2;
85     
86       pTemp[iHalfN] = pVector[0];
87       for (i = 0; i < iHalfN; i++)
88         pTemp[i + 1 + iHalfN] = pVector[i + 1];
89       for (i = 0; i < iHalfN; i++)
90         pTemp[i] = pVector[i + iHalfN + 1];
91     } else {     // Even
92       int iHalfN = n / 2;
93       pTemp[iHalfN] = pVector[0];
94       for (i = 0; i < iHalfN; i++)
95         pTemp[i] = pVector[i + iHalfN];
96       for (i = 0; i < iHalfN - 1; i++)
97         pTemp[i + iHalfN + 1] = pVector[i+1];
98     }
99   
100     for (i = 0; i < n; i++)
101       pVector[i] = pTemp[i];
102     delete pTemp;
103   }
104 #if 0
105     static void shuffleNaturalToFourierOrder (float* pdVector, const int n);
106     static void shuffleNaturalToFourierOrder (double* pdVector, const int n);
107     static void shuffleNaturalToFourierOrder (std::complex<double>* pdVector, const int n);
108     static void shuffleFourierToNaturalOrder (float* pdVector, const int n);
109     static void shuffleFourierToNaturalOrder (double* pdVector, const int n);
110     static void shuffleFourierToNaturalOrder (std::complex<double>* pdVector, const int n);
111 #endif
112 };
113
114