All files base.js

97.39% Statements 187/192
83.33% Branches 5/6
75% Functions 3/4
97.39% Lines 187/192

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 184 185 186 187 188 189 190 191 192 1933x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x     28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 24x 24x 28x 3x 3x 3x 3x 3x 3x 3x 3x       3x 3x 3x 3x 3x 3x 3x 3x 24x 24x 24x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 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.
*/
 
'use strict';
 
/* eslint-disable vars-on-top */
 
// MODULES //
 
var dtypes = require( '@stdlib/ndarray/dtypes' );
var factory = require( '@stdlib/ndarray/base/nullary-strided1d-dispatch-factory' );
var isEven = require( '@stdlib/math/base/assert/is-even' );
var floor = require( '@stdlib/math/base/special/floor' );
var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' );
var getStride = require( '@stdlib/ndarray/base/stride' );
var getOffset = require( '@stdlib/ndarray/base/offset' );
var getData = require( '@stdlib/ndarray/base/data-buffer' );
var gcopy = require( '@stdlib/blas/base/gcopy' ).ndarray;
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' );
var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var Float64Array = require( '@stdlib/array/float64' );
var Float32Array = require( '@stdlib/array/float32' );
var drffti = require( '@stdlib/fft/base/fftpack/float64/rffti' );
var drfftf = require( '@stdlib/fft/base/fftpack/float64/rfftf' );
var srffti = require( '@stdlib/fft/base/fftpack/float32/rffti' );
var srfftf = require( '@stdlib/fft/base/fftpack/float32/rfftf' );
var grffti = require( '@stdlib/fft/base/fftpack/generic/rffti' );
var grfftf = require( '@stdlib/fft/base/fftpack/generic/rfftf' );
 
 
// FUNCTIONS //
 
/**
* Computes the forward FFT and writes results to the output complex ndarray.
*
* @private
* @param {Function} reinterpret - complex-to-real reinterpret function
* @param {Function} rffti - workspace initialization function
* @param {Function} rfftf - forward FFT function
* @param {Function} Workspace - typed array constructor for workspace
* @param {ArrayLikeObject<Object>} arrays - array-like object containing ndarrays
* @returns {void}
*/
function kernel( reinterpret, rffti, rfftf, Workspace, arrays ) {
	var view;
	var out;
	var M;
	var N;
	var w;
	var x;
 
	out = arrays[ 0 ];
	x = arrays[ 1 ];
	N = numelDimension( x, 0 );
	M = floor( N/2 ) + 1;
	view = reinterpret( getData( out ), 0 );
 
	if ( N === 0 ) {
		return;
	}
	// Copy the input into the output buffer at offset 1:
	gcopy( N, getData( x ), getStride( x, 0 ), getOffset( x ), view, 1, 1 );
 
	// Initialize the workspace and compute the forward FFT in-place:
	w = new Workspace( ( 2*N ) + 34 );
	rffti( N, w, 1, 0 );
	rfftf( N, view, 1, 1, w, 1, 0 );
 
	// Move the DC component from the imaginary slot to the real slot of the first complex element and zero out the imaginary part:
	view[ 0 ] = view[ 1 ];
	view[ 1 ] = 0.0;
 
	// For even-length sequences, zero out the imaginary part of the Nyquist component:
	if ( isEven( N ) ) {
		view[ ( 2*M ) - 1 ] = 0.0;
	}
}
 
/**
* Computes the forward FFT of a real-valued input.
*
* @private
* @param {ArrayLikeObject<Object>} arrays - array-like object containing ndarrays
* @returns {void}
*/
function gkernel( arrays ) {
	kernel( reinterpret, grffti, grfftf, Float64Array, arrays );
}
 
/**
* Computes the forward FFT of a real-valued double-precision input.
*
* @private
* @param {ArrayLikeObject<Object>} arrays - array-like object containing ndarrays
* @returns {void}
*/
function dkernel( arrays ) {
	kernel( reinterpret128, drffti, drfftf, Float64Array, arrays );
}
 
/**
* Computes the forward FFT of a real-valued single-precision input.
*
* @private
* @param {ArrayLikeObject<Object>} arrays - array-like object containing ndarrays
* @returns {void}
*/
function skernel( arrays ) {
	kernel( reinterpret64, srffti, srfftf, Float32Array, arrays );
}
 
 
// VARIABLES //
 
var idtypes = dtypes( 'real_and_generic' );
var odtypes = dtypes( 'complex_floating_point' );
var table = {
	'types': [
		'complex128',
		'complex64'
	],
	'fcns': [
		dkernel,
		skernel
	],
	'default': gkernel
};
 
 
// MAIN //
 
/**
* Computes the one-dimensional discrete Fourier transform of a real-valued input ndarray and assigns the results to a provided output ndarray.
*
* @private
* @name rfftf
* @type {Function}
* @param {ndarray} out - output ndarray
* @param {ndarray} x - input ndarray
* @throws {TypeError} first argument must be an ndarray-like object
* @throws {TypeError} second argument must be an ndarray-like object
* @returns {ndarray} output ndarray
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var Complex128Array = require( '@stdlib/array/complex128' );
* var ndarray = require( '@stdlib/ndarray/ctor' );
*
* // Create a data buffer:
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
*
* // Define the shape of the input array:
* var sh = [ 4 ];
*
* // Define the array strides:
* var sx = [ 1 ];
*
* // Define the index offset:
* var ox = 0;
*
* // Create an input ndarray:
* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' );
*
* // Create an output ndarray:
* var out = new ndarray( 'complex128', new Complex128Array( 3 ), [ 3 ], [ 1 ], 0, 'row-major' );
*
* // Perform operation:
* var v = rfftf( out, x );
* // returns <ndarray>[ <Complex128>[ 10.0, 0.0 ], <Complex128>[ -2.0, 2.0 ], <Complex128>[ -2.0, 0.0 ] ]
*/
var rfftf = factory( table, [ idtypes ], odtypes );
 
 
// EXPORTS //
 
module.exports = rfftf;