Edinburgh Speech Tools  2.1-release
EST_filter.h
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 
34 
35 
36 
37 #ifndef __EST_FILTER_H__
38 #define __EST_FILTER_H__
39 
40 #include "EST_Wave.h"
41 #include "EST_FMatrix.h"
42 #include "EST_Track.h"
43 
44 #define DEFAULT_PRE_EMPH_FACTOR 0.95
45 #define DEFAULT_FILTER_ORDER 199
46 
47 /** @defgroup FilterFunctions Filter Functions
48  */
49 
50 /**@defgroup FIRfilters FIR filters
51  @ingroup FilterFunctions
52 
53 Finite impulse response (FIR) filters which are useful for band-pass,
54 low-pass and high-pass filtering.
55 
56 FIR filters perform the following operation:
57 
58 \f[y_t=\sum_{i=0}^{O-1} c_i \; x_{t-i}\f]
59 
60 where \f$O\f$ is the filter order, \f$c_i\f$ are the filter coefficients,
61 \f$x_t\f$ is the input at time \f$t\f$ and \f$y_t\f$ is the output at time
62 \f$t\f$. Functions are provided for designing the filter (i.e. finding
63 the coefficients).
64 
65 */
66 
67 ///@{
68 
69 /** General purpose FIR filter. This function will filter the
70 waveform `sig` with a previously designed filter, given as
71 `numerator`. The filter coefficients can be designed using one of the
72 designed functions, e.g. \ref design_FIR_filter.
73 
74 @see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter,
75 @see FIRfilter, FIRlowpass_filter, FIRhighpass_filter
76 
77 */
78 void FIRfilter(EST_Wave &in_sig, const EST_FVector &numerator,
79  int delay_correction=0);
80 
81 
82 /** General purpose FIR filter. This function will filter the
83 waveform `sig` with a previously designed filter, given as `numerator`.
84 The filter coefficients can be designed using one of the
85 designed functions, e.g. \ref design_FIR_filter.
86 
87 @see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter,
88 @see FIRfilter, FIRlowpass_filter, FIRhighpass_filter
89 */
90 void FIRfilter(const EST_Wave &in_sig, EST_Wave &out_sig,
91  const EST_FVector &numerator, int delay_correction=0);
92 
93 /** General purpose FIR double (zero-phase) filter. This function
94 will double filter the waveform `sig` with a previously designed
95 filter, given as `numerator`. The filter coefficients can be
96 designed using one of the designed functions,
97 e.g. \ref design_FIR_filter. Double filtering is performed by
98 filtering the signal normally, reversing the waveform, filtering
99 again and reversing the waveform again. Normal filtering will impose a
100 lag on the signal depending on the order of the filter. By filtering
101 the signal forwards and backwards, the lags cancel each other out and
102 the output signal is in phase with the input signal.
103 
104 @see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter,
105 @see FIRfilter, FIRlowpass_filter, FIRhighpass_filter
106 */
107 void FIR_double_filter(EST_Wave &in_sig, EST_Wave &out_sig,
108  const EST_FVector &numerator);
109 
110 /** Quick function for one-off low pass filtering. If repeated lowpass
111 filtering is needed, first design the required filter using
112 \ref design_lowpass_filter, and then use \ref FIRfilter to do the actual
113 filtering.
114 
115 @see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter, FIRfilter, FIRhighpass_filter, FIRlowpass_filter
116 
117 @param sigin input waveform, which will be overwritten
118 @param freq
119 @param order number of filter coefficients, eg. 99
120 */
121 void FIRlowpass_filter(EST_Wave &sigin, int freq, int order=DEFAULT_FILTER_ORDER);
122 
123 /** Quick function for one-off low pass filtering. If repeated lowpass
124 filtering is needed, first design the required filter using
125 \ref design_lowpass_filter, and then use \ref FIRfilter to do the actual
126 filtering.
127 
128 @param in_sig input waveform
129 @param out_sig output waveform
130 @param freq cutoff frequency in Hertz
131 @param order number of filter coefficients , e.g. 99
132 
133 @see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter, FIRfilter, FIRhighpass_filter
134 */
135 void FIRlowpass_filter(const EST_Wave &in_sig, EST_Wave &out_sig,
136  int freq, int order=DEFAULT_FILTER_ORDER);
137 
138 /** Quick function for one-off high pass filtering. If repeated lowpass
139 filtering is needed, first design the required filter using
140 design_lowpass_filter, and then use FIRfilter to do the actual
141 filtering.
142 
143 @param in_sig input waveform, which will be overwritten
144 @param freq cutoff frequency in Hertz
145 @param order number of filter coefficients, eg. 99
146 
147 @see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter
148 @see FIRfilter, FIRlowpass_filter, FIRhighpass_filter
149 */
150 void FIRhighpass_filter(EST_Wave &in_sig, int freq, int order);
151 
152 /** Quick function for one-off high pass filtering. If repeated highpass
153 filtering is needed, first design the required filter using
154 design_highpass_filter, and then use FIRfilter to do the actual
155 filtering.
156 
157 @param in_sig input waveform
158 @param out_sig output waveform
159 @param freq cutoff frequency in Hertz
160 @param order number of filter coefficients, eg. 99
161 
162 @see design_FIR_filter, design_lowpass_FIR_filter, design_highpass_FIR_filter
163 @see FIRfilter, FIRlowpass_filter, FIRhighpass_filter
164 */
165 void FIRhighpass_filter(const EST_Wave &sigin, EST_Wave &out_sig,
166  int freq, int order=DEFAULT_FILTER_ORDER);
167 
168 
169 /** Quick function for one-off double low pass filtering.
170 
171 Normal low pass filtering (\ref FIRlowpass_filter) introduces a time delay.
172 This function filters the signal twice, first forward and then backwards,
173 which ensures a zero phase lag. Hence the order parameter need only be
174 half what it is for (\ref FIRlowpass_filter to achieve the same effect.
175 
176 @param in_sig input waveform, which will be overwritten
177 @param freq cutoff frequency in Hertz
178 @param order number of filter coefficients, eg. 99
179 
180 @see FIRhighpass_filter
181 */
182 void FIRhighpass_double_filter(EST_Wave &sigin, int freq,
183  int order=DEFAULT_FILTER_ORDER);
184 
185 /** Quick function for one-off double low pass filtering.
186 
187 Normal low pass filtering (\ref FIRlowpass_filter) introduces a time delay.
188 This function filters the signal twice, first forward and then backwards,
189 which ensures a zero phase lag. Hence the order parameter need only be
190 half what it is for (\ref FIRlowpass_filter) to achieve the same effect.
191 
192 @param in_sig input waveform
193 @param out_sig output waveform
194 @param freq cutoff frequency in Hertz
195 @param order number of filter coefficients, eg. 99
196 
197 @see FIRhighpass_filter
198 
199 */
200 void FIRhighpass_double_filter(const EST_Wave &int_sig, EST_Wave &out_sig,
201  int freq, int order=DEFAULT_FILTER_ORDER);
202 
203 /** Quick function for one-off zero phase high pass filtering.
204 
205 Normal high pass filtering (\ref FIRhighpass_filter) introduces a time delay.
206 This function filters the signal twice, first forward and then backwards,
207 which ensures a zero phase lag. Hence the order parameter need only be
208 half what it is for (\ref FIRhighpass_filter) to achieve the same effect.
209 
210 @param in_sig input waveform, which will be overwritten
211 @param freq cutoff frequency in Hertz
212 @param order number of filter coefficients, eg. 99
213 
214 @see FIRlowpass_filter
215 */
216 void FIRlowpass_double_filter(EST_Wave &sigin, int freq,
217  int order=DEFAULT_FILTER_ORDER);
218 
219 /** Quick function for one-off zero phase high pass filtering.
220 
221 Normal high pass filtering (\ref FIRhighpass_filter) introduces a time delay.
222 This function filters the signal twice, first forward and then backwards,
223 which ensures a zero phase lag. Hence the order parameter need only be
224 half what it is for (\ref FIRhighpass_filter) to achieve the same effect.
225 
226 @param in_sig input waveform
227 @param out_sig output waveform
228 @param freq cutoff frequency in Hertz
229 @param order number of filter coefficients, eg. 99
230 
231 @see FIRlowpass_filter
232 */
233 void FIRlowpass_double_filter(const EST_Wave &in_sig, EST_Wave &out_sig,
234  int freq, int order=DEFAULT_FILTER_ORDER);
235 
236 ///@}
237 
238 /**@defgroup LinearPredictionfilters Linear Prediction filters
239  @ingroup FilterFunctions
240 
241 The linear prediction filters are used for the analysis and synthesis of
242 waveforms according the to linear prediction all-pole model.
243 
244 The linear prediction states that the value of a signal at a given
245 point is equal to a weighted sum of the previous P values, plus a
246 correction value for that point:
247 
248 \f[s_{n} = \sum_{i=1}^{P} a_{i}.s_{n-i} + e_{n}\f]
249 
250 Given a set of coefficients and the original signal, we can use this
251 equation to work out e, the *residual*. Conversely given the
252 coefficients and the residual signal, an estimation of the original
253 signal can be calculated.
254 
255 If a single set of coefficients were used for the entire waveform, the
256 filtering process would be simple. It is usual however to have a
257 different set of coefficients for every frame, and there are many
258 possible ways to switch from one coefficient set to another so as not
259 to cause discontinuities at the frame boundaries.
260 */
261 
262 ///@{
263 
264 /** Synthesize a signal from a single set of linear prediction
265 coefficients and the residual values.
266 
267 @param sig the waveform to be synthesized
268 @param a a single set of LP coefficients
269 @param res the input residual waveform
270 */
271 void lpc_filter(EST_Wave &sig, EST_FVector &a, EST_Wave &res);
272 
273 /** Filter the waveform using a single set of coefficients so as to
274 produce a residual signal.
275 
276 @param sig the speech waveform to be filtered
277 @param a a single set of LP coefficients
278 @param res the output residual waveform
279 */
280 void inv_lpc_filter(EST_Wave &sig, EST_FVector &a, EST_Wave &res);
281 
282 /** Synthesize a signal from a track of linear prediction coefficients.
283 This function takes a set of LP frames and a residual and produces a
284 synthesized signal.
285 
286 For each frame, the function picks an end point, which is half-way
287 between the current frame's time position and the next frame's. A
288 start point is defined as being the previous frame's end. Using these
289 two values, a portion of residual is extracted and passed to
290 \ref lpc_filter along with the LP coefficients for that frame. This
291 function writes directly into the signal for the values between start
292 and end;
293 
294 @param sig the waveform to be synthesized
295 @param lpc a track of time positioned LP coefficients
296 @param res the input residual waveform
297 */
298 void lpc_filter_1(EST_Track &lpc, EST_Wave & res, EST_Wave &sig);
299 
300 /** Synthesize a signal from a track of linear prediction coefficients.
301 This function takes a set of LP frames and a residual and produces a
302 synthesized signal.
303 
304 This is functionally equivalent to \ref lpc_filter_1 except it
305 reduces the residual by 0.5 before filtering. Importantly it is
306 about three times faster than \ref lpc_filter_1 but in doing so uses
307 direct C buffers rather than the neat C++ access function. This
308 function should be regarded as temporary and will be deleted after
309 we restructure the low level classes to give better access.
310 
311 @param sig the waveform to be synthesized
312 @param lpc a track of time positioned LP coefficients
313 @param res the input residual waveform
314 */
315 void lpc_filter_fast(EST_Track &lpc, EST_Wave & res, EST_Wave &sig);
316 
317 /** Produce a residual from a track of linear prediction coefficients
318 and a signal using an overlap add technique.
319 
320 For each frame, the function estimates the local pitch period and
321 picks a start point one period before the current time position and an
322 end point one period after it.
323 
324 A portion of residual corresponding to these times is then produced
325 using \ref inv_lpc_filter. The resultant section of residual is then
326 overlap-added into the main residual wave object.
327 
328 @param sig the speech waveform to be filtered
329 @param lpc a track of time positioned LP coefficients
330 @param res the output residual waveform
331 */
332 void inv_lpc_filter_ola(EST_Wave &sig, EST_Track &lpc, EST_Wave &res);
333 
334 ///@}
335 
336 /**@defgroup PrePostEmphasisfilters Pre/Post Emphasis filters.
337  @ingroup FilterFunctions
338  @brief These functions adjust the spectral tilt of the input waveform.
339 
340 */
341 
342 ///@{
343 
344 /** Pre-emphasis filtering. This performs simple high pass
345 filtering with a one tap filter of value `a`. Normal values of a
346 range between 0.95 and 0.99. */
347 void pre_emphasis(EST_Wave &sig, float a=DEFAULT_PRE_EMPH_FACTOR);
348 
349 /** Pre-emphasis filtering. This performs simple high pass
350 filtering with a one tap filter of value `a`. Normal values of a
351 range between 0.95 and 0.99. */
352 void pre_emphasis(EST_Wave &sig, EST_Wave &out,
353  float a=DEFAULT_PRE_EMPH_FACTOR);
354 
355 /** Post-emphasis filtering. This performs simple low pass
356 filtering with a one tap filter of value a. Normal values of a range
357 between 0.95 and 0.99. The same values of `a` should be used when
358 pre- and post-emphasizing the same signal. */
360 
361 /** Post-emphasis filtering. This performs simple low pass
362 filtering with a one tap filter of value a. Normal values of a range
363 between 0.95 and 0.99. The same values of `a` should be used when
364 pre- and post-emphasizing the same signal. */
365 void post_emphasis(EST_Wave &sig, EST_Wave &out,
366  float a=DEFAULT_PRE_EMPH_FACTOR);
367 
368 ///@}
369 
370 /**@defgroup Miscellaneousfilters Miscellaneous filters.
371  @ingroup FilterFunctions
372  @brief Some of these filters are non-linear and therefore don't fit the normal paradigm.
373 
374 */
375 ///@{
376 
377 /** Filters the waveform by means of median smoothing.
378 
379 This is a sort of low pass filter which aims to remove extreme values.
380 Median smoothing works examining each sample in the wave, taking all
381 the values in a window of size `n` around that sample, sorting
382 them and replacing that sample with the middle ranking sample in the
383 sorted samples.
384 
385 @param c waveform to be filtered
386 @param n size of smoothing window
387 
388 */
389 void simple_mean_smooth(EST_Wave &c, int n);
390 
391 ///@}
392 
393 #endif /* __EST_FILTER_H__ */
394 
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
#define DEFAULT_FILTER_ORDER
Definition: EST_filter.h:45
void FIRfilter(EST_Wave &in_sig, const EST_FVector &numerator, int delay_correction=0)
Definition: filter.cc:336
void pre_emphasis(EST_Wave &sig, float a=DEFAULT_PRE_EMPH_FACTOR)
Definition: filter.cc:256
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
Definition: EST_FMatrix.h:119
void FIRlowpass_filter(EST_Wave &sigin, int freq, int order=DEFAULT_FILTER_ORDER)
Definition: filter.cc:526
void FIR_double_filter(EST_Wave &in_sig, EST_Wave &out_sig, const EST_FVector &numerator)
Definition: filter.cc:408
void lpc_filter_1(EST_Track &lpc, EST_Wave &res, EST_Wave &sig)
Definition: filter.cc:149
#define DEFAULT_PRE_EMPH_FACTOR
Definition: EST_filter.h:44
void lpc_filter_fast(EST_Track &lpc, EST_Wave &res, EST_Wave &sig)
Definition: filter.cc:197
void inv_lpc_filter_ola(EST_Wave &sig, EST_Track &lpc, EST_Wave &res)
Definition: filter.cc:102
void FIRlowpass_double_filter(EST_Wave &sigin, int freq, int order=DEFAULT_FILTER_ORDER)
Definition: filter.cc:549
void inv_lpc_filter(EST_Wave &sig, EST_FVector &a, EST_Wave &res)
Definition: filter.cc:68
void FIRhighpass_filter(EST_Wave &in_sig, int freq, int order)
Definition: filter.cc:534
void FIRhighpass_double_filter(EST_Wave &sigin, int freq, int order=DEFAULT_FILTER_ORDER)
Definition: filter.cc:584
void simple_mean_smooth(EST_Wave &c, int n)
Definition: filter.cc:297
void post_emphasis(EST_Wave &sig, float a=DEFAULT_PRE_EMPH_FACTOR)
Definition: filter.cc:242
void lpc_filter(EST_Wave &sig, EST_FVector &a, EST_Wave &res)
Definition: filter.cc:53