Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<!--

@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.

-->

# rfftf

> Compute the forward discrete Fourier transform (DFT) of a real-valued one-dimensional ndarray.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var rfftf = require( '@stdlib/fft/base/fftpack/ndarray/generic/rfftf' );
```

#### rfftf( arrays )

Computes the forward discrete Fourier transform (DFT) of a real-valued one-dimensional ndarray.

```javascript
var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var rffti = require( '@stdlib/fft/base/fftpack/ndarray/generic/rffti' );

var N = 4;
var len = scalar2ndarray( N, {
'dtype': 'int32'
});

var w = new Float64Vector( ( 2*N ) + 34 );
rffti( [ w, len ] );

var r = new Float64Vector( [ 1.0, 2.0, 3.0, 4.0 ] );

var out = rfftf( [ r, w ] );
// returns <ndarray>[ 10.0, -2.0, 2.0, -2.0 ]

var bool = ( out === r );
// returns true
```

The function has the following parameters:

- **arrays**: array-like object containing the following ndarrays:

- a one-dimensional input ndarray.
- a one-dimensional workspace ndarray containing pre-computed values.

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Before calling this function, initialize the workspace ndarray by calling [`rffti`][@stdlib/fft/base/fftpack/ndarray/generic/rffti] with the same sequence length and workspace layout.

- The function performs the transform in-place (i.e., the input ndarray is **mutated**).

- For `N = 4`, the output

```text
[ 10.0, -2.0, 2.0, -2.0 ]
```

corresponds to a zero-frequency term `10.0`, a complex coefficient `-2.0 + 2.0i` at frequency `1`, and a Nyquist term `-2.0`.

- If `N` equals `1`, the function returns early without modifying the input, as a single data point is its own Fourier transform.

- This transform is unnormalized as a call to this function followed by a call performing a [backward transform][@stdlib/fft/base/fftpack/ndarray/generic/rfftb] will multiply the input ndarray by `N`.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var zeros = require( '@stdlib/ndarray/zeros' );
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
var rffti = require( '@stdlib/fft/base/fftpack/ndarray/generic/rffti' );
var rfftf = require( '@stdlib/fft/base/fftpack/ndarray/generic/rfftf' );

var N = 4;
var r = discreteUniform( [ N ], -10, 10, {
'dtype': 'generic'
});
var w = zeros( [ ( 2*N ) + 34 ], {
'dtype': 'generic'
});
var len = scalar2ndarray( N, {
'dtype': 'int32'
});

console.log( ndarray2array( r ) );

rffti( [ w, len ] );
var out = rfftf( [ r, w ] );

console.log( ndarray2array( out ) );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/fft/base/fftpack/ndarray/generic/rffti]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fft/base/fftpack/ndarray/generic/rffti

[@stdlib/fft/base/fftpack/ndarray/generic/rfftb]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/fft/base/fftpack/ndarray/generic/rfftb

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* @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';

// MODULES //

var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/uniform' );
var floor = require( '@stdlib/math/base/special/floor' );
var pow = require( '@stdlib/math/base/special/pow' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var copy = require( '@stdlib/ndarray/copy' );
var zeros = require( '@stdlib/ndarray/zeros' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var format = require( '@stdlib/string/format' );
var rffti = require( '@stdlib/fft/base/fftpack/ndarray/generic/rffti' );
var pkg = require( './../package.json' ).name;
var rfftf = require( './../lib' );


// VARIABLES //

var options = {
'dtype': 'generic'
};
var nopts = {
'dtype': 'int32'
};


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} iter - number of iterations
* @param {PositiveInteger} N - sequence length
* @returns {Function} benchmark function
*/
function createBenchmark( iter, N ) {
var len;
var x;
var w;
var i;

x = [];
for ( i = 0; i < iter; i++ ) {
x.push( uniform( [ N ], -100.0, 100.0, options ) );
}
len = scalar2ndarray( N, nopts );
w = zeros( [ ( 2*N ) + 34 ], options );
rffti( [ w, len ] );

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var xc;
var v;
var i;

xc = [];
for ( i = 0; i < iter; i++ ) {
xc.push( copy( x[ i ] ) );
}
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = rfftf( [ xc[ i ], w ] );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( isnan( v.get( i%N ) ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var lengths;
var opts;
var iter;
var N;
var f;
var i;

lengths = [
8,
16,
32,
64,
128,
256,
512,
1024
];

iter = 1e6;

for ( i = 0; i < lengths.length; i++ ) {
N = lengths[ i ];
f = createBenchmark( iter, N );
opts = {
'iterations': iter
};
bench( format( '%s:N=%d', pkg, N ), opts, f );
iter = floor( pow( iter, 3.0/4.0 ) );
}
}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

{{alias}}( arrays )
Computes the forward discrete Fourier transform (DFT) of a real-valued
one-dimensional ndarray.

Before calling this function, one must first initialize the workspace
ndarray using the corresponding initialization function with the same
sequence length and workspace layout.

This transform is unnormalized as a call to this function followed by a
call performing a backward transform will multiply the input ndarray by the
sequence length.

Parameters
----------
arrays: ArrayLikeObject<ndarray>
Array-like object containing the following ndarrays:

- a one-dimensional input ndarray.
- a one-dimensional workspace ndarray containing pre-computed values.

Returns
-------
out: ndarray
Input ndarray.

Examples
--------
> var N = 4;
> var opts = { 'dtype': 'int32' };
> var len = {{alias:@stdlib/ndarray/from-scalar}}( N, opts );
> var w = new {{alias:@stdlib/ndarray/vector/float64}}( ( 2*N ) + 34 );
> {{alias:@stdlib/fft/base/fftpack/ndarray/generic/rffti}}( [ w, len ] );
> var r = new {{alias:@stdlib/ndarray/vector/float64}}( [ 1.0, 2.0, 3.0, 4.0 ] );
> var out = {{alias}}( [ r, w ] )
<ndarray>[ 10.0, -2.0, 2.0, -2.0 ]
> var bool = ( out === r )
true

See Also
--------

Loading
Loading