/*--------------------------------------------------------------------------*\
FILE........: vqtrainsp.c
AUTHOR......: David Rowe
DATE CREATED: 7 August 2012
This program trains sparse amplitude vector quantisers.
Modified from vqtrainph.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 */
/*-----------------------------------------------------------------------*\
FUNCTION PROTOTYPES
\*-----------------------------------------------------------------------*/
void zero(float v[], int d);
void acc(float v1[], float v2[], int d);
void norm(float v[], int k, int n[]);
int quantise(float cb[], float vec[], int d, int e, float *se);
void print_vec(float cb[], int d, int e);
void split(float cb[], int d, int b);
int gain_shape_quantise(float cb[], float vec[], int d, int e, float *se, float *best_gain);
/*-----------------------------------------------------------------------* \
MAIN
\*-----------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
int d,e; /* dimension and codebook size */
float *vec; /* current vector */
float *cb; /* vector codebook */
float *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 sd;
int var_n, bits, b, levels;
/* Interpret command line arguments */
if (argc < 5) {
printf("usage: %s TrainFile D(dimension) B(number of bits) VQFile [error.txt file]\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]);
bits = atoi(argv[3]);
e = 1< 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