Edinburgh Speech Tools  2.4-release
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
EST_wave_temp.cc
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 EST_Wave difference(EST_Wave &a, EST_Wave &b)
51 {
52  int i, j;
53 
54  int size = Lof(a.num_samples(), b.num_samples());
55  EST_Wave diff = a;
56 
57  // ERROR REORG - this needs to return a proper error
58  if (a.num_channels() != b.num_channels())
59  {
60  cerr << "Error: Can't compare " << a.num_channels() <<
61  " channel EST_Wave with " << b.num_channels() << " channel EST_Wave\n";
62  return diff;
63  }
64 
65  for (i = 0; i < size; ++i)
66  for (j = 0; j < a.num_channels(); ++j)
67  diff.a(i, j) = a.a(i, j) - b.a(i, j);
68 
69  return diff;
70 }
71 
72 void meansd(EST_Wave &tr, float &mean, float &sd, int channel)
73 {
74  float var=0.0;
75  int i, n;
76 
77  for (n = 0, i = 0, mean = 0.0; i < tr.num_samples(); ++i)
78  {
79  mean += tr.a(i, channel);
80  ++n;
81  }
82 
83  mean /= n;
84 
85  for (i = 0, mean = 0.0; i < tr.num_samples(); ++i)
86  var += pow(tr.a(i, channel) - mean, float(2.0));
87 
88  var /= n;
89  sd = sqrt(var);
90 }
91 
92 float rms_error(EST_Wave &a, EST_Wave &b, int channel)
93 {
94  int i;
95  int size = Lof(a.num_samples(), b.num_samples());
96  float sum = 0;
97 
98  for (i = 0; i < size; ++i)
99  sum += pow(float(a.a(i, channel) - b.a(i, channel)), float(2.0));
100 
101  sum = sqrt(sum / size);
102  return sum;
103 }
104 
105 float abs_error(EST_Wave &a, EST_Wave &b, int channel)
106 {
107  int i;
108  int size = Lof(a.num_samples(), b.num_samples());
109  float sum = 0;
110  for (i = 0; i < size; ++i)
111  {
112  // cout << i << " " << a.a(i, channel) << " " << b.a(i, channel) << endl;
113  sum += fabs(float(a.a(i, channel) - b.a(i, channel)));
114  }
115  return sum / size;
116 }
117 
118 float correlation(EST_Wave &a, EST_Wave &b, int channel)
119 {
120  int i;
121  int size = Lof(a.num_samples(), b.num_samples());
122  float predict,real;
123  EST_SuffStats x,y,xx,yy,xy,se,e;
124  float cor,error;
125 
126  for (i = 0; i < size; ++i)
127  {
128  // cout << a.a(i, channel) << " " << b.a(i, channel) << endl;
129  predict = b.a(i, channel);
130  real = a.a(i, channel);
131  x += predict;
132  y += real;
133  error = predict-real;
134  se += error*error;
135  e += fabs(error);
136  xx += predict*predict;
137  yy += real*real;
138  xy += predict*real;
139  }
140 
141  cor = (xy.mean() - (x.mean()*y.mean()))/
142  (sqrt(xx.mean()-(x.mean()*x.mean())) *
143  sqrt(yy.mean()-(y.mean()*y.mean())));
144 
145  // cout << xy.mean() << " " << x.mean() << " " << y.mean() << " " << xx.mean() << " " << yy.mean() << endl;
146 
147  // cout << "RMSE " << sqrt(se.mean()) << " Correlation is " << cor << " Mean (abs) Error " << e.mean() << " (" << e.stddev() << ")" << endl;
148 
149  return cor;
150 }
151 
152 void absolute(EST_Wave &wave)
153 {
154  int i, j;
155  for (i = 0; i < wave.num_samples(); ++i)
156  for (j = 0; j < wave.num_channels(); ++j)
157  wave.a(i, j) = abs(wave.a(i, j));
158 }
159 
160 EST_FVector rms_error(EST_Wave &a, EST_Wave &b)
161 {
162  int i;
163  EST_FVector e;
164 
165  // ERROR REORG - this needs to return a proper error
166  if (a.num_channels() != b.num_channels())
167  {
168  cerr << "Error: Can't compare " << a.num_channels() <<
169  " channel EST_Wave with " << b.num_channels() << " channel EST_Wave\n";
170  return e;
171  }
172  e.resize(a.num_channels());
173  for (i = 0; i < a.num_channels(); ++i)
174  e[i] = rms_error(a, b, i);
175 
176  return e;
177 }
178 
179 EST_FVector abs_error(EST_Wave &a, EST_Wave &b)
180 {
181  int i;
182  EST_FVector e;
183 
184  // ERROR REORG - this needs to return a proper error
185  if (a.num_channels() != b.num_channels())
186  {
187  cerr << "Error: Can't compare " << a.num_channels() <<
188  " channel EST_Wave with " << b.num_channels() << " channel EST_Wave\n";
189  return e;
190  }
191  e.resize(a.num_channels());
192  for (i = 0; i < a.num_channels(); ++i)
193  e[i] = abs_error(a, b, i);
194 
195  return e;
196 }
197 
198 EST_FVector correlation(EST_Wave &a, EST_Wave &b)
199 {
200  int i;
201  EST_FVector cor;
202 
203  // ERROR REORG - this needs to return a proper error
204  if (a.num_channels() != b.num_channels())
205  {
206  cerr << "Error: Can't compare " << a.num_channels() <<
207  " channel EST_Wave with " << b.num_channels() << " channel EST_Wave\n";
208  return cor;
209  }
210  cor.resize(a.num_channels());
211  for (i = 0; i < a.num_channels(); ++i)
212  cor[i] = correlation(a, b, i);
213 
214  return cor;
215 }
216 
217 EST_Wave error(EST_Wave &ref, EST_Wave &test, int relax)
218 {
219  int i, j, k, l;
220  EST_Wave diff;
221  diff = ref;
222  int t;
223 
224  // relaxation allows an error to be ignored near boundaries. The
225  // degree of relation specifies how many samples can be ignored.
226 
227  int *r = new int[relax*3];
228 
229  for (l = 0; l < ref.num_channels(); ++l)
230  for (i = 0; i < ref.num_samples(); ++i)
231  {
232  t = 0;
233  for (k = 0, j = Gof((i - relax), 0); j < i + relax + 1; ++j, ++k)
234  {
235  if (ref.a(i, l) > 0.5)
236  r[k] = ((j < test.num_samples()) && (test.a(j, l)> 0.6)) ?1
237  : 0;
238  else
239  r[k] = ((j < test.num_samples()) && (test.a(j, l)< 0.4)) ?1
240  : 0;
241 
242  t |= r[k];
243  }
244  diff.a(i, l) = t;
245  }
246 
247  delete [] r;
248  return diff;
249 }
250