From ac7c48b4dee99d4c772f133d70d8d1b38262fcd2 Mon Sep 17 00:00:00 2001 From: Author Name Date: Fri, 7 Jul 2023 12:20:59 +0930 Subject: shallow zip-file copy from codec2 e9d726bf20 --- misc/vq_binary_switch.c | 296 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 misc/vq_binary_switch.c (limited to 'misc/vq_binary_switch.c') diff --git a/misc/vq_binary_switch.c b/misc/vq_binary_switch.c new file mode 100644 index 0000000..4270c3d --- /dev/null +++ b/misc/vq_binary_switch.c @@ -0,0 +1,296 @@ +/* + vq_binary_switch.c + David Rowe Dec 2021 + + C implementation of [1], that re-arranges VQ indexes so they are robust to single + bit errors. + + [1] Pseudo Gray Coding, Zeger & Gersho 1990 +*/ + +#include +#include +#include +#include +#include +#include +#include +#include "mbest.h" + +#define MAX_DIM 20 +#define MAX_ENTRIES 4096 + +// equation (33) of [1], total cost of all hamming distance 1 vectors of vq index k +float cost_of_distance_one(float *vq, int n, int dim, float *prob, int k, int st, int en, int verbose) { + int log2N = log2(n); + float c = 0.0; + for (int b=0; b best_delta) { + best_delta = fabs(delta); + best_j = j; + } + } + // unswitch + swap(vq, dim, prob, A[i], j); + } + } //next j + + // printf("best_delta: %f best_j: %d\n", best_delta, best_j); + if (best_delta == 0.0) { + // Hmm, no improvement, lets try the next vector in the sorted cost list + if (i == n-1) finished = 1; else i++; + } else { + // OK keep the switch that minimised the distortion + swap(vq, dim, prob, A[i], best_j); + switches++; + + // save results + FILE *fq=fopen(argv[dx+1], "wb"); + if (fq == NULL) { + fprintf(stderr, "Couldn't open: %s\n", argv[dx+1]); + exit(1); + } + int nwr = fwrite(vq, sizeof(float), n*dim, fq); + assert(nwr == n*dim); + fclose(fq); + + // set up for next iteration + iteration++; + float distortion = distortion_of_current_mapping(vq, n, dim, prob, st, en); + fprintf(stderr, "it: %3d dist: %f %3.2f i: %3d sw: %3d\n", iteration, distortion, + distortion/distortion0, i, switches); + if (iteration >= max_iter) finished = 1; + i = 0; + } + } + + return 0; +} + -- cgit v1.2.3