add fixed-point a-law companding encoder/decoder

This commit is contained in:
2026-04-03 13:58:30 +02:00
parent 9769346951
commit 082590e5f5
2 changed files with 184 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
/*
* fold.h
* alaw.h
* Copyright (c) 2025 Christopher Herb
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -24,10 +24,23 @@
#pragma once
#include <cmath>
#include <cstdint>
#include "lut.h"
namespace trnr {
constexpr float A_LAW_A = 87.6f;
constexpr float A_LAW_LN_A = 4.474f;
constexpr int32_t A_LAW_A_Q15 = static_cast<int32_t>(A_LAW_A * (1 << 15));
constexpr int32_t A_LAW_LN_A_Q15 = static_cast<int32_t>(A_LAW_LN_A * (1 << 15));
constexpr int32_t ONE_LN_A_Q15 = (1 << 15) + A_LAW_LN_A_Q15;
// Threshold in Q15: 1/A ≈ 0.0114 → 373 in Q15
constexpr int32_t ENCODE_THRESHOLD_Q15 = static_cast<int32_t>((1.0f / A_LAW_A) * (1 << 15));
// Threshold in Q15: 1/(1+ln(A)) ≈ 0.183 → 5996 in Q15
constexpr int32_t DECODE_THRESHOLD_Q15 = static_cast<int32_t>((1.0f / (1.0f + A_LAW_LN_A)) * (1 << 15));
inline float alaw_encode(float input)
{
@@ -59,4 +72,68 @@ inline float alaw_decode(float input)
return sample;
}
} // namespace trnr
inline int16_t alaw_encode_fixed(int16_t input)
{
int32_t sign = (input >= 0) ? 1 : -1;
int32_t abs_sample = std::abs(input);
int32_t output;
if (abs_sample < ENCODE_THRESHOLD_Q15) {
// Linear region: y = A*x / (1 + ln(A))
// A_Q15 * x_Q15 / ONE_LN_A_Q15 where ONE_LN_A_Q15 is already scaled by 32768
output = (int32_t)(((int64_t)A_LAW_A_Q15 * abs_sample) / ONE_LN_A_Q15);
} else {
// Logarithmic region: y = (1 + ln(A*x)) / (1 + ln(A))
// Scale Q15 sample to log table input range [4, 4096]
int32_t log_input = std::max(4, abs_sample >> 3);
int32_t log_x_q15 = fixed_log(log_input);
// ln(A*x) = ln(A) + ln(x) in Q15
int32_t log_ax_q15 = A_LAW_LN_A_Q15 + log_x_q15;
// y = (1 + ln(A*x)) / (1 + ln(A))
// numerator in Q15, denominator is ONE_LN_A_Q15 (which is (1+ln(A))*32768)
int64_t numerator = ((int64_t)1 << 15) + log_ax_q15;
output = (int32_t)((numerator << 15) / ONE_LN_A_Q15);
}
output *= sign;
// Clamp to int16 range
if (output > 32767) output = 32767;
if (output < -32768) output = -32768;
return static_cast<int16_t>(output);
}
inline int16_t alaw_decode_fixed(int16_t input)
{
int32_t sign = (input >= 0) ? 1 : -1;
int32_t abs_comp = std::abs(input);
int32_t sample;
if (abs_comp < DECODE_THRESHOLD_Q15) {
// Linear region: x = y * (1 + ln(A)) / A
sample = (int32_t)(((int64_t)abs_comp * ONE_LN_A_Q15) / A_LAW_A_Q15);
} else {
// Logarithmic region: x = exp(y * (1 + ln(A)) - 1) / A
// exp_arg_q12 = ((y_q15 * ONE_LN_A_Q15 * 8192) / (32768*32768*9)) - 910
// Computing in 64-bit to avoid overflow
int64_t temp = (((int64_t)abs_comp * ONE_LN_A_Q15) * 8192LL) / (32768LL * 32768LL * 9LL) - 910;
int32_t exp_arg_q12 = (int32_t)temp;
// Clamp to valid range
if (exp_arg_q12 < 0) exp_arg_q12 = 0;
if (exp_arg_q12 > 4096) exp_arg_q12 = 4096;
int32_t exp_val_q15 = fixed_exp(exp_arg_q12);
sample = (int32_t)(((int64_t)exp_val_q15 << 15) / A_LAW_A_Q15);
}
sample *= sign;
if (sample > 32767) sample = 32767;
if (sample < -32768) sample = -32768;
return static_cast<int16_t>(sample);
}
} // namespace trnr