Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<!--

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

-->

# cosqi

> Initialize a workspace array for performing a quarter-wave cosine transform on a one-dimensional double-precision floating-point ndarray.

<section class="intro">

</section>

<!-- /.intro -->

<section class="usage">

## Usage

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

#### cosqi( arrays )

Initializes a workspace array for performing a quarter-wave cosine transform on a one-dimensional double-precision floating-point ndarray.

```javascript
var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var Slice = require( '@stdlib/slice/ctor' );
var slice = require( '@stdlib/ndarray/slice' );

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

var w = new Float64Vector( ( 3*N ) + 34 );

var out = cosqi( [ w, len ] );
// returns <ndarray>

var bool = ( out === w );
// returns true

var cosineTable = slice( w, new Slice( 0, N ) );
// returns <ndarray>[ ~0.98, ~0.92, ~0.83, ~0.7, ~0.56, ~0.38, ~0.2, ~0.0 ]

var twiddleFactors = slice( w, new Slice( 2*N, 3*N ) );
// returns <ndarray>[ ~0.707, ~0.707, 0, 0, 0, 0, 0, 0 ]

var factors = slice( w, new Slice( 3*N, ( 3*N ) + 4 ) );
// returns <ndarray>[ 8, 2, 2, 4 ]
```

The function has the following parameters:

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

- a one-dimensional input ndarray.
- a zero-dimensional ndarray containing the length of the sequence to transform.

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Let `N` equal the length of the sequence to transform. The input ndarray is divided into four sections:

```text
size = N N N 2+ceil(log2(N)/2)
↓ ↓ ↓ ↓
| cosine table | scratch / workspace | twiddle factors | radix factor table |
↑ ↑ ↑ ↑
i = 0 ... N ... 2N ... 3N ...
```

- **cosine table**: a table of precomputed cosine coefficients used by quarter-wave cosine transforms.
- **scratch/workspace**: used as a scratch space when performing transforms. This section is not updated during initialization.
- **twiddle factors**: a table of reusable complex-exponential constants stored as cosine/sine pairs.
- **radix factor table**: a table containing the sequence length `N`, the number of factors into which `N` was decomposed, and the individual integer radix factors.

- In general, an input ndarray should have `3N + 34` indexed elements (as `log2(N)/2 ≤ 32` for all `2^64`). During initialization, only the sections for storing the cosine coefficients, twiddle factors, and the factorization of `N` are updated.

- The radix factor table is comprised as follows:

```text
| sequence_length | number_of_factors | integer_factors |
```

- If `N` equals `0`, the function returns the input ndarray unchanged.

</section>

<!-- /.notes -->

<section class="examples">

## Examples

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

```javascript
var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var cosqi = require( '@stdlib/fft/base/fftpack/ndarray/float64/cosqi' );

var N = 8;

var w = new Float64Vector( ( 3*N ) + 34 );
console.log( ndarray2array( w ) );

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

var out = cosqi( [ w, len ] );
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">

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* @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 Float64Vector = require( '@stdlib/ndarray/vector/float64' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var cosqi = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} N - sequence length
* @returns {Function} benchmark function
*/
function createBenchmark( N ) {
var len;
var w;

len = scalar2ndarray( N, {
'dtype': 'int32'
});
w = new Float64Vector( ( 3*N ) + 34 );

return benchmark;

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = cosqi( [ w, len ] );
if ( typeof v !== 'object' ) {
b.fail( 'should return an ndarray' );
}
}
b.toc();
if ( isnan( v.get( 2*N ) ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

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

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

for ( i = 0; i < lengths.length; i++ ) {
N = lengths[ i ];
f = createBenchmark( N );
bench( format( '%s:N=%d', pkg, N ), f );
}
}

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

{{alias}}( arrays )
Initializes a workspace array for performing a quarter-wave cosine transform
on a one-dimensional double-precision floating-point ndarray.

If the length of the sequence to transform is less than or equal to zero,
the function returns the input ndarray unchanged.

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

- a one-dimensional input ndarray.
- a zero-dimensional ndarray containing the length of the sequence to
transform.

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

Examples
--------
> var N = 8;
> var w = new {{alias:@stdlib/ndarray/vector/float64}}( ( 3*N ) + 34 );
> var opts = { 'dtype': 'int32' };
> var len = {{alias:@stdlib/ndarray/from-scalar}}( N, opts );
> var out = {{alias}}( [ w, len ] )
<ndarray>
> var bool = ( out === w )
true
> var s = new {{alias:@stdlib/slice/ctor}}( 0, N );
> var cosineTable = {{alias:@stdlib/ndarray/slice}}( w, s )
<ndarray>[ ~0.98, ~0.92, ~0.83, ~0.7, ~0.56, ~0.38, ~0.2, ~0.0 ]
> s = new {{alias:@stdlib/slice/ctor}}( 2*N, 3*N );
> var twiddleFactors = {{alias:@stdlib/ndarray/slice}}( w, s )
<ndarray>[ ~0.707, ~0.707, 0, 0, 0, 0, 0, 0 ]
> s = new {{alias:@stdlib/slice/ctor}}( 3*N, ( 3*N ) + 4 );
> var factors = {{alias:@stdlib/ndarray/slice}}( w, s )
<ndarray>[ 8, 2, 2, 4 ]

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* @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.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

import { typedndarray, float64ndarray } from '@stdlib/types/ndarray';

/**
* Initializes a workspace array for performing a quarter-wave cosine transform on a one-dimensional double-precision floating-point ndarray.
*
* ## Notes
*
* - The function expects the following ndarrays:
*
* - a one-dimensional input ndarray.
* - a zero-dimensional ndarray containing the length of the sequence to transform.
*
* @param arrays - array-like object containing ndarrays
* @returns input ndarray
*
* @example
* var Float64Vector = require( '@stdlib/ndarray/vector/float64' );
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
* var Slice = require( '@stdlib/slice/ctor' );
* var slice = require( '@stdlib/ndarray/slice' );
*
* var N = 8;
* var len = scalar2ndarray( N, {
* 'dtype': 'int32'
* });
*
* var w = new Float64Vector( ( 3*N ) + 34 );
*
* var out = cosqi( [ w, len ] );
* // returns <ndarray>
*
* var bool = ( out === w );
* // returns true
*
* var cosineTable = slice( w, new Slice( 0, N ) );
* // returns <ndarray>[ ~0.98, ~0.92, ~0.83, ~0.7, ~0.56, ~0.38, ~0.2, ~0.0 ]
*
* var twiddleFactors = slice( w, new Slice( 2*N, 3*N ) );
* // returns <ndarray>[ ~0.707, ~0.707, 0, 0, 0, 0, 0, 0 ]
*
* var factors = slice( w, new Slice( 3*N, ( 3*N ) + 4 ) );
* // returns <ndarray>[ 8, 2, 2, 4 ]
*/
declare function cosqi( arrays: [ float64ndarray, typedndarray<number> ] ): float64ndarray;


// EXPORTS //

export = cosqi;
Loading