Skip to content

Instantly share code, notes, and snippets.

@profi200
Last active October 4, 2021 23:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save profi200/bb840c84020a44fa67cda1d017c7d2a1 to your computer and use it in GitHub Desktop.
Save profi200/bb840c84020a44fa67cda1d017c7d2a1 to your computer and use it in GitHub Desktop.
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Compile with "gcc -std=c17 -O2 -fstrict-aliasing -ffunction-sections -Wall -Wl,--gc-sections codecCoeffConvert.c -o codecCoeffConvert -lm"
int main(int argc, char *argv[])
{
if(argc != 4 && argc != 6)
{
fputs("Not enough arguments.\n", stderr);
return 1;
}
const bool fixedIn = strchr(argv[1], '.') == NULL;
double b0, b1, b2, a1, a2;
if(argc == 4) // First order.
{
if(fixedIn) // Fixed point input.
{
b0 = (double)strtol(argv[1], NULL, 0) / 32768;
b1 = (double)strtol(argv[2], NULL, 0) / 32768;
b2 = 0.0;
a1 = (double)strtol(argv[3], NULL, 0) / 32768 * (-1);
a2 = 0.0;
}
else // Normalized input.
{
b0 = strtod(argv[1], NULL) * 32768;
b1 = strtod(argv[2], NULL) * 32768;
b2 = 0.0;
a1 = strtod(argv[3], NULL) * 32768 * (-1);
a2 = 0.0;
}
}
else // Second order biquad.
{
if(fixedIn) // Fixed point input.
{
b0 = (double)strtol(argv[1], NULL, 0) / 32768;
b1 = (double)strtol(argv[2], NULL, 0) * 2 / 32768;
b2 = (double)strtol(argv[3], NULL, 0) / 32768;
a1 = (double)strtol(argv[4], NULL, 0) * 2 / 32768 * (-1);
a2 = (double)strtol(argv[5], NULL, 0) / 32768 * (-1);
}
else // Normalized input.
{
b0 = strtod(argv[1], NULL) * 32768;
b1 = strtod(argv[2], NULL) * 32768 / 2;
b2 = strtod(argv[3], NULL) * 32768;
a1 = strtod(argv[4], NULL) * 32768 / 2 * (-1);
a2 = strtod(argv[5], NULL) * 32768 * (-1);
}
}
if(!fixedIn)
{
printf("b0 = %d\nb1 = %d\nb2 = %d\n"
"a1 = %d\na2 = %d\n",
(int)round(b0), (int)round(b1), (int)round(b2),
(int)round(a1), (int)round(a2));
}
else
{
printf("b0 = %.17g\nb1 = %.17g\nb2 = %.17g\n"
"a1 = %.17g\na2 = %.17g\n",
b0, b1, b2, a1, a2);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment