Skip to content

Commit 3b2c9de

Browse files
committed
feat: add cfftfi, cffti, decompose, update cfftf
1 parent 3bfdc59 commit 3b2c9de

File tree

4 files changed

+358
-0
lines changed

4 files changed

+358
-0
lines changed

lib/node_modules/@stdlib/fft/base/fftpack/lib/cfftf.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ var cfftf1 = require( './cfftf1.js' );
7777
function cfftf( n, c, wsave ) {
7878
var iw1;
7979
var iw2;
80+
81+
// Function Body:
8082
if ( n === 1 ) {
8183
return;
8284
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*
19+
* ## Notice
20+
*
21+
* The original C code and copyright notice are from the [PFFFT library]{@link https://github.com/marton78/pffft/blob/master/fftpack.c}. The implementation follows the original, but has been modified for JavaScript.
22+
*
23+
* ```text
24+
* Copyright (c) 2004 the University Corporation for Atmospheric
25+
* Research ("UCAR"). All rights reserved. Developed by NCAR's
26+
* Computational and Information Systems Laboratory, UCAR,
27+
* www.cisl.ucar.edu.
28+
*
29+
* Redistribution and use of the Software in source and binary forms,
30+
* with or without modification, is permitted provided that the
31+
* following conditions are met:
32+
*
33+
* - Neither the names of NCAR's Computational and Information Systems
34+
* Laboratory, the University Corporation for Atmospheric Research,
35+
* nor the names of its sponsors or contributors may be used to
36+
* endorse or promote products derived from this Software without
37+
* specific prior written permission.
38+
*
39+
* - Redistributions of source code must retain the above copyright
40+
* notices, this list of conditions, and the disclaimer below.
41+
*
42+
* - Redistributions in binary form must reproduce the above copyright
43+
* notice, this list of conditions, and the disclaimer below in the
44+
* documentation and/or other materials provided with the
45+
* distribution.
46+
*
47+
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48+
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE WARRANTIES OF
49+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50+
* NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT
51+
* HOLDERS BE LIABLE FOR ANY CLAIM, INDIRECT, INCIDENTAL, SPECIAL,
52+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY, WHETHER IN AN
53+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
54+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
55+
* SOFTWARE.
56+
* ```
57+
*/
58+
59+
'use strict';
60+
61+
// MODULES //
62+
63+
var cffti1 = require( './cffti1.js' );
64+
65+
66+
// MAIN //
67+
68+
/**
69+
* Initializes the working array for the complex FFT.
70+
*
71+
* @private
72+
* @param {number} n - length of the sequence to transform
73+
* @param {Float64Array} wsave - working array containing precomputed values
74+
* @returns {void}
75+
*/
76+
function cffti( n, wsave ) {
77+
var iw1;
78+
var iw2;
79+
80+
// Function Body:
81+
if ( n === 1 ) {
82+
return;
83+
}
84+
iw1 = ( 2 * n ) + 1;
85+
iw2 = iw1 + ( 2 * n );
86+
cffti1( n, wsave[ iw1 - 1 ], wsave[ iw2 - 1 ] );
87+
}
88+
89+
90+
// EXPORTS //
91+
92+
module.exports = cffti;
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*
19+
* ## Notice
20+
*
21+
* The original C code and copyright notice are from the [PFFFT library]{@link https://github.com/marton78/pffft/blob/master/fftpack.c}. The implementation follows the original, but has been modified for JavaScript.
22+
*
23+
* ```text
24+
* Copyright (c) 2004 the University Corporation for Atmospheric
25+
* Research ("UCAR"). All rights reserved. Developed by NCAR's
26+
* Computational and Information Systems Laboratory, UCAR,
27+
* www.cisl.ucar.edu.
28+
*
29+
* Redistribution and use of the Software in source and binary forms,
30+
* with or without modification, is permitted provided that the
31+
* following conditions are met:
32+
*
33+
* - Neither the names of NCAR's Computational and Information Systems
34+
* Laboratory, the University Corporation for Atmospheric Research,
35+
* nor the names of its sponsors or contributors may be used to
36+
* endorse or promote products derived from this Software without
37+
* specific prior written permission.
38+
*
39+
* - Redistributions of source code must retain the above copyright
40+
* notices, this list of conditions, and the disclaimer below.
41+
*
42+
* - Redistributions in binary form must reproduce the above copyright
43+
* notice, this list of conditions, and the disclaimer below in the
44+
* documentation and/or other materials provided with the
45+
* distribution.
46+
*
47+
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48+
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE WARRANTIES OF
49+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50+
* NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT
51+
* HOLDERS BE LIABLE FOR ANY CLAIM, INDIRECT, INCIDENTAL, SPECIAL,
52+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY, WHETHER IN AN
53+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
54+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
55+
* SOFTWARE.
56+
* ```
57+
*/
58+
59+
'use strict';
60+
61+
// MODULES //
62+
63+
var sin = require( '@stdlib/math/base/special/sin' );
64+
var cos = require( '@stdlib/math/base/special/cos' );
65+
var TWO_PI = require( '@stdlib/constants/float64/two-pi' );
66+
var decompose = require( './decompose.js' );
67+
68+
69+
// VARIABLES //
70+
71+
var NTRYH = [ 3, 4, 2, 5 ]; // Initial trial factors for FFT factorization
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Initializes the working arrays for the FFT algorithm.
78+
*
79+
* @private
80+
* @param {number} n - length of the sequence
81+
* @param {Float64Array} wa - array of twiddle factors
82+
* @param {Int32Array} ifac - array containing factorization information
83+
* @returns {void}
84+
*/
85+
function cffti1( n, wa, ifac ) {
86+
var argld;
87+
var argh;
88+
var idot;
89+
var ipm;
90+
var ido;
91+
var arg;
92+
var nf;
93+
var ip;
94+
var ii;
95+
var ld;
96+
var fi;
97+
var l2;
98+
var l1;
99+
var k1;
100+
var i1;
101+
var j;
102+
var i;
103+
104+
nf = decompose( n, ifac, NTRYH );
105+
argh = TWO_PI / n;
106+
i = 2;
107+
l1 = 1;
108+
for ( k1 = 1; k1 <= nf; k1++ ) {
109+
ip = ifac[ k1 + 2 - 1 ];
110+
ld = 0;
111+
l2 = ip * l1;
112+
ido = n / l2;
113+
idot = ido + ido + 2;
114+
ipm = ip - 1;
115+
for ( j = 1; j <= ipm; j++ ) {
116+
i1 = i;
117+
wa[ i - 1 - 1 ] = 1.0;
118+
wa[ i - 1 ] = 0.0;
119+
ld += l1;
120+
fi = 0.0;
121+
argld = ld * argh;
122+
for ( ii = 4; ii <= idot; ii += 2 ) {
123+
i += 2;
124+
fi += 1.0;
125+
arg = fi * argld;
126+
wa[ i - 1 - 1 ] = cos( arg );
127+
wa[ i - 1 ] = sin( arg );
128+
}
129+
if ( ip > 5 ) {
130+
wa[ i1 - 1 - 1 ] = wa[ i - 1 - 1 ];
131+
wa[ i1 - 1 ] = wa[ i - 1 ];
132+
}
133+
}
134+
135+
l1 = l2;
136+
}
137+
}
138+
139+
140+
// EXPORTS //
141+
142+
module.exports = cffti1;
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*
19+
* ## Notice
20+
*
21+
* The original C code and copyright notice are from the [PFFFT library]{@link https://github.com/marton78/pffft/blob/master/fftpack.c}. The implementation follows the original, but has been modified for JavaScript.
22+
*
23+
* ```text
24+
* Copyright (c) 2004 the University Corporation for Atmospheric
25+
* Research ("UCAR"). All rights reserved. Developed by NCAR's
26+
* Computational and Information Systems Laboratory, UCAR,
27+
* www.cisl.ucar.edu.
28+
*
29+
* Redistribution and use of the Software in source and binary forms,
30+
* with or without modification, is permitted provided that the
31+
* following conditions are met:
32+
*
33+
* - Neither the names of NCAR's Computational and Information Systems
34+
* Laboratory, the University Corporation for Atmospheric Research,
35+
* nor the names of its sponsors or contributors may be used to
36+
* endorse or promote products derived from this Software without
37+
* specific prior written permission.
38+
*
39+
* - Redistributions of source code must retain the above copyright
40+
* notices, this list of conditions, and the disclaimer below.
41+
*
42+
* - Redistributions in binary form must reproduce the above copyright
43+
* notice, this list of conditions, and the disclaimer below in the
44+
* documentation and/or other materials provided with the
45+
* distribution.
46+
*
47+
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48+
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE WARRANTIES OF
49+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50+
* NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT
51+
* HOLDERS BE LIABLE FOR ANY CLAIM, INDIRECT, INCIDENTAL, SPECIAL,
52+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY, WHETHER IN AN
53+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
54+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
55+
* SOFTWARE.
56+
* ```
57+
*/
58+
59+
'use strict';
60+
61+
// MAIN //
62+
63+
/**
64+
* Decomposes the sequence length into its prime factors.
65+
*
66+
* @private
67+
* @param {number} n - length of the sequence
68+
* @param {Int32Array} ifac - array to store the factors
69+
* @param {Array<number>} ntryh - array of initial trial divisors
70+
* @returns {number} status code
71+
*/
72+
function decompose( n, ifac, ntryh ) {
73+
var ntry;
74+
var nl;
75+
var nf;
76+
var nq;
77+
var nr;
78+
var ib;
79+
var i;
80+
var j;
81+
82+
ntry = 0;
83+
nl = n;
84+
nf = 0;
85+
j = 0;
86+
do {
87+
if ( j < 4 ) {
88+
ntry = ntryh[ j ];
89+
} else {
90+
ntry += 2;
91+
}
92+
j += 1;
93+
while ( true ) {
94+
nq = nl / ntry;
95+
nr = nl - (ntry * nq);
96+
if ( nr !== 0 ) {
97+
break;
98+
}
99+
nf += 1;
100+
ifac[ nf + 2 ] = ntry;
101+
nl = nq;
102+
if ( ntry === 2 && nf !== 1 ) {
103+
for ( i = 2; i <= nf; i++ ) {
104+
ib = nf - i + 2;
105+
ifac[ ib + 2 ] = ifac[ ib + 1 ];
106+
}
107+
ifac[ 3 ] = 2;
108+
}
109+
if ( nl === 1 ) {
110+
break;
111+
}
112+
}
113+
} while ( nl !== 1 );
114+
ifac[ 1 ] = n;
115+
ifac[ 2 ] = nf;
116+
return nf;
117+
}
118+
119+
120+
// EXPORTS //
121+
122+
module.exports = decompose;

0 commit comments

Comments
 (0)