A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
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/* This is a slightly modified version of Mayer's contribution; write
47* msp@ucsd.edu for the original code. Kudos to Mayer for a fine piece
48* of work. -msp
49*/
50
51#ifdef MSW
52#pragma warning( disable : 4305 ) /* uncast const double to float */
53#pragma warning( disable : 4244 ) /* uncast double to float */
54#pragma warning( disable : 4101 ) /* unused local variables */
55#endif
56
57#define REAL float
58#define GOOD_TRIG
59
60#ifdef GOOD_TRIG
61#else
62#define FAST_TRIG
63#endif
64
65#if defined(GOOD_TRIG)
66#define FHT_SWAP(a,b,t) {(t)=(a);(a)=(b);(b)=(t);}
67#define TRIG_VARS \
68 int t_lam=0;
69#define TRIG_INIT(k,c,s) \
70 { \
71 int i; \
72 for (i=2 ; i<=k ; i++) \
73 {coswrk[i]=costab[i];sinwrk[i]=sintab[i];} \
74 t_lam = 0; \
75 c = 1; \
76 s = 0; \
77 }
78#define TRIG_NEXT(k,c,s) \
79 { \
80 int i,j; \
81 (t_lam)++; \
82 for (i=0 ; !((1<<i)&t_lam) ; i++); \
83 i = k-i; \
84 s = sinwrk[i]; \
85 c = coswrk[i]; \
86 if (i>1) \
87 { \
88 for (j=k-i+2 ; (1<<j)&t_lam ; j++); \
89 j = k - j; \
90 sinwrk[i] = halsec[i] * (sinwrk[i-1] + sinwrk[j]); \
91 coswrk[i] = halsec[i] * (coswrk[i-1] + coswrk[j]); \
92 } \
93 }
94#define TRIG_RESET(k,c,s)
95#endif
96
97#if defined(FAST_TRIG)
98#define TRIG_VARS \
99 REAL t_c,t_s;
100#define TRIG_INIT(k,c,s) \
101 { \
102 t_c = costab[k]; \
103 t_s = sintab[k]; \
104 c = 1; \
105 s = 0; \
106 }
107#define TRIG_NEXT(k,c,s) \
108 { \
109 REAL t = c; \
110 c = t*t_c - s*t_s; \
111 s = t*t_s + s*t_c; \
112 }
113#define TRIG_RESET(k,c,s)
114#endif
115
116static REAL halsec[20]=
117 {
118 0,
119 0,
120 .54119610014619698439972320536638942006107206337801,
121 .50979557910415916894193980398784391368261849190893,
122 .50241928618815570551167011928012092247859337193963,
123 .50060299823519630134550410676638239611758632599591,
124 .50015063602065098821477101271097658495974913010340,
125 .50003765191554772296778139077905492847503165398345,
126 .50000941253588775676512870469186533538523133757983,
127 .50000235310628608051401267171204408939326297376426,
128 .50000058827484117879868526730916804925780637276181,
129 .50000014706860214875463798283871198206179118093251,
130 .50000003676714377807315864400643020315103490883972,
131 .50000000919178552207366560348853455333939112569380,
132 .50000000229794635411562887767906868558991922348920,
133 .50000000057448658687873302235147272458812263401372
134 };
135static REAL costab[20]=
136 {
137 .00000000000000000000000000000000000000000000000000,
138 .70710678118654752440084436210484903928483593768847,
139 .92387953251128675612818318939678828682241662586364,
140 .98078528040323044912618223613423903697393373089333,
141 .99518472667219688624483695310947992157547486872985,
142 .99879545620517239271477160475910069444320361470461,
143 .99969881869620422011576564966617219685006108125772,
144 .99992470183914454092164649119638322435060646880221,
145 .99998117528260114265699043772856771617391725094433,
146 .99999529380957617151158012570011989955298763362218,
147 .99999882345170190992902571017152601904826792288976,
148 .99999970586288221916022821773876567711626389934930,
149 .99999992646571785114473148070738785694820115568892,
150 .99999998161642929380834691540290971450507605124278,
151 .99999999540410731289097193313960614895889430318945,
152 .99999999885102682756267330779455410840053741619428
153 };
154static REAL sintab[20]=
155 {
156 1.0000000000000000000000000000000000000000000000000,
157 .70710678118654752440084436210484903928483593768846,
158 .38268343236508977172845998403039886676134456248561,
159 .19509032201612826784828486847702224092769161775195,
160 .09801714032956060199419556388864184586113667316749,
161 .04906767432741801425495497694268265831474536302574,
162 .02454122852291228803173452945928292506546611923944,
163 .01227153828571992607940826195100321214037231959176,
164 .00613588464915447535964023459037258091705788631738,
165 .00306795676296597627014536549091984251894461021344,
166 .00153398018628476561230369715026407907995486457522,
167 .00076699031874270452693856835794857664314091945205,
168 .00038349518757139558907246168118138126339502603495,
169 .00019174759731070330743990956198900093346887403385,
170 .00009587379909597734587051721097647635118706561284,
171 .00004793689960306688454900399049465887274686668768
172 };
173static REAL coswrk[20]=
174 {
175 .00000000000000000000000000000000000000000000000000,
176 .70710678118654752440084436210484903928483593768847,
177 .92387953251128675612818318939678828682241662586364,
178 .98078528040323044912618223613423903697393373089333,
179 .99518472667219688624483695310947992157547486872985,
180 .99879545620517239271477160475910069444320361470461,
181 .99969881869620422011576564966617219685006108125772,
182 .99992470183914454092164649119638322435060646880221,
183 .99998117528260114265699043772856771617391725094433,
184 .99999529380957617151158012570011989955298763362218,
185 .99999882345170190992902571017152601904826792288976,
186 .99999970586288221916022821773876567711626389934930,
187 .99999992646571785114473148070738785694820115568892,
188 .99999998161642929380834691540290971450507605124278,
189 .99999999540410731289097193313960614895889430318945,
190 .99999999885102682756267330779455410840053741619428
191 };
192static REAL sinwrk[20]=
193 {
194 1.0000000000000000000000000000000000000000000000000,
195 .70710678118654752440084436210484903928483593768846,
196 .38268343236508977172845998403039886676134456248561,
197 .19509032201612826784828486847702224092769161775195,
198 .09801714032956060199419556388864184586113667316749,
199 .04906767432741801425495497694268265831474536302574,
200 .02454122852291228803173452945928292506546611923944,
201 .01227153828571992607940826195100321214037231959176,
202 .00613588464915447535964023459037258091705788631738,
203 .00306795676296597627014536549091984251894461021344,
204 .00153398018628476561230369715026407907995486457522,
205 .00076699031874270452693856835794857664314091945205,
206 .00038349518757139558907246168118138126339502603495,
207 .00019174759731070330743990956198900093346887403385,
208 .00009587379909597734587051721097647635118706561284,
209 .00004793689960306688454900399049465887274686668768
210 };
211
212
213#define SQRT2_2 0.70710678118654752440084436210484
214#define SQRT2 2*0.70710678118654752440084436210484
215
216void mayer_fht(REAL *fz, int n)
217{
218/* REAL a,b;
219REAL c1,s1,s2,c2,s3,c3,s4,c4;
220 REAL f0,g0,f1,g1,f2,g2,f3,g3; */
221 int k,k1,k2,k3,k4,kx;
222 REAL *fi,*fn,*gi;
223 TRIG_VARS;
224
225 for (k1=1,k2=0;k1<n;k1++)
226 {
227 REAL aa;
228 for (k=n>>1; (!((k2^=k)&k)); k>>=1);
229 if (k1>k2)
230 {
231 aa=fz[k1];fz[k1]=fz[k2];fz[k2]=aa;
232 }
233 }
234 for ( k=0 ; (1<<k)<n ; k++ );
235 k &= 1;
236 if (k==0)
237 {
238 for (fi=fz,fn=fz+n;fi<fn;fi+=4)
239 {
240 REAL f0,f1,f2,f3;
241 f1 = fi[0 ]-fi[1 ];
242 f0 = fi[0 ]+fi[1 ];
243 f3 = fi[2 ]-fi[3 ];
244 f2 = fi[2 ]+fi[3 ];
245 fi[2 ] = (f0-f2);
246 fi[0 ] = (f0+f2);
247 fi[3 ] = (f1-f3);
248 fi[1 ] = (f1+f3);
249 }
250 }
251 else
252 {
253 for (fi=fz,fn=fz+n,gi=fi+1;fi<fn;fi+=8,gi+=8)
254 {
255 REAL bs1,bc1,bs2,bc2,bs3,bc3,bs4,bc4,
256 bg0,bf0,bf1,bg1,bf2,bg2,bf3,bg3;
257 bc1 = fi[0 ] - gi[0 ];
258 bs1 = fi[0 ] + gi[0 ];
259 bc2 = fi[2 ] - gi[2 ];
260 bs2 = fi[2 ] + gi[2 ];
261 bc3 = fi[4 ] - gi[4 ];
262 bs3 = fi[4 ] + gi[4 ];
263 bc4 = fi[6 ] - gi[6 ];
264 bs4 = fi[6 ] + gi[6 ];
265 bf1 = (bs1 - bs2);
266 bf0 = (bs1 + bs2);
267 bg1 = (bc1 - bc2);
268 bg0 = (bc1 + bc2);
269 bf3 = (bs3 - bs4);
270 bf2 = (bs3 + bs4);
271 bg3 = SQRT2*bc4;
272 bg2 = SQRT2*bc3;
273 fi[4 ] = bf0 - bf2;
274 fi[0 ] = bf0 + bf2;
275 fi[6 ] = bf1 - bf3;
276 fi[2 ] = bf1 + bf3;
277 gi[4 ] = bg0 - bg2;
278 gi[0 ] = bg0 + bg2;
279 gi[6 ] = bg1 - bg3;
280 gi[2 ] = bg1 + bg3;
281 }
282 }
283 if (n<16) return;
284
285 do
286 {
287 REAL s1,c1;
288 int ii;
289 k += 2;
290 k1 = 1 << k;
291 k2 = k1 << 1;
292 k4 = k2 << 1;
293 k3 = k2 + k1;
294 kx = k1 >> 1;
295 fi = fz;
296 gi = fi + kx;
297 fn = fz + n;
298 do
299 {
300 REAL g0,f0,f1,g1,f2,g2,f3,g3;
301 f1 = fi[0 ] - fi[k1];
302 f0 = fi[0 ] + fi[k1];
303 f3 = fi[k2] - fi[k3];
304 f2 = fi[k2] + fi[k3];
305 fi[k2] = f0 - f2;
306 fi[0 ] = f0 + f2;
307 fi[k3] = f1 - f3;
308 fi[k1] = f1 + f3;
309 g1 = gi[0 ] - gi[k1];
310 g0 = gi[0 ] + gi[k1];
311 g3 = SQRT2 * gi[k3];
312 g2 = SQRT2 * gi[k2];
313 gi[k2] = g0 - g2;
314 gi[0 ] = g0 + g2;
315 gi[k3] = g1 - g3;
316 gi[k1] = g1 + g3;
317 gi += k4;
318 fi += k4;
319 } while (fi<fn);
320 TRIG_INIT(k,c1,s1);
321 for (ii=1;ii<kx;ii++)
322 {
323 REAL c2,s2;
324 TRIG_NEXT(k,c1,s1);
325 c2 = c1*c1 - s1*s1;
326 s2 = 2*(c1*s1);
327 fn = fz + n;
328 fi = fz +ii;
329 gi = fz +k1-ii;
330 do
331 {
332 REAL a,b,g0,f0,f1,g1,f2,g2,f3,g3;
333 b = s2*fi[k1] - c2*gi[k1];
334 a = c2*fi[k1] + s2*gi[k1];
335 f1 = fi[0 ] - a;
336 f0 = fi[0 ] + a;
337 g1 = gi[0 ] - b;
338 g0 = gi[0 ] + b;
339 b = s2*fi[k3] - c2*gi[k3];
340 a = c2*fi[k3] + s2*gi[k3];
341 f3 = fi[k2] - a;
342 f2 = fi[k2] + a;
343 g3 = gi[k2] - b;
344 g2 = gi[k2] + b;
345 b = s1*f2 - c1*g3;
346 a = c1*f2 + s1*g3;
347 fi[k2] = f0 - a;
348 fi[0 ] = f0 + a;
349 gi[k3] = g1 - b;
350 gi[k1] = g1 + b;
351 b = c1*g2 - s1*f3;
352 a = s1*g2 + c1*f3;
353 gi[k2] = g0 - a;
354 gi[0 ] = g0 + a;
355 fi[k3] = f1 - b;
356 fi[k1] = f1 + b;
357 gi += k4;
358 fi += k4;
359 } while (fi<fn);
360 }
361 TRIG_RESET(k,c1,s1);
362 } while (k4<n);
363}
364
365void mayer_fft(int n, REAL *real, REAL *imag)
366{
367 REAL a,b,c,d;
368 REAL q,r,s,t;
369 int i,j,k;
370 for (i=1,j=n-1,k=n/2;i<k;i++,j--) {
371 a = real[i]; b = real[j]; q=a+b; r=a-b;
372 c = imag[i]; d = imag[j]; s=c+d; t=c-d;
373 real[i] = (q+t)*.5; real[j] = (q-t)*.5;
374 imag[i] = (s-r)*.5; imag[j] = (s+r)*.5;
375 }
376 mayer_fht(real,n);
377 mayer_fht(imag,n);
378}
379
380void mayer_ifft(int n, REAL *real, REAL *imag)
381{
382 REAL a,b,c,d;
383 REAL q,r,s,t;
384 int i,j,k;
385 mayer_fht(real,n);
386 mayer_fht(imag,n);
387 for (i=1,j=n-1,k=n/2;i<k;i++,j--) {
388 a = real[i]; b = real[j]; q=a+b; r=a-b;
389 c = imag[i]; d = imag[j]; s=c+d; t=c-d;
390 imag[i] = (s+r)*0.5; imag[j] = (s-r)*0.5;
391 real[i] = (q-t)*0.5; real[j] = (q+t)*0.5;
392 }
393}
394
395void mayer_realfft(int n, REAL *real)
396{
397#ifdef ROCKBOX
398 REAL a,b;
399#else
400 REAL a,b,c,d;
401#endif
402 int i,j,k;
403 mayer_fht(real,n);
404 for (i=1,j=n-1,k=n/2;i<k;i++,j--) {
405 a = real[i];
406 b = real[j];
407 real[j] = (a-b)*0.5;
408 real[i] = (a+b)*0.5;
409 }
410}
411
412void mayer_realifft(int n, REAL *real)
413{
414#ifdef ROCKBOX
415 REAL a,b;
416#else
417 REAL a,b,c,d;
418#endif
419 int i,j,k;
420 for (i=1,j=n-1,k=n/2;i<k;i++,j--) {
421 a = real[i];
422 b = real[j];
423 real[j] = (a-b);
424 real[i] = (a+b);
425 }
426 mayer_fht(real,n);
427}
428