diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2018-11-24 22:01:41 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2018-11-29 00:23:01 +0300 |
commit | 4275785ac40b12584277e2fb7b31ef6ec1fceed8 (patch) | |
tree | fe339da88ac04d28c377af4ccdb6d91e4e45dfb2 /test | |
parent | 5b176978c464b4d60ccd8b4d576eb31e3039f9a8 (diff) | |
download | libgha-4275785ac40b12584277e2fb7b31ef6ec1fceed8.tar.gz |
Initiall implementation of multi dimentional Newton optimization
Diffstat (limited to 'test')
-rw-r--r-- | test/dtmf.c | 31 | ||||
-rw-r--r-- | test/ut.c | 45 |
2 files changed, 74 insertions, 2 deletions
diff --git a/test/dtmf.c b/test/dtmf.c index a2146be..ae73a6b 100644 --- a/test/dtmf.c +++ b/test/dtmf.c @@ -12,6 +12,17 @@ void usage(const char* selfname) { fprintf(stderr, "EXPECTED_MAGNITUDE_(LOW|HIGH) - expected magnitude (0 - 1)\n"); } +static void calc_resuidal(float* resuidal, size_t size, void* user_ctx) +{ + int i; + double s = 0.0; + float* result = (float*)user_ctx; + for (i = 0; i < size; i++) { + s += resuidal[i] * resuidal[i]; + } + *result = sqrt(s / size); +} + int main(int argc, char** argv) { if (argc != 4 && argc != 8) { usage(argv[0]); @@ -23,7 +34,8 @@ int main(int argc, char** argv) { gha_ctx_t ctx; float* buf = malloc(len * sizeof(float)); - if (!buf) + float* buf2 = malloc(len * sizeof(float)); + if (!buf || !buf2) abort(); if (load_file(argv[1], len, atoi(argv[2]), 8, buf)) { @@ -32,8 +44,12 @@ int main(int argc, char** argv) { return 1; } - + //Make copy of data to adjust extracted params + memcpy(buf2, buf, sizeof(float) * len); ctx = gha_create_ctx(len); + + float resuidal; + gha_set_user_resuidal_cb(&calc_resuidal, &resuidal, ctx); if (!ctx) { fprintf(stderr, "Unable to create gha ctx\n"); free(buf); @@ -43,6 +59,8 @@ int main(int argc, char** argv) { struct gha_info res[2]; gha_extract_many_simple(buf, &res[0], 2, ctx); + float resuidal_1 = resuidal; + if (res[0].frequency > res[1].frequency) { struct gha_info tmp; memcpy(&tmp, &res[0], sizeof(struct gha_info)); @@ -50,6 +68,14 @@ int main(int argc, char** argv) { memcpy(&res[1], &tmp, sizeof(struct gha_info)); } + gha_adjust_info(buf2, res, 2, ctx); + + if (resuidal > resuidal_1) { + fprintf(stderr, "gha_adjust_info wrong result\n"); + return 1; + } + + gha_free_ctx(ctx); free(buf); @@ -69,5 +95,6 @@ int main(int argc, char** argv) { } else { fprintf(stderr, "dtmf result: low freq: %f, magn: %f, high freq: %f, magn: %f\n", res[0].frequency, res[0].magnitude, res[1].frequency, res[1].magnitude); + return 0; } } diff --git a/test/ut.c b/test/ut.c new file mode 100644 index 0000000..89a58ea --- /dev/null +++ b/test/ut.c @@ -0,0 +1,45 @@ +#include <3rd/fctx/fct.h> + +#include <sle.h> + +static double eq_matrix_1[3][4] = + {{ 2, 1, -1, 8}, + {-3, -1, 2, -11}, + {-2, 1, 2, -3}}; +static double eq_result_1[3] = + {2.0, 3.0, -1.0}; + +static double eq_matrix_2[2][3] = + {{ 2, 1, 1}, + {4, 2, 2}}; + + +FCT_BGN() +{ + FCT_SUITE_BGN(simple) + { + FCT_TEST_BGN(sle_ok_1) + { + double* result = calloc(3, sizeof(double));; + int i, rv; + rv = sle_solve(eq_matrix_1[0], 3, result); + fct_chk_eq_int(rv, 0); + for (i = 0; i < 3; i++) + fct_chk_eq_dbl(result[i], eq_result_1[i]); + free(result); + } + FCT_TEST_END(); + + FCT_TEST_BGN(sle_not_ok_2) + { + double* result = calloc(2, sizeof(double));; + int rv; + rv = sle_solve(eq_matrix_2[0], 2, result); + fct_chk_eq_int(rv, -1); + free(result); + } + FCT_TEST_END(); + } + FCT_SUITE_END(); +} +FCT_END(); |