diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2018-11-03 21:17:03 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2018-11-03 21:17:03 +0300 |
commit | d8502a297b556cb06bf59bd00b2524931a8f034a (patch) | |
tree | d2633c73f96b0e05f0dcc246d4465cee35aa4a43 | |
parent | f054da2e4b4138c987c0b4f760b16c092664cb98 (diff) | |
download | libgha-d8502a297b556cb06bf59bd00b2524931a8f034a.tar.gz |
A bit more clean api
-rw-r--r-- | include/libgha.h | 37 | ||||
-rw-r--r-- | src/gha.c | 18 | ||||
-rw-r--r-- | test/dtmf.c | 10 | ||||
-rw-r--r-- | test/main.c | 6 |
4 files changed, 42 insertions, 29 deletions
diff --git a/include/libgha.h b/include/libgha.h index 6ebb33a..31639ac 100644 --- a/include/libgha.h +++ b/include/libgha.h @@ -5,23 +5,36 @@ #include <stddef.h> -typedef struct gha_ctx gha_ctx; +typedef struct gha_ctx *gha_ctx_t; struct gha_info { - FLOAT freq; + FLOAT frequency; FLOAT phase; FLOAT magnitude; }; -// size must be even -gha_ctx* gha_create_ctx(size_t size); -void gha_free_ctx(gha_ctx* ctx); - -// This function performs one GHA step for given PCM signal, -// the result will be writen in to given gha_ingo structure -void gha_analyze_one(const FLOAT* pcm, struct gha_info* info, gha_ctx* ctx); - -// Performs one GHA step and extract analysed harmonic from given PCM signal -void gha_extract_one(FLOAT* pcm, struct gha_info* info, gha_ctx* ctx); +/* + * Create context to perform GHA, size is number of samples provided to analyze. + * Size must be even + * + * Returns null in case of fail. + */ +gha_ctx_t gha_create_ctx(size_t size); + +/* + * Free GHA context + */ +void gha_free_ctx(gha_ctx_t ctx); + +/* + * Performs one GHA step for given PCM signal, + * the result will be writen in to given gha_ingo structure + */ +void gha_analyze_one(const FLOAT* pcm, struct gha_info* info, gha_ctx_t ctx); + +/* + * Performs one GHA step and extract analysed harmonic from given PCM signal + */ +void gha_extract_one(FLOAT* pcm, struct gha_info* info, gha_ctx_t ctx); #endif @@ -17,7 +17,7 @@ struct gha_ctx { FLOAT* tmp_buf; }; -static void gha_init_window(gha_ctx* ctx) +static void gha_init_window(gha_ctx_t ctx) { size_t i; size_t n = ctx->size + 1; @@ -26,9 +26,9 @@ static void gha_init_window(gha_ctx* ctx) } } -gha_ctx* gha_create_ctx(size_t size) +gha_ctx_t gha_create_ctx(size_t size) { - gha_ctx* ctx = malloc(sizeof(struct gha_ctx)); + gha_ctx_t ctx = malloc(sizeof(struct gha_ctx)); if (!ctx) return NULL; @@ -70,7 +70,7 @@ exit_free_gha_ctx: return NULL; } -void gha_free_ctx(gha_ctx* ctx) +void gha_free_ctx(gha_ctx_t ctx) { free(ctx->fft_out); free(ctx->tmp_buf); @@ -80,7 +80,7 @@ void gha_free_ctx(gha_ctx* ctx) free(ctx); } -static size_t gha_estimate_bin(gha_ctx* ctx) +static size_t gha_estimate_bin(gha_ctx_t ctx) { size_t i, end; size_t j = 0; @@ -156,7 +156,7 @@ static void gha_search_omega_newton(const FLOAT* pcm, size_t bin, size_t size, s // Last iteration if (loop == MAX_LOOPS) { - result->freq = omega_rad; + result->frequency = omega_rad; //assume zero-phase sine result->phase = M_PI / 2 - atan(Xi / Xr); if (Xr < 0) @@ -186,7 +186,7 @@ static void gha_estimate_magnitude(const FLOAT* pcm, const FLOAT* regen, size_t result->magnitude = t1 / t2; } -void gha_analyze_one(const FLOAT* pcm, struct gha_info* info, gha_ctx* ctx) +void gha_analyze_one(const FLOAT* pcm, struct gha_info* info, gha_ctx_t ctx) { int i = 0; int bin = 0; @@ -199,11 +199,11 @@ void gha_analyze_one(const FLOAT* pcm, struct gha_info* info, gha_ctx* ctx) bin = gha_estimate_bin(ctx); gha_search_omega_newton(ctx->tmp_buf, bin, ctx->size, info); - gha_generate_sine(ctx->tmp_buf, ctx->size, info->freq, info->phase); + gha_generate_sine(ctx->tmp_buf, ctx->size, info->frequency, info->phase); gha_estimate_magnitude(pcm, ctx->tmp_buf, ctx->size, info); } -void gha_extract_one(FLOAT* pcm, struct gha_info* info, gha_ctx* ctx) +void gha_extract_one(FLOAT* pcm, struct gha_info* info, gha_ctx_t ctx) { int i; FLOAT magnitude; diff --git a/test/dtmf.c b/test/dtmf.c index c98164b..54fcb6d 100644 --- a/test/dtmf.c +++ b/test/dtmf.c @@ -20,7 +20,7 @@ int main(int argc, char** argv) { long long len = atoll(argv[3]); - gha_ctx* ctx; + gha_ctx_t ctx; float* buf = malloc(len * sizeof(float)); if (!buf) @@ -44,7 +44,7 @@ int main(int argc, char** argv) { gha_extract_one(buf, &res[0], ctx); gha_extract_one(buf, &res[1], ctx); - if (res[0].freq > res[1].freq) { + if (res[0].frequency > res[1].frequency) { struct gha_info tmp; memcpy(&tmp, &res[0], sizeof(struct gha_info)); memcpy(&res[0], &res[1], sizeof(struct gha_info)); @@ -61,14 +61,14 @@ int main(int argc, char** argv) { magn[0] = atof(argv[5]); freq[1] = atof(argv[6]); magn[1] = atof(argv[7]); - if (fabs(freq[0] - res[0].freq) > 0.001 || fabs(magn[0] - res[0].magnitude) > 0.001 || - fabs(freq[1] - res[1].freq) > 0.001 || fabs(magn[1] - res[1].magnitude) > 0.001) + if (fabs(freq[0] - res[0].frequency) > 0.001 || fabs(magn[0] - res[0].magnitude) > 0.001 || + fabs(freq[1] - res[1].frequency) > 0.001 || fabs(magn[1] - res[1].magnitude) > 0.001) return 1; return 0; } else { fprintf(stderr, "dtmf result: low freq: %f, magn: %f, high freq: %f, magn: %f\n", - res[0].freq, res[0].magnitude, res[1].freq, res[1].magnitude); + res[0].frequency, res[0].magnitude, res[1].frequency, res[1].magnitude); } } diff --git a/test/main.c b/test/main.c index 703cbf5..6d730f2 100644 --- a/test/main.c +++ b/test/main.c @@ -22,7 +22,7 @@ int main(int argc, char** argv) { long long len = atoll(argv[3]); - gha_ctx* ctx; + gha_ctx_t ctx; float* buf = malloc(len * sizeof(float)); if (!buf) @@ -51,11 +51,11 @@ int main(int argc, char** argv) { double freq = atof(argv[4]); double phase = atof(argv[5]); double magn = atof(argv[6]); - if (fabs(freq - res.freq) > 0.001 || compare_phase(phase, res.phase, 0.001) || fabs(magn - res.magnitude) > 0.001) + if (fabs(freq - res.frequency) > 0.001 || compare_phase(phase, res.phase, 0.001) || fabs(magn - res.magnitude) > 0.001) return 1; return 0; } else { - fprintf(stderr, "Result: freq: %f, phase: %f, magn: %f\n", res.freq, res.phase, res.magnitude); + fprintf(stderr, "Result: freq: %f, phase: %f, magn: %f\n", res.frequency, res.phase, res.magnitude); } return 0; |