Skip to main content

Module cordic

Module cordic 

Source
Expand description

Fixed-point CORDIC sine/cosine, in 32-bit and 64-bit word sizes.

The COordinate Rotation DIgital Computer computes sine and cosine with only shifts and adds: a vector is rotated toward the target angle by a fixed sequence of micro-rotations of angle atan(2^-i), each chosen clockwise or counter-clockwise. This implementation is entirely integer arithmetic, as it would be in hardware, and the single algorithm is instantiated for two word sizes:

typeangleresultsmax iterationsaccuracy
Cordic32 (alias Cordic)u32 binary anglei32, Q1.3030about 1e-8
Cordic64u64 binary anglei64, Q1.6262about 4e-16 at 52 iterations

A binary angle has 2^N units per turn, so it wraps naturally and its top two bits select the quadrant. Results are signed (sine and cosine go negative) with 1.0 represented as 1 << FRACTIONAL_BITS. The micro-rotation angles and the cumulative gain compensation are precomputed integer tables, and the shifts use round-to-nearest to keep the bias down.

Widening only the data would not help: the angle’s resolution limits the accuracy, so both words grow together. Cordic32 is the hardware-style choice (1e-8 is about -160 dB, beyond what a 24-bit converter chain can use); Cordic64 matches the 64-bit phase of the NCO and reaches double-precision accuracy, at the cost of about 1.7 times as many iterations.

See the book entry Signal sources: oscillators, CORDIC and waveforms.

Structs§

Cordic32
A 32-bit CORDIC rotator: u32 binary angles (2^32 per turn) and i32 Q1.30 results. Accurate to about 1e-8 (a dozen least-significant bits) at 30 iterations.
Cordic64
A 64-bit CORDIC rotator: u64 binary angles (2^64 per turn, the same width as the NCO phase) and i64 Q1.62 results. Reaches double-precision accuracy (about 4e-16) at 52 iterations and about 3e-18 at the maximum of 62.

Constants§

FRACTIONAL_BITS
Number of fractional bits in Cordic32 results.
MAX_ITERATIONS
The maximum number of Cordic32 micro-rotations.
ONE
The Cordic32 fixed-point representation of 1.0.

Functions§

angle_from_radians
Converts an angle in radians to a u32 binary angle (modulo one turn).
angle_from_turns
Converts an angle in turns to a u32 binary angle (modulo one turn).
angle_to_radians
Converts a u32 binary angle to radians in [0, 2*pi).
cordic_sin_cos
Returns (sin(angle), cos(angle)) for an angle in radians, using the 32-bit integer CORDIC with Cordic32::DEFAULT_ITERATIONS (accurate to about 1e-8). Use Cordic64 for full double-precision accuracy.

Type Aliases§

Cordic
The 32-bit rotator, kept under its original name.