Edinburgh Speech Tools  2.1-release
EST_wave_temp.cc
Go to the documentation of this file.
1  /************************************************************************/
2  /* */
3  /* Centre for Speech Technology Research */
4  /* University of Edinburgh, UK */
5  /* Copyright (c) 1996,1997 */
6  /* All Rights Reserved. */
7  /* */
8  /* Permission is hereby granted, free of charge, to use and distribute */
9  /* this software and its documentation without restriction, including */
10  /* without limitation the rights to use, copy, modify, merge, publish, */
11  /* distribute, sublicense, and/or sell copies of this work, and to */
12  /* permit persons to whom this work is furnished to do so, subject to */
13  /* the following conditions: */
14  /* 1. The code must retain the above copyright notice, this list of */
15  /* conditions and the following disclaimer. */
16  /* 2. Any modifications must be clearly marked as such. */
17  /* 3. Original authors' names are not deleted. */
18  /* 4. The authors' names are not used to endorse or promote products */
19  /* derived from this software without specific prior written */
20  /* permission. */
21  /* */
22  /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23  /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24  /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25  /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26  /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27  /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28  /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29  /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30  /* THIS SOFTWARE. */
31  /* */
32  /************************************************************************/
33  /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
34  /* Date: Tue May 27 1997 */
35  /************************************************************************/
36 
37  /************************************************************************/
38  /* */
39  /* temporary place fro some new functions. */
40  /* */
41  /************************************************************************/
42 
43 #include <cmath>
44 #include <cstdlib>
45 #include "EST_Wave.h"
46 #include "EST_wave_aux.h"
47 #include "EST_simplestats.h"
48 #include "EST_cutils.h"
49 
50 using namespace std;
51 
53 {
54  int i, j;
55 
56  int size = Lof(a.num_samples(), b.num_samples());
57  EST_Wave diff = a;
58 
59  // ERROR REORG - this needs to return a proper error
60  if (a.num_channels() != b.num_channels())
61  {
62  cerr << "Error: Can't compare " << a.num_channels() <<
63  " channel EST_Wave with " << b.num_channels() << " channel EST_Wave\n";
64  return diff;
65  }
66 
67  for (i = 0; i < size; ++i)
68  for (j = 0; j < a.num_channels(); ++j)
69  diff.a(i, j) = a.a(i, j) - b.a(i, j);
70 
71  return diff;
72 }
73 
74 void meansd(EST_Wave &tr, float &mean, float &sd, int channel)
75 {
76  float var=0.0;
77  int i, n;
78 
79  for (n = 0, i = 0, mean = 0.0; i < tr.num_samples(); ++i)
80  {
81  mean += tr.a(i, channel);
82  ++n;
83  }
84  if (n == 0) {
85  mean = NAN;
86  sd = NAN;
87  return;
88  }
89  mean /= n;
90 
91  for (i = 0, mean = 0.0; i < tr.num_samples(); ++i)
92  var += pow(tr.a(i, channel) - mean, float(2.0));
93 
94  var /= n;
95  sd = sqrt(var);
96 }
97 
98 float rms_error(EST_Wave &a, EST_Wave &b, int channel)
99 {
100  int i;
101  int size = Lof(a.num_samples(), b.num_samples());
102  float sum = 0;
103 
104  for (i = 0; i < size; ++i)
105  sum += pow(float(a.a(i, channel) - b.a(i, channel)), float(2.0));
106 
107  sum = sqrt(sum / size);
108  return sum;
109 }
110 
111 float abs_error(EST_Wave &a, EST_Wave &b, int channel)
112 {
113  int i;
114  int size = Lof(a.num_samples(), b.num_samples());
115  float sum = 0;
116  for (i = 0; i < size; ++i)
117  {
118  // cout << i << " " << a.a(i, channel) << " " << b.a(i, channel) << endl;
119  sum += fabs(float(a.a(i, channel) - b.a(i, channel)));
120  }
121  return sum / size;
122 }
123 
124 float correlation(EST_Wave &a, EST_Wave &b, int channel)
125 {
126  int i;
127  int size = Lof(a.num_samples(), b.num_samples());
128  float predict,real;
129  EST_SuffStats x,y,xx,yy,xy,se,e;
130  float cor,error;
131 
132  for (i = 0; i < size; ++i)
133  {
134  // cout << a.a(i, channel) << " " << b.a(i, channel) << endl;
135  predict = b.a(i, channel);
136  real = a.a(i, channel);
137  x += predict;
138  y += real;
139  error = predict-real;
140  se += error*error;
141  e += fabs(error);
142  xx += predict*predict;
143  yy += real*real;
144  xy += predict*real;
145  }
146 
147  cor = (xy.mean() - (x.mean()*y.mean()))/
148  (sqrt(xx.mean()-(x.mean()*x.mean())) *
149  sqrt(yy.mean()-(y.mean()*y.mean())));
150 
151  // cout << xy.mean() << " " << x.mean() << " " << y.mean() << " " << xx.mean() << " " << yy.mean() << endl;
152 
153  // cout << "RMSE " << sqrt(se.mean()) << " Correlation is " << cor << " Mean (abs) Error " << e.mean() << " (" << e.stddev() << ")" << endl;
154 
155  return cor;
156 }
157 
158 void absolute(EST_Wave &wave)
159 {
160  int i, j;
161  for (i = 0; i < wave.num_samples(); ++i)
162  for (j = 0; j < wave.num_channels(); ++j)
163  wave.a(i, j) = abs(wave.a(i, j));
164 }
165 
167 {
168  int i;
169  EST_FVector e;
170 
171  // ERROR REORG - this needs to return a proper error
172  if (a.num_channels() != b.num_channels())
173  {
174  cerr << "Error: Can't compare " << a.num_channels() <<
175  " channel EST_Wave with " << b.num_channels() << " channel EST_Wave\n";
176  return e;
177  }
178  e.resize(a.num_channels());
179  for (i = 0; i < a.num_channels(); ++i)
180  e[i] = rms_error(a, b, i);
181 
182  return e;
183 }
184 
186 {
187  int i;
188  EST_FVector e;
189 
190  // ERROR REORG - this needs to return a proper error
191  if (a.num_channels() != b.num_channels())
192  {
193  cerr << "Error: Can't compare " << a.num_channels() <<
194  " channel EST_Wave with " << b.num_channels() << " channel EST_Wave\n";
195  return e;
196  }
197  e.resize(a.num_channels());
198  for (i = 0; i < a.num_channels(); ++i)
199  e[i] = abs_error(a, b, i);
200 
201  return e;
202 }
203 
205 {
206  int i;
207  EST_FVector cor;
208 
209  // ERROR REORG - this needs to return a proper error
210  if (a.num_channels() != b.num_channels())
211  {
212  cerr << "Error: Can't compare " << a.num_channels() <<
213  " channel EST_Wave with " << b.num_channels() << " channel EST_Wave\n";
214  return cor;
215  }
216  cor.resize(a.num_channels());
217  for (i = 0; i < a.num_channels(); ++i)
218  cor[i] = correlation(a, b, i);
219 
220  return cor;
221 }
222 
223 EST_Wave error(EST_Wave &ref, EST_Wave &test, int relax)
224 {
225  int i, j, k, l;
226  EST_Wave diff;
227  diff = ref;
228  int t;
229 
230  // relaxation allows an error to be ignored near boundaries. The
231  // degree of relation specifies how many samples can be ignored.
232 
233  int *r = new int[relax*3];
234 
235  for (l = 0; l < ref.num_channels(); ++l)
236  for (i = 0; i < ref.num_samples(); ++i)
237  {
238  t = 0;
239  for (k = 0, j = Gof((i - relax), 0); j < i + relax + 1; ++j, ++k)
240  {
241  if (ref.a(i, l) > 0.5)
242  r[k] = ((j < test.num_samples()) && (test.a(j, l)> 0.6)) ?1
243  : 0;
244  else
245  r[k] = ((j < test.num_samples()) && (test.a(j, l)< 0.4)) ?1
246  : 0;
247 
248  t |= r[k];
249  }
250  diff.a(i, l) = t;
251  }
252 
253  delete [] r;
254  return diff;
255 }
256 
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
Definition: EST_FMatrix.h:119
double mean(void) const
mean of currently cummulated values
ssize_t num_samples() const
return the number of samples in the waveform
Definition: EST_Wave.h:143
short & a(ssize_t i, ssize_t channel=0)
Definition: EST_Wave.cc:128
#define Lof(a, b)
Definition: EST_cutils.h:97
void absolute(EST_Wave &wave)
float abs_error(EST_Wave &a, EST_Wave &b, int channel)
float mean(EST_FVector &m)
#define Gof(a, b)
Definition: EST_cutils.h:95
void meansd(EST_Wave &tr, float &mean, float &sd, int channel)
ssize_t num_channels() const
return the number of channels in the waveform
Definition: EST_Wave.h:145
float correlation(EST_Wave &a, EST_Wave &b, int channel)
EST_Wave error(EST_Wave &ref, EST_Wave &test, int relax)
float sum(const EST_FMatrix &a)
sum of elements
Definition: vec_mat_aux.cc:147
void resize(int n, int set=1)
resize vector
float rms_error(EST_Wave &a, EST_Wave &b, int channel)
EST_Wave difference(EST_Wave &a, EST_Wave &b)