Fix distortion.

This commit is contained in:
George Norton 2023-09-02 00:56:10 +01:00
parent 0d23d73f4f
commit 354d8e09bb
2 changed files with 9 additions and 9 deletions

View File

@ -56,22 +56,20 @@ static inline int32_t norm_fix3_28_to_s16sample(fix3_28_t a) {
*/ */
// Saturate the value if an overflow has occurred // Saturate the value if an overflow has occurred
uint32_t upper = (a >> 30); uint32_t upper = (a >> 29);
if (a < 0) { if (a < 0) {
if (~upper) if (~upper) {
{
return 0xff800000; return 0xff800000;
} }
} else { } else {
if (upper) if (upper) {
{ return 0x00efffff;
return 0x007fffff;
} }
} }
/* When we converted the USB audio sample to a fixed point number, we applied /* When we converted the USB audio sample to a fixed point number, we applied
a normalization, or a gain of 1/65536. To convert it back, we can undo that a normalization, or a gain of 1/65536. To convert it back, we can undo that
by shifting it but we output 24bts, so the shift is reduced. */ by shifting it but we output 24bts, so the shift is reduced. */
return (a >> 4); return (a >> 5);
} }
static inline fix3_28_t fix3_28_from_flt(float a) { static inline fix3_28_t fix3_28_from_flt(float a) {

View File

@ -54,10 +54,12 @@ int main(int argc, char* argv[])
out[i] = in[i]; out[i] = in[i];
} }
const fix3_28_t preamp = fix3_28_from_flt(0.92f);
for (int i = 0; i < samples; i ++) for (int i = 0; i < samples; i ++)
{ {
// Left channel filter // Left channel filter
fix3_28_t x_f16 = norm_fix3_28_from_s16sample((int16_t) out[i]); fix3_28_t x_f16 = fix16_mul(norm_fix3_28_from_s16sample((int16_t) out[i]), preamp);
for (int j = 0; j < filter_stages; j++) for (int j = 0; j < filter_stages; j++)
{ {
@ -69,7 +71,7 @@ int main(int argc, char* argv[])
// Right channel filter // Right channel filter
i++; i++;
x_f16 = norm_fix3_28_from_s16sample((int16_t) out[i]); x_f16 = fix16_mul(norm_fix3_28_from_s16sample((int16_t) out[i]), preamp);
for (int j = 0; j < filter_stages; j++) for (int j = 0; j < filter_stages; j++)
{ {