Pd++  0.01
A pure C++ implementation of Pure Data objects
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
fft.h
1 /*
2  ** FFT and FHT routines
3  ** Copyright 1988, 1993; Ron Mayer
4  **
5  ** mayer_fht(fz,n);
6  ** Does a hartley transform of "n" points in the array "fz".
7  ** mayer_fft(n,real,imag)
8  ** Does a fourier transform of "n" points of the "real" and
9  ** "imag" arrays.
10  ** mayer_ifft(n,real,imag)
11  ** Does an inverse fourier transform of "n" points of the "real"
12  ** and "imag" arrays.
13  ** mayer_realfft(n,real)
14  ** Does a real-valued fourier transform of "n" points of the
15  ** "real" array. The real part of the transform ends
16  ** up in the first half of the array and the imaginary part of the
17  ** transform ends up in the second half of the array.
18  ** mayer_realifft(n,real)
19  ** The inverse of the realfft() routine above.
20  **
21  **
22  ** NOTE: This routine uses at least 2 patented algorithms, and may be
23  ** under the restrictions of a bunch of different organizations.
24  ** Although I wrote it completely myself, it is kind of a derivative
25  ** of a routine I once authored and released under the GPL, so it
26  ** may fall under the free software foundation's restrictions;
27  ** it was worked on as a Stanford Univ project, so they claim
28  ** some rights to it; it was further optimized at work here, so
29  ** I think this company claims parts of it. The patents are
30  ** held by R. Bracewell (the FHT algorithm) and O. Buneman (the
31  ** trig generator), both at Stanford Univ.
32  ** If it were up to me, I'd say go do whatever you want with it;
33  ** but it would be polite to give credit to the following people
34  ** if you use this anywhere:
35  ** Euler - probable inventor of the fourier transform.
36  ** Gauss - probable inventor of the FFT.
37  ** Hartley - probable inventor of the hartley transform.
38  ** Buneman - for a really cool trig generator
39  ** Mayer(me) - for authoring this particular version and
40  ** including all the optimizations in one package.
41  ** Thanks,
42  ** Ron Mayer; mayer@acuson.com
43  **
44  */
45 
46 #ifndef __Pd____fft__
47 #define __Pd____fft__
48 
49 #include <string>
50 
51 
52 
53 
54 /* These pragmas are only used for MSVC, not MinGW or Cygwin <hans@at.or.at> */
55 #ifdef _MSC_VER
56 #pragma warning( disable : 4305 ) /* uncast const double to float */
57 #pragma warning( disable : 4244 ) /* uncast double to float */
58 #pragma warning( disable : 4101 ) /* unused local variables */
59 #endif
60 
61 
62 #define REAL double
63 #define GOOD_TRIG
64 
65 /* the following is needed only to declare pd_fft() as exportable in MSW */
66 #ifdef GOOD_TRIG
67 #else
68 #define FAST_TRIG
69 #endif
70 
71 #if defined(GOOD_TRIG)
72 #define FHT_SWAP(a,b,t) {(t)=(a);(a)=(b);(b)=(t);}
73 #define TRIG_VARS \
74 int t_lam=0;
75 #define TRIG_INIT(k,c,s) \
76 { \
77 int i; \
78 for (i=2 ; i<=k ; i++) \
79 {coswrk[i]=costab[i];sinwrk[i]=sintab[i];} \
80 t_lam = 0; \
81 c = 1; \
82 s = 0; \
83 }
84 #define TRIG_NEXT(k,c,s) \
85 { \
86 int i,j; \
87 (t_lam)++; \
88 for (i=0 ; !((1<<i)&t_lam) ; i++); \
89 i = k-i; \
90 s = sinwrk[i]; \
91 c = coswrk[i]; \
92 if (i>1) \
93 { \
94 for (j=k-i+2 ; (1<<j)&t_lam ; j++); \
95 j = k - j; \
96 sinwrk[i] = halsec[i] * (sinwrk[i-1] + sinwrk[j]); \
97 coswrk[i] = halsec[i] * (coswrk[i-1] + coswrk[j]); \
98 } \
99 }
100 #define TRIG_RESET(k,c,s)
101 #endif
102 
103 #if defined(FAST_TRIG)
104 #define TRIG_VARS \
105 REAL t_c,t_s;
106 #define TRIG_INIT(k,c,s) \
107 { \
108 t_c = costab[k]; \
109 t_s = sintab[k]; \
110 c = 1; \
111 s = 0; \
112 }
113 #define TRIG_NEXT(k,c,s) \
114 { \
115 REAL t = c; \
116 c = t*t_c - s*t_s; \
117 s = t*t_s + s*t_c; \
118 }
119 #define TRIG_RESET(k,c,s)
120 #endif
121 
122 /*! \brief An FFT superclass.
123  Adapted from the Pd version of FFT.
124  */
125 class FFT {
126 
127 private:
128  REAL halsec[20]=
129  {
130  0,
131  0,
132  .54119610014619698439972320536638942006107206337801,
133  .50979557910415916894193980398784391368261849190893,
134  .50241928618815570551167011928012092247859337193963,
135  .50060299823519630134550410676638239611758632599591,
136  .50015063602065098821477101271097658495974913010340,
137  .50003765191554772296778139077905492847503165398345,
138  .50000941253588775676512870469186533538523133757983,
139  .50000235310628608051401267171204408939326297376426,
140  .50000058827484117879868526730916804925780637276181,
141  .50000014706860214875463798283871198206179118093251,
142  .50000003676714377807315864400643020315103490883972,
143  .50000000919178552207366560348853455333939112569380,
144  .50000000229794635411562887767906868558991922348920,
145  .50000000057448658687873302235147272458812263401372
146  };
147  REAL costab[20]=
148  {
149  .00000000000000000000000000000000000000000000000000,
150  .70710678118654752440084436210484903928483593768847,
151  .92387953251128675612818318939678828682241662586364,
152  .98078528040323044912618223613423903697393373089333,
153  .99518472667219688624483695310947992157547486872985,
154  .99879545620517239271477160475910069444320361470461,
155  .99969881869620422011576564966617219685006108125772,
156  .99992470183914454092164649119638322435060646880221,
157  .99998117528260114265699043772856771617391725094433,
158  .99999529380957617151158012570011989955298763362218,
159  .99999882345170190992902571017152601904826792288976,
160  .99999970586288221916022821773876567711626389934930,
161  .99999992646571785114473148070738785694820115568892,
162  .99999998161642929380834691540290971450507605124278,
163  .99999999540410731289097193313960614895889430318945,
164  .99999999885102682756267330779455410840053741619428
165  };
166  REAL sintab[20]=
167  {
168  1.0000000000000000000000000000000000000000000000000,
169  .70710678118654752440084436210484903928483593768846,
170  .38268343236508977172845998403039886676134456248561,
171  .19509032201612826784828486847702224092769161775195,
172  .09801714032956060199419556388864184586113667316749,
173  .04906767432741801425495497694268265831474536302574,
174  .02454122852291228803173452945928292506546611923944,
175  .01227153828571992607940826195100321214037231959176,
176  .00613588464915447535964023459037258091705788631738,
177  .00306795676296597627014536549091984251894461021344,
178  .00153398018628476561230369715026407907995486457522,
179  .00076699031874270452693856835794857664314091945205,
180  .00038349518757139558907246168118138126339502603495,
181  .00019174759731070330743990956198900093346887403385,
182  .00009587379909597734587051721097647635118706561284,
183  .00004793689960306688454900399049465887274686668768
184  };
185  REAL coswrk[20]=
186  {
187  .00000000000000000000000000000000000000000000000000,
188  .70710678118654752440084436210484903928483593768847,
189  .92387953251128675612818318939678828682241662586364,
190  .98078528040323044912618223613423903697393373089333,
191  .99518472667219688624483695310947992157547486872985,
192  .99879545620517239271477160475910069444320361470461,
193  .99969881869620422011576564966617219685006108125772,
194  .99992470183914454092164649119638322435060646880221,
195  .99998117528260114265699043772856771617391725094433,
196  .99999529380957617151158012570011989955298763362218,
197  .99999882345170190992902571017152601904826792288976,
198  .99999970586288221916022821773876567711626389934930,
199  .99999992646571785114473148070738785694820115568892,
200  .99999998161642929380834691540290971450507605124278,
201  .99999999540410731289097193313960614895889430318945,
202  .99999999885102682756267330779455410840053741619428
203  };
204  REAL sinwrk[20]=
205  {
206  1.0000000000000000000000000000000000000000000000000,
207  .70710678118654752440084436210484903928483593768846,
208  .38268343236508977172845998403039886676134456248561,
209  .19509032201612826784828486847702224092769161775195,
210  .09801714032956060199419556388864184586113667316749,
211  .04906767432741801425495497694268265831474536302574,
212  .02454122852291228803173452945928292506546611923944,
213  .01227153828571992607940826195100321214037231959176,
214  .00613588464915447535964023459037258091705788631738,
215  .00306795676296597627014536549091984251894461021344,
216  .00153398018628476561230369715026407907995486457522,
217  .00076699031874270452693856835794857664314091945205,
218  .00038349518757139558907246168118138126339502603495,
219  .00019174759731070330743990956198900093346887403385,
220  .00009587379909597734587051721097647635118706561284,
221  .00004793689960306688454900399049465887274686668768
222  };
223 
224 
225 
226 public:
227  FFT();
228  ~FFT();
229  void mayer_fht(double *fz, int n);
230  void mayer_fft(int n, double *real, double *imag);
231  void mayer_ifft(int n, double *real, double *imag);
232  void mayer_realfft(int n, double *real);
233  void mayer_realifft(int n, double *real);
234 
235 };
236 
237 #endif /* defined(__Pd____fft__) */
An FFT superclass. Adapted from the Pd version of FFT.
Definition: fft.h:125