/*--------------------------------------------------------------------------*\ FILE........: vqtrainph.c AUTHOR......: David Rowe DATE CREATED: 27 July 2012 This program trains phase vector quantisers. Modified from vqtrain.c \*--------------------------------------------------------------------------*/ /* Copyright (C) 2012 David Rowe All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, see . */ /*-----------------------------------------------------------------------*\ INCLUDES \*-----------------------------------------------------------------------*/ #include #include #include #include #include #include typedef struct { float real; float imag; } COMP; /*-----------------------------------------------------------------------* \ DEFINES \*-----------------------------------------------------------------------*/ #define DELTAQ 0.01 /* quiting distortion */ #define MAX_STR 80 /* maximum string length */ #define PI 3.141592654 /*-----------------------------------------------------------------------*\ FUNCTION PROTOTYPES \*-----------------------------------------------------------------------*/ void zero(COMP v[], int d); void acc(COMP v1[], COMP v2[], int d); void norm(COMP v[], int k); int quantise(COMP cb[], COMP vec[], int d, int e, float *se); void print_vec(COMP cb[], int d, int e); /*-----------------------------------------------------------------------* \ MAIN \*-----------------------------------------------------------------------*/ int main(int argc, char *argv[]) { int d,e; /* dimension and codebook size */ COMP *vec; /* current vector */ COMP *cb; /* vector codebook */ COMP *cent; /* centroids for each codebook entry */ int *n; /* number of vectors in this interval */ int J; /* number of vectors in training set */ int ind; /* index of current vector */ float se; /* total squared error for this iteration */ float var; /* variance */ float var_1; /* previous variance */ float delta; /* improvement in distortion */ FILE *ftrain; /* file containing training set */ FILE *fvq; /* file containing vector quantiser */ int ret; int i,j, finished, iterations; float b; /* equivalent number of bits */ float improvement; float sd_vec, sd_element, sd_theory, bits_theory; int var_n; /* Interpret command line arguments */ if (argc != 5) { printf("usage: %s TrainFile D(dimension) E(number of entries) VQFile\n", argv[0]); exit(1); } /* Open training file */ ftrain = fopen(argv[1],"rb"); if (ftrain == NULL) { printf("Error opening training database file: %s\n",argv[1]); exit(1); } /* determine k and m, and allocate arrays */ d = atoi(argv[2]); e = atoi(argv[3]); printf("\n"); printf("dimension D=%d number of entries E=%d\n", d, e); vec = (COMP*)malloc(sizeof(COMP)*d); cb = (COMP*)malloc(sizeof(COMP)*d*e); cent = (COMP*)malloc(sizeof(COMP)*d*e); n = (int*)malloc(sizeof(int)*e); if (cb == NULL || cb == NULL || cent == NULL || vec == NULL) { printf("Error in malloc.\n"); exit(1); } /* determine size of training set */ J = 0; var_n = 0; while(fread(vec, sizeof(COMP), d, ftrain) == (size_t)d) { for(j=0; j 1) { if (var > 0.0) { delta = (var_1 - var)/var; } else delta = 0; if (delta < DELTAQ) finished = 1; } if (!finished) { /* determine new codebook from centroids */ for(i=0; i