2
0

vqtrain.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*--------------------------------------------------------------------------*\
  2. FILE........: VQTRAIN.C
  3. AUTHOR......: David Rowe
  4. DATE CREATED: 23/2/95
  5. This program trains vector quantisers using K dimensional Lloyd-Max
  6. method.
  7. \*--------------------------------------------------------------------------*/
  8. /*
  9. Copyright (C) 2009 David Rowe
  10. All rights reserved.
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU Lesser General Public License version 2, as
  13. published by the Free Software Foundation. This program is
  14. distributed in the hope that it will be useful, but WITHOUT ANY
  15. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  17. License for more details.
  18. You should have received a copy of the GNU Lesser General Public License
  19. along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /*-----------------------------------------------------------------------*\
  22. INCLUDES
  23. \*-----------------------------------------------------------------------*/
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #include <ctype.h>
  29. #include <assert.h>
  30. /*-----------------------------------------------------------------------*\
  31. DEFINES
  32. \*-----------------------------------------------------------------------*/
  33. #define DELTAQ 0.01 /* quiting distortion */
  34. #define MAX_STR 80 /* maximum string length */
  35. /*-----------------------------------------------------------------------*\
  36. FUNCTION PROTOTYPES
  37. \*-----------------------------------------------------------------------*/
  38. void zero(float v[], int k);
  39. void acc(float v1[], float v2[], int k);
  40. void norm(float v[], int k, long n);
  41. long quantise(float cb[], float vec[], int k, int m, float *se);
  42. /*-----------------------------------------------------------------------* \
  43. MAIN
  44. \*-----------------------------------------------------------------------*/
  45. int main(int argc, char *argv[]) {
  46. long k,m; /* dimension and codebook size */
  47. float *vec; /* current vector */
  48. float *cb; /* vector codebook */
  49. float *cent; /* centroids for each codebook entry */
  50. long *n; /* number of vectors in this interval */
  51. long J; /* number of vectors in training set */
  52. long i,j;
  53. long ind; /* index of current vector */
  54. float se; /* squared error for this iteration */
  55. float Dn,Dn_1; /* current and previous iterations distortion */
  56. float delta; /* improvement in distortion */
  57. FILE *ftrain; /* file containing training set */
  58. FILE *fvq; /* file containing vector quantiser */
  59. int ret;
  60. /* Interpret command line arguments */
  61. if (argc != 5) {
  62. printf("usage: %s TrainFile K(dimension) M(codebook size) VQFile\n", argv[0]);
  63. exit(1);
  64. }
  65. /* Open training file */
  66. ftrain = fopen(argv[1],"rb");
  67. if (ftrain == NULL) {
  68. printf("Error opening training database file: %s\n",argv[1]);
  69. exit(1);
  70. }
  71. /* determine k and m, and allocate arrays */
  72. k = atol(argv[2]);
  73. m = atol(argv[3]);
  74. printf("dimension K=%ld number of entries M=%ld\n", k, m);
  75. vec = (float*)malloc(sizeof(float)*k);
  76. cb = (float*)malloc(sizeof(float)*k*m);
  77. cent = (float*)malloc(sizeof(float)*k*m);
  78. n = (long*)malloc(sizeof(long)*m);
  79. if (cb == NULL || cb == NULL || cent == NULL || vec == NULL) {
  80. printf("Error in malloc.\n");
  81. exit(1);
  82. }
  83. /* determine size of training set */
  84. J = 0;
  85. while(fread(vec, sizeof(float), k, ftrain) == (size_t)k)
  86. J++;
  87. printf("J=%ld entries in training set\n", J);
  88. /* set up initial codebook state from samples of training set */
  89. rewind(ftrain);
  90. ret = fread(cb, sizeof(float), k*m, ftrain);
  91. /* main loop */
  92. Dn = 1E32;
  93. j = 1;
  94. do {
  95. Dn_1 = Dn;
  96. /* zero centroids */
  97. for(i=0; i<m; i++) {
  98. zero(&cent[i*k], k);
  99. n[i] = 0;
  100. }
  101. /* quantise training set */
  102. se = 0.0;
  103. rewind(ftrain);
  104. for(i=0; i<J; i++) {
  105. ret = fread(vec, sizeof(float), k, ftrain);
  106. ind = quantise(cb, vec, k, m, &se);
  107. n[ind]++;
  108. acc(&cent[ind*k], vec, k);
  109. }
  110. Dn = se/J;
  111. delta = (Dn_1-Dn)/Dn;
  112. printf("\r Iteration %ld, Dn = %f, Delta = %e\n", j, Dn, delta);
  113. j++;
  114. /* determine new codebook from centroids */
  115. if (delta > DELTAQ)
  116. for(i=0; i<m; i++) {
  117. if (n[i] != 0) {
  118. norm(&cent[i*k], k, n[i]);
  119. memcpy(&cb[i*k], &cent[i*k], k*sizeof(float));
  120. }
  121. }
  122. } while (delta > DELTAQ);
  123. /* save codebook to disk */
  124. fvq = fopen(argv[4],"wt");
  125. if (fvq == NULL) {
  126. printf("Error opening VQ file: %s\n",argv[4]);
  127. exit(1);
  128. }
  129. fprintf(fvq,"%ld %ld\n",k,m);
  130. for(j=0; j<m; j++) {
  131. for(i=0; i<k; i++)
  132. fprintf(fvq,"%f ",cb[j*k+i]);
  133. fprintf(fvq,"\n");
  134. }
  135. fclose(fvq);
  136. return 0;
  137. }
  138. /*-----------------------------------------------------------------------*\
  139. FUNCTIONS
  140. \*-----------------------------------------------------------------------*/
  141. /*---------------------------------------------------------------------------*\
  142. FUNCTION....: zero()
  143. AUTHOR......: David Rowe
  144. DATE CREATED: 23/2/95
  145. Zeros a vector of length k.
  146. \*---------------------------------------------------------------------------*/
  147. void zero(float v[], int k)
  148. /* float v[]; ptr to start of vector */
  149. /* int k; lngth of vector */
  150. {
  151. int i;
  152. for(i=0; i<k; i++)
  153. v[i] = 0.0;
  154. }
  155. /*---------------------------------------------------------------------------*\
  156. FUNCTION....: acc()
  157. AUTHOR......: David Rowe
  158. DATE CREATED: 23/2/95
  159. Adds k dimensional vectors v1 to v2 and stores the result back in v1.
  160. \*---------------------------------------------------------------------------*/
  161. void acc(float v1[], float v2[], int k)
  162. /* float v1[]; ptr to start of vector to accumulate */
  163. /* float v2[]; ptr to start of vector to add */
  164. /* int k; dimension of vectors */
  165. {
  166. int i;
  167. for(i=0; i<k; i++)
  168. v1[i] += v2[i];
  169. }
  170. /*---------------------------------------------------------------------------*\
  171. FUNCTION....: norm()
  172. AUTHOR......: David Rowe
  173. DATE CREATED: 23/2/95
  174. Divides each element in k dimensional vector v by n.
  175. \*---------------------------------------------------------------------------*/
  176. void norm(float v[], int k, long n)
  177. /* float v[]; ptr to start of vector */
  178. /* int k; dimension of vectors */
  179. /* int n; normalising factor */
  180. {
  181. int i;
  182. for(i=0; i<k; i++)
  183. v[i] /= n;
  184. }
  185. /*---------------------------------------------------------------------------*\
  186. FUNCTION....: quantise()
  187. AUTHOR......: David Rowe
  188. DATE CREATED: 23/2/95
  189. Quantises vec by choosing the nearest vector in codebook cb, and
  190. returns the vector index. The squared error of the quantised vector
  191. is added to se.
  192. \*---------------------------------------------------------------------------*/
  193. long quantise(float cb[], float vec[], int k, int m, float *se)
  194. /* float cb[][K]; current VQ codebook */
  195. /* float vec[]; vector to quantise */
  196. /* int k; dimension of vectors */
  197. /* int m; size of codebook */
  198. /* float *se; accumulated squared error */
  199. {
  200. float e; /* current error */
  201. long besti; /* best index so far */
  202. float beste; /* best error so far */
  203. long j;
  204. int i;
  205. float diff;
  206. besti = 0;
  207. beste = 1E32;
  208. for(j=0; j<m; j++) {
  209. e = 0.0;
  210. for(i=0; i<k; i++) {
  211. diff = cb[j*k+i]-vec[i];
  212. e += pow(diff,2.0);
  213. }
  214. if (e < beste) {
  215. beste = e;
  216. besti = j;
  217. }
  218. }
  219. *se += beste;
  220. return(besti);
  221. }