Edinburgh Speech Tools  2.1-release
delta.cc
Go to the documentation of this file.
1 /*************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1995,1996 */
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 : Simon King */
34 /* Date : July 1995 */
35 /*-----------------------------------------------------------------------*/
36 /* Compute delta coefficients for */
37 /* Tracks and Tracks */
38 /* */
39 /*=======================================================================*/
40 #include <cstdlib>
41 #include "EST_Track.h"
42 #include "EST_Wave.h"
43 
44 using namespace std;
45 
46 # define MAX_DELTA_ORDER 2
47 /// max. number of points on which the delta co-eff is based
48 # define MAX_REGRESSION_LENGTH 4
49 
50 static float compute_gradient(const EST_FVector &x, int num_points);
51 
52 void delta(EST_Track &tr, EST_Track &d, int regression_length)
53 {
54  ssize_t reg_index, this_index;
55 
56  // need at least two points to compute gradient
57  if ((regression_length < 2)||(regression_length > MAX_REGRESSION_LENGTH)){
58  cerr << "delta(EST_Track&, int) : ERROR : regression_length is "
59  << regression_length << endl;
60  exit(0);
61  }
62 
63  // temp stores the points passed to compute_gradient
64  EST_FVector temp(regression_length);
65 
66  for (ssize_t j = 0; j < tr.num_channels(); j++ )
67  for (ssize_t i = 0; i < tr.num_frames(); i++)
68  {
69  // copy values needed to compute gradient into temp[]
70  for (reg_index=0; reg_index<regression_length; reg_index++)
71  {
72  // gradient is computed from points to left of current time
73  // rather than points centred around the current time
74  this_index = i - reg_index;
75  if (this_index >= 0)
76  temp[reg_index] = tr.a(this_index, j);
77  }
78 
79  // gradient at frame 0 is defined as 0
80  if (i < 1)
81  d.a(i, j) = 0.0;
82  else if (i < regression_length - 1)
83  // enough data, but would prefer more
84  // number of points available is only i+1
85  d.a(i, j) = compute_gradient(temp, i + 1);
86  else
87  // plenty of data, use the last regression_length points
88  d.a(i, j) = compute_gradient(temp, regression_length);
89  }
90 }
91 
92 void delta(EST_Wave &tr, EST_Wave &d, int regression_length)
93 {
94  int reg_index, this_index;
95 
96  // need at least two points to compute gradient
97  if ((regression_length < 2)||(regression_length > MAX_REGRESSION_LENGTH)){
98  cerr << "delta(EST_Track&, int) : ERROR : regression_length is "
99  << regression_length << endl;
100  exit(0);
101  }
102 
103  // temp stores the points passed to compute_gradient
104  EST_FVector temp(regression_length);
105 
106  for (int j = 0; j < tr.num_channels(); j++ )
107  for (int i = 0; i < tr.num_samples(); i++)
108  {
109  // copy values needed to compute gradient into temp[]
110  for (reg_index=0; reg_index<regression_length; reg_index++)
111  {
112  // gradient is computed from points to left of current time
113  // rather than points centred around the current time
114  this_index = i - reg_index;
115  if (this_index >= 0)
116  temp.a_no_check(reg_index) = (float)tr.a(this_index, j);
117  }
118 
119  // gradient at frame 0 is defined as 0
120  if (i < 1)
121  d.a(i, j) = 0;
122  else if (i < regression_length - 1)
123  // enough data, but would prefer more
124  // number of points available is only i+1
125  d.a(i, j) = (short)compute_gradient(temp, i + 1);
126  else
127  // plenty of data, use the last regression_length points
128  d.a(i, j) = (short)compute_gradient(temp, regression_length);
129  }
130 }
131 
132 static float compute_gradient(const EST_FVector &x, int num_points)
133 {
134  float gradient;
135 
136  // NB x[0] is the point LATEST in time
137  // so x[1] is really x[t-1]
138 
139  // time between points is assumed to be one unit
140 
141  // These are solutions to least-squares fit of straight line
142  // to num_points points.
143  switch (num_points){
144 
145  case 1:
146  gradient = 0.0;
147  break;
148 
149  case 2:
150  gradient = x(0) - x(1);
151  break;
152 
153  case 3:
154  gradient = (x(0) -x(2)) / 2.0;
155  break;
156 
157  case 4:
158  gradient = ( (3.0*x(0)) + x(1) - x(2) - (3.0 * x(3)) ) / 10.0;
159  break;
160 
161  default:
162 
163  cerr << "compute_gradient(float*, int) : ERROR : num_points is"
164  << num_points << endl;
165 
166  exit(0);
167  break;
168  }
169 
170  return gradient;
171 }
172 
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
void delta(EST_Track &tr, EST_Track &d, int regression_length)
Definition: delta.cc:52
#define MAX_REGRESSION_LENGTH
max. number of points on which the delta co-eff is based
Definition: delta.cc:48
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
Definition: EST_FMatrix.h:119
int num_channels() const
return number of channels in track
Definition: EST_Track.h:657
INLINE const T & a_no_check(ssize_t n) const
read-only const access operator: without bounds checking
Definition: EST_TVector.h:254
ssize_t num_samples() const
return the number of samples in the waveform
Definition: EST_Wave.h:143
int ssize_t
short & a(ssize_t i, ssize_t channel=0)
Definition: EST_Wave.cc:128
float & a(ssize_t i, int c=0)
Definition: EST_Track.cc:1025
ssize_t num_frames() const
return number of frames in track
Definition: EST_Track.h:651
ssize_t num_channels() const
return the number of channels in the waveform
Definition: EST_Wave.h:145