All files / fft/base/fftpack/lib rffti1.js

65.57% Statements 120/183
66.66% Branches 2/3
100% Functions 1/1
65.57% Lines 120/183

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 1841x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                               1x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* ## Notice
*
* 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.
*
* ```text
* Copyright (c) 2004 the University Corporation for Atmospheric
* Research ("UCAR"). All rights reserved. Developed by NCAR's
* Computational and Information Systems Laboratory, UCAR,
* www.cisl.ucar.edu.
*
* Redistribution and use of the Software in source and binary forms,
* with or without modification, is permitted provided that the
* following conditions are met:
*
*     - Neither the names of NCAR's Computational and Information Systems
*       Laboratory, the University Corporation for Atmospheric Research,
*       nor the names of its sponsors or contributors may be used to
*       endorse or promote products derived from this Software without
*       specific prior written permission.
*
*     - Redistributions of source code must retain the above copyright
*       notices, this list of conditions, and the disclaimer below.
*
*     - Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions, and the disclaimer below in the
*       documentation and/or other materials provided with the
*       distribution.
*
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
* SOFTWARE.
* ```
*/
 
'use strict';
 
// MODULES //
 
var sincos = require( '@stdlib/math/base/special/sincos' ).assign;
var TWO_PI = require( '@stdlib/constants/float64/two-pi' );
var floor = require( '@stdlib/math/base/special/floor' );
var decompose = require( './decompose.js' );
 
 
// VARIABLES //
 
// Define a list of initial trial factors for FFT factorization:
var TRIAL_FACTORS = [ 4, 2, 3, 5 ];
 
 
// MAIN //
 
/**
* Initializes the working arrays for applying a Fourier transform to a real-valued sequence.
*
* @private
* @param {NonNegativeInteger} N - length of the sequence
* @param {Collection} twiddles - array of twiddle factors
* @param {integer} strideT - stride length for `twiddles`
* @param {NonNegativeInteger} offsetT - starting index for `twiddles`
* @param {Collection} factors - array containing a radix factorization
* @param {integer} strideF - stride length for `factors`
* @param {NonNegativeInteger} offsetF - starting index for `factors`
* @returns {void}
*/
function rffti1( N, twiddles, strideT, offsetT, factors, strideF, offsetF ) {
	var factor;
	var argld;
	var argh;
	var fidx;
	var nf;
	var fi;
	var l2;
	var l1;
	var ld;
	var st;
	var it;
	var im;
	var M;
	var m;
	var k;
	var j;
 
	// Decompose the sequence length into its radix factors:
	nf = decompose( N, TRIAL_FACTORS, factors, strideF, offsetF );
 
	// If the number of radix factors is `1`, the only twiddle factor we need is `W_N^0 = 1`, which the main transform kernels already hard-code, so nothing to pre-compute...
	if ( nf-1 === 0 ) {
		return;
	}
	// Compute the master angular step (i.e., the basic angle that generates all twiddles):
	argh = TWO_PI / N;

	// Define the location of the first sine term we want to compute:
	im = 3;

	// Initialize a running product of factors already processed:
	l1 = 1;

	// Compute the index of the first radix factor:
	fidx = offsetF + ( 2*strideF );

	// Compute the stride length for each cosine/sine pair:
	st = 2 * strideT;

	// Generate twiddle factors...
	for ( k = 0; k < nf-1; k++ ) {
		// Resolve the next radix factor:
		factor = factors[ fidx ];

		// Compute the length of the transform after including the current radix:
		l2 = factor * l1;

		// Compute the number of the "butterfly wings" (at this stage, the data is viewed as a `factor`-point transform of sub-vectors of length `l1`; `M` describes how many such vectors fit in the full array of length `N`):
		M = floor( N / l2 );

		// Initialize a running offset used to step the angle:
		ld = 0;

		// Iterate over each butterfly column within the current radix...
		for ( j = 1; j < factor; j++ ) {
			// Advance to the next column:
			ld += l1;

			// Compute the angle step for this column:
			argld = ld * argh; // 2Ļ€ ā‹… j ā‹… li / N, which is the j-th column's base angle

			// Initialize a counter which counts from `1` to `(M/2)-1` (i.e., all non-trivial harmonics):
			fi = 1.0;

			// Compute the index of the sine term:
			it = offsetT + ( im*strideT );

			// Iterate over each non-trivial harmonic in the column (note: we skip the `m=0` and `m=1` (i.e., the first cosine/sine pair, the first harmonic `W_N^0 = cos(0) + j sin(0) = 1 + jā‹…0` is trivial and hard-coded by transform kernels)...
			for ( m = 2; m < M; m += 2 ) {
				// Compute `(cos(fi*argld), sin(fi*argld))`:
				sincos( fi*argld, twiddles, -strideT, it ); // note: `sincos` returns the sine and cosine, in that order, so we need to use a negative stride when assigning the values to `twiddles` to ensure that cosine comes before sine in the twiddles table

				// Update for the next harmonic:
				fi += 1.0;

				// Resolve the index of the next sine term:
				it += st;
			}
			// Advance `im` by the size of the current block, skipping the cosine/sine pairs we have just written:
			im += M;
		}
		// Update the running product of factors already processed:
		l1 = l2;

		// Resolve the index of the next radix factor:
		fidx += strideF;
	}
}
 
 
// EXPORTS //
 
module.exports = rffti1;