File: vqtrain.cpp

package info (click to toggle)
libitpp 4.3.1-13
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 9,952 kB
  • sloc: cpp: 73,628; makefile: 661; python: 548; sh: 261
file content (296 lines) | stat: -rw-r--r-- 7,173 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*!
 * \file
 * \brief Implementation of a vector quantizer training functions
 * \author Thomas Eriksson
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2010  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ 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 General Public License along
 * with IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/srccode/vqtrain.h>
#include <itpp/base/matfunc.h>
#include <itpp/base/random.h>
#include <itpp/base/sort.h>
#include <itpp/base/specmat.h>
#include <itpp/base/math/min_max.h>
#include <itpp/stat/misc_stat.h>
#include <iostream>

//! \cond

namespace itpp
{

// the cols contains codevectors
double kmeansiter(Array<vec> &DB, mat &codebook)
{
  int    DIM = DB(0).length(), SIZE = codebook.cols(), T = DB.length();
  vec    x, xnum(SIZE);
  mat    xsum(DIM, SIZE);
  int    MinIndex, i, j, k;
  double   MinS, S, D, *xp, *cp;

  xsum.clear();
  xnum.clear();

  D = 1E20;
  D = 0;
  for (k = 0;k < T;k++) {
    x = DB(k);
    xp = x._data();
    MinS = 1E20;
    MinIndex = 0;
    for (i = 0;i < SIZE;i++) {
      cp = &codebook(0, i);
      S = sqr(xp[0] - cp[0]);
      for (j = 1;j < DIM;j++) {
        S += sqr(xp[j] - cp[j]);
        if (S >= MinS) goto sune;
      }
      MinS = S;
      MinIndex = i;
    sune:
    void();
    }
    D += MinS;
    cp = &xsum(0, MinIndex);
    for (j = 0;j < DIM;j++) {
      cp[j] += xp[j];
    }
    xnum(MinIndex)++;
  }
  for (i = 0;i < SIZE;i++) {
    for (j = 0;j < DIM;j++) {
      codebook(j, i) = xsum(j, i) / xnum(i);
    }
  }
  return D;
}

mat kmeans(Array<vec> &DB, int SIZE, int NOITER, bool VERBOSE)
{
  int    DIM = DB(0).length(), T = DB.length();
  mat    codebook(DIM, SIZE);
  int    n, i, j;
  double   D, Dold;
  ivec   ind(SIZE);

  for (i = 0;i < SIZE;i++) {
    ind(i) = randi(0, T - 1);
    j = 0;
    while (j < i) {
      if (ind(j) == ind(i)) {
        ind(i) = randi(0, T - 1);
        j = 0;
      }
      j++;
    }
    codebook.set_col(i, DB(ind(i)));
  }


  if (VERBOSE) std::cout << "Training VQ..." << std::endl ;

  D = 1E20;
  for (n = 0;n < NOITER;n++) {
    Dold = D;
    D = kmeansiter(DB, codebook);
    if (VERBOSE) std::cout << n << ": " << D / T << " ";
    if (std::abs((D - Dold) / D) < 1e-4) break;
  }
  return codebook;
}

mat lbg(Array<vec> &DB, int SIZE, int NOITER, bool VERBOSE)
{
  int  S = 1, DIM = DB(0).length(), T = DB.length(), i, n;
  mat  cb;
  vec  delta = 0.001 * randn(DIM), x;
  double D, Dold;

  x = zeros(DIM);
  for (i = 0;i < T;i++) {
    x += DB(i);
  }
  x /= T;
  cb.set_size(DIM, 1);
  cb.set_col(0, x);
  while (cb.cols() < SIZE) {
    S = cb.cols();
    if (2*S <= SIZE) cb.set_size(DIM, 2*S, true);
    else cb.set_size(DIM, 2*S, true);
    for (i = S;i < cb.cols();i++) {
      cb.set_col(i, cb.get_col(i - S) + delta);
    }
    D = 1E20;
    for (n = 0;n < NOITER;n++) {
      Dold = D;
      D = kmeansiter(DB, cb);
      if (VERBOSE) std::cout << n << ": " << D / T << " ";
      if (std::abs((D - Dold) / D) < 1e-4) break;
    }
  }

  return cb;
}

mat vqtrain(Array<vec> &DB, int SIZE, int NOITER, double STARTSTEP, bool VERBOSE)
{
  int    DIM = DB(0).length();
  vec    x;
  vec    codebook(DIM*SIZE);
  int    n, MinIndex, i, j;
  double   MinS, S, D, step, *xp, *cp;

  for (i = 0;i < SIZE;i++) {
    codebook.replace_mid(i*DIM, DB(randi(0, DB.length() - 1)));
  }
  if (VERBOSE) std::cout << "Training VQ..." << std::endl ;

res:
  D = 0;
  for (n = 0;n < NOITER;n++) {
    step = STARTSTEP * (1.0 - double(n) / NOITER);
    if (step < 0) step = 0;
    x = DB(randi(0, DB.length() - 1)); // seems unnecessary! Check it up.
    xp = x._data();

    MinS = 1E20;
    MinIndex = 0;
    for (i = 0;i < SIZE;i++) {
      cp = &codebook(i * DIM);
      S = sqr(xp[0] - cp[0]);
      for (j = 1;j < DIM;j++) {
        S += sqr(xp[j] - cp[j]);
        if (S >= MinS) goto sune;
      }
      MinS = S;
      MinIndex = i;
    sune:
      i = i;
    }
    D += MinS;
    cp = &codebook(MinIndex * DIM);
    for (j = 0;j < DIM;j++) {
      cp[j] += step * (xp[j] - cp[j]);
    }
    if ((n % 20000 == 0) && (n > 1)) {
      if (VERBOSE) std::cout << n << ": " << D / 20000 << " ";
      D = 0;
    }
  }

  // checking training result
  vec dist(SIZE), num(SIZE);

  dist.clear();
  num.clear();
  for (n = 0;n < DB.length();n++) {
    x = DB(n);
    xp = x._data();
    MinS = 1E20;
    MinIndex = 0;
    for (i = 0;i < SIZE;i++) {
      cp = &codebook(i * DIM);
      S = sqr(xp[0] - cp[0]);
      for (j = 1;j < DIM;j++) {
        S += sqr(xp[j] - cp[j]);
        if (S >= MinS) goto sune2;
      }
      MinS = S;
      MinIndex = i;
    sune2:
      i = i;
    }
    dist(MinIndex) += MinS;
    num(MinIndex) += 1;
  }
  dist = 10 * log10(dist * dist.length() / sum(dist));
  if (VERBOSE) std::cout << std::endl << "Distortion contribution: " << dist << std::endl ;
  if (VERBOSE) std::cout << "Num spread: " << num / DB.length()*100 << " %" << std::endl << std::endl ;
  if (min(dist) < -30) {
    std::cout << "Points without entries! Retraining" << std::endl ;
    j = min_index(dist);
    i = max_index(num);
    codebook.replace_mid(j*DIM, codebook.mid(i*DIM, DIM));
    goto res;
  }

  mat cb(DIM, SIZE);
  for (i = 0;i < SIZE;i++) {
    cb.set_col(i, codebook.mid(i*DIM, DIM));
  }
  return cb;
}

vec sqtrain(const vec &inDB, int SIZE)
{
  vec  DB(inDB);
  vec  Levels, Levels_old;
  ivec indexlist(SIZE + 1);
  int  il, im, ih, i;
  int  SIZEDB = inDB.length();
  double x;

  sort(DB);
  Levels = DB(round_i(linspace(0.01 * SIZEDB, 0.99 * SIZEDB, SIZE)));
  Levels_old = zeros(SIZE);

  while (energy(Levels - Levels_old) > 0.0001) {
    Levels_old = Levels;
    for (i = 0;i < SIZE - 1;i++) {
      x = (Levels(i) + Levels(i + 1)) / 2;
      il = 0;
      ih = SIZEDB - 1;
      while (il < ih - 1) {
        im = (il + ih) / 2;
        if (x < DB(im)) ih = im;
        else il = im;
      }
      indexlist(i + 1) = il;
    }
    indexlist(0) = -1;
    indexlist(SIZE) = SIZEDB - 1;
    for (i = 0;i < SIZE;i++) Levels(i) = mean(DB(indexlist(i) + 1, indexlist(i + 1)));
  }
  return Levels;
}

ivec bitalloc(const vec &variances, int nobits)
{
  ivec bitvec(variances.length());
  bitvec.clear();
  int  i, bits = nobits;
  vec  var = variances;

  while (bits > 0) {
    i = max_index(var);
    var(i) /= 4;
    bitvec(i)++;
    bits--;
  }
  return bitvec;
}

} // namespace itpp

//! \endcond