Livt packages
Livt.Math
Version 1.7.2
Reusable integer and fixed-point arithmetic, complex numbers, trigonometry, and statistics for FPGA applications written in Livt.
Published files (90)
src/lookup/TrigQ15.lvt
namespace Livt.Math.Lookup
/**
* Small Q1.15 sine/cosine lookup values for common DSP examples.
* Supports size 4 and size 8 tables for deterministic examples.
*/
component TrigQ15
{
// Compile-time dimensions, bounds, and numeric constants.
public const SCALE: int = 32768
public const NEG_ONE: int = -32768
public const ZERO: int = 0
public const ONE: int = 32767
public const SQRT_HALF: int = 23170
/**
* Return cos(index * 2*pi / size) in Q15 for size 4 or 8.
*/
public fn Cos(index: int, size: int) int
{
if (size == 4) {
return this.Cos4(index)
}
if (size == 8) {
return this.Cos8(index)
}
return 32767
}
/**
* Return sin(index * 2*pi / size) in Q15 for size 4 or 8.
*/
public fn Sin(index: int, size: int) int
{
if (size == 4) {
return this.Sin4(index)
}
if (size == 8) {
return this.Sin8(index)
}
return 0
}
/**
* Wraps a signed index into 0..size-1. Requires a positive size; callers use four or
* eight.
*/
fn NormalizeIndex(index: int, size: int) int
{
// Only positive powers of two (4 and 8) reach this private helper.
// Masking also gives the required nonnegative wrapping for negative indices.
return index & (size - 1)
}
/**
* Returns the Q1.15 cosine at a quarter-turn index, wrapping negative and positive
* indices.
*/
fn Cos4(index: int) int
{
var value: int = this.NormalizeIndex(index, 4)
if (value == 0) {
return 32767
}
if (value == 1) {
return 0
}
if (value == 2) {
return -32768
}
return 0
}
/**
* Returns the Q1.15 sine at a quarter-turn index, wrapping negative and positive indices.
*/
fn Sin4(index: int) int
{
var value: int = this.NormalizeIndex(index, 4)
if (value == 0) {
return 0
}
if (value == 1) {
return 32767
}
if (value == 2) {
return 0
}
return -32768
}
/**
* Returns the Q1.15 cosine at an eighth-turn index, wrapping negative and positive
* indices.
*/
fn Cos8(index: int) int
{
var value: int = this.NormalizeIndex(index, 8)
if (value == 0) {
return 32767
}
if (value == 1) {
return 23170
}
if (value == 2) {
return 0
}
if (value == 3) {
return -23170
}
if (value == 4) {
return -32768
}
if (value == 5) {
return -23170
}
if (value == 6) {
return 0
}
return 23170
}
/**
* Returns the Q1.15 sine at an eighth-turn index, wrapping negative and positive indices.
*/
fn Sin8(index: int) int
{
var value: int = this.NormalizeIndex(index, 8)
if (value == 0) {
return 0
}
if (value == 1) {
return 23170
}
if (value == 2) {
return 32767
}
if (value == 3) {
return 23170
}
if (value == 4) {
return 0
}
if (value == 5) {
return -23170
}
if (value == 6) {
return -32768
}
return -23170
}
}