2 ** FFT and FHT routines
3 ** Copyright 1988, 1993; Ron Mayer
6 ** Does a hartley transform of "n" points in the array "fz".
8 ** NOTE: This routine uses at least 2 patented algorithms, and may be
9 ** under the restrictions of a bunch of different organizations.
10 ** Although I wrote it completely myself; it is kind of a derivative
11 ** of a routine I once authored and released under the GPL, so it
12 ** may fall under the free software foundation's restrictions;
13 ** it was worked on as a Stanford Univ project, so they claim
14 ** some rights to it; it was further optimized at work here, so
15 ** I think this company claims parts of it. The patents are
16 ** held by R. Bracewell (the FHT algorithm) and O. Buneman (the
17 ** trig generator), both at Stanford Univ.
18 ** If it were up to me, I'd say go do whatever you want with it;
19 ** but it would be polite to give credit to the following people
20 ** if you use this anywhere:
21 ** Euler - probable inventor of the fourier transform.
22 ** Gauss - probable inventor of the FFT.
23 ** Hartley - probable inventor of the hartley transform.
24 ** Buneman - for a really cool trig generator
25 ** Mayer(me) - for authoring this particular version and
26 ** including all the optimizations in one package.
28 ** Ron Mayer; mayer@acuson.com
35 #define SQRT2 1.4142135623730951454746218587388284504414
38 static FLOAT costab
[20] = {
39 .00000000000000000000000000000000000000000000000000,
40 .70710678118654752440084436210484903928483593768847,
41 .92387953251128675612818318939678828682241662586364,
42 .98078528040323044912618223613423903697393373089333,
43 .99518472667219688624483695310947992157547486872985,
44 .99879545620517239271477160475910069444320361470461,
45 .99969881869620422011576564966617219685006108125772,
46 .99992470183914454092164649119638322435060646880221,
47 .99998117528260114265699043772856771617391725094433,
48 .99999529380957617151158012570011989955298763362218,
49 .99999882345170190992902571017152601904826792288976,
50 .99999970586288221916022821773876567711626389934930,
51 .99999992646571785114473148070738785694820115568892,
52 .99999998161642929380834691540290971450507605124278,
53 .99999999540410731289097193313960614895889430318945,
54 .99999999885102682756267330779455410840053741619428
56 static FLOAT sintab
[20] = {
57 1.0000000000000000000000000000000000000000000000000,
58 .70710678118654752440084436210484903928483593768846,
59 .38268343236508977172845998403039886676134456248561,
60 .19509032201612826784828486847702224092769161775195,
61 .09801714032956060199419556388864184586113667316749,
62 .04906767432741801425495497694268265831474536302574,
63 .02454122852291228803173452945928292506546611923944,
64 .01227153828571992607940826195100321214037231959176,
65 .00613588464915447535964023459037258091705788631738,
66 .00306795676296597627014536549091984251894461021344,
67 .00153398018628476561230369715026407907995486457522,
68 .00076699031874270452693856835794857664314091945205,
69 .00038349518757139558907246168118138126339502603495,
70 .00019174759731070330743990956198900093346887403385,
71 .00009587379909597734587051721097647635118706561284,
72 .00004793689960306688454900399049465887274686668768
75 /* This is a simplified version for n an even power of 2 */
76 /* MFC: In the case of LayerII encoding, n==1024 always. */
78 static void fht (FLOAT
* fz
)
80 int i
, k
, k1
, k2
, k3
, k4
, kx
;
86 unsigned short k1
, k2
;
1083 for (i
= 0; i
< sizeof k1k2tab
/ sizeof k1k2tab
[0]; ++i
) {
1092 for (fi
= fz
, fn
= fz
+ 1024; fi
< fn
; fi
+= 4) {
1093 FLOAT f0
, f1
, f2
, f3
;
1117 FLOAT g0
, f0
, f1
, g1
, f2
, g2
, f3
, g3
;
1118 f1
= fi
[0] - fi
[k1
];
1119 f0
= fi
[0] + fi
[k1
];
1120 f3
= fi
[k2
] - fi
[k3
];
1121 f2
= fi
[k2
] + fi
[k3
];
1126 g1
= gi
[0] - gi
[k1
];
1127 g0
= gi
[0] + gi
[k1
];
1128 g3
= SQRT2
* gi
[k3
];
1129 g2
= SQRT2
* gi
[k2
];
1142 for (i
= 1; i
< kx
; i
++) {
1145 c1
= t
* t_c
- s1
* t_s
;
1146 s1
= t
* t_s
+ s1
* t_c
;
1147 c2
= c1
* c1
- s1
* s1
;
1153 FLOAT a
, b
, g0
, f0
, f1
, g1
, f2
, g2
, f3
, g3
;
1154 b
= s2
* fi
[k1
] - c2
* gi
[k1
];
1155 a
= c2
* fi
[k1
] + s2
* gi
[k1
];
1160 b
= s2
* fi
[k3
] - c2
* gi
[k3
];
1161 a
= c2
* fi
[k3
] + s2
* gi
[k3
];
1166 b
= s1
* f2
- c1
* g3
;
1167 a
= c1
* f2
+ s1
* g3
;
1172 b
= c1
* g2
- s1
* f3
;
1173 a
= s1
* g2
+ c1
* f3
;
1188 #define ATANSIZE 2000
1189 #define ATANSCALE 50.0
1190 static FLOAT atan_t
[ATANSIZE
];
1192 INLINE FLOAT
atan_table(FLOAT y
, FLOAT x
) {
1195 index
= (int)(ATANSCALE
* fabs(y
/x
));
1196 if (index
>=ATANSIZE
)
1200 return( PI
- atan_t
[index
] );
1203 return( -atan_t
[index
] );
1206 return( atan_t
[index
] - PI
);
1208 return(atan_t
[index
]);
1211 void atan_table_init(void) {
1213 for (i
=0;i
<ATANSIZE
;i
++)
1214 atan_t
[i
] = atan((double)i
/ATANSCALE
);
1219 /* For variations on psycho model 2:
1220 N always equals 1024
1221 BUT in the returned values, no energy/phi is used at or above an index of 513 */
1222 void psycho_2_fft (FLOAT
* x_real
, FLOAT
* energy
, FLOAT
* phi
)
1223 /* got rid of size "N" argument as it is always 1024 for layerII */
1240 energy
[0] = x_real
[0] * x_real
[0];
1242 for (i
= 1, j
= 1023; i
< 512; i
++, j
--) {
1245 /* MFC FIXME Mar03 Why is this divided by 2.0?
1246 if a and b are the real and imaginary components then
1247 r = sqrt(a^2 + b^2),
1248 but, back in the psycho2 model, they calculate r=sqrt(energy),
1249 which, if you look at the original equation below is different */
1250 energy
[i
] = (a
* a
+ b
* b
) / 2.0;
1251 if (energy
[i
] < 0.0005) {
1257 phi
[i
] = atan_table(-a
, b
) + PI
/4;
1261 phi
[i
] = atan2(-(double)a
, (double)b
) + PI
/4;
1265 energy
[512] = x_real
[512] * x_real
[512];
1266 phi
[512] = atan2 (0.0, (double) x_real
[512]);
1270 void psycho_1_fft (FLOAT
* x_real
, FLOAT
* energy
, int N
)
1277 energy
[0] = x_real
[0] * x_real
[0];
1279 for (i
= 1, j
= N
- 1; i
< N
/ 2; i
++, j
--) {
1282 energy
[i
] = (a
* a
+ b
* b
) / 2.0;
1284 energy
[N
/ 2] = x_real
[N
/ 2] * x_real
[N
/ 2];