/* 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; }