Edinburgh Speech Tools  2.1-release
sigfilter_main.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 : Paul Taylor */
34 /* Date : April 1995 */
35 /*-----------------------------------------------------------------------*/
36 /* Filter signals */
37 /* */
38 /*=======================================================================*/
39 
40 #include <cstdlib>
41 #include <iostream>
42 #include <cmath>
43 #include "EST_Wave.h"
44 #include "EST_cmd_line.h"
45 #include "EST_cmd_line_options.h"
46 #include "EST_sigpr.h"
47 #include "EST_wave_aux.h"
48 
49 using namespace std;
50 
51 void inv_filter_ola(EST_Wave &sig, EST_Track &lpc, EST_Wave &res);
52 void frame_filter(EST_Wave &sig, EST_Track &lpc, EST_Wave &res);
53 void inv_lpc_filter_ola(EST_Wave &in_sig, EST_Track &lpc, EST_Wave &out_sig);
54 
55 void FIR_double_filter(EST_Wave &in_sig, EST_Wave &out_sig,
56  const EST_FVector &numerator);
57 
58 
59 int main (int argc, char *argv[])
60 {
61  EST_Wave sig, fsig;
62  EST_String in_file("-"), out_file("-"), op_file(""), test;
63  EST_Option al;
65  EST_Track filter;
66 
68  (argc, argv,
69  EST_String("[input file0] -o [output file]\n") +
70  "Summary: filter waveform files\n"
71  "use \"-\" to make input and output files stdin/out\n"
72  "-h Options help\n"+
73  options_wave_input()+ "\n"+
74  options_wave_output()+ "\n"
75  "-scale <float> Scaling factor. Increase or descrease the amplitude\n"
76  " of the whole waveform by the factor given\n\n"
77 
78  "-scaleN <float> Scaling factor with normalization. \n"
79  " The waveform is scaled to its maximum level, after which \n"
80  " it is scaled by the factor given\n\n"
81  "-double Perform double filtering by applying a forward filter,\n"
82  " then a backward filter, leaving the filtered signla in phase\n"
83  " with the original\n\n"
84  "-lpfilter <int> Low pass filter, with cutoff frequency in Hz \n"
85  " Filtering is performed by a FIR filter which is built at run \n"
86  " time. The order of the filter can be given by -forder. The \n"
87  " default value is 199\n\n"
88 
89  "-hpfilter <int> High pass filter, with cutoff frequency in Hz \n"
90  " Filtering is performed by a FIR filter which is \n"
91  " built at run time. The order of the filter can \n"
92  " be given by -forder. The default value is 199.\n\n"
93 
94  "-forder <int> Order of FIR filter used for lpfilter and \n"
95  " hpfilter. This must be ODD. Sensible values range \n"
96  " from 19 (quick but with a shallow rolloff) to 199 \n"
97  " (slow but with a steep rolloff). The default is 199.\n\n"
98  "-lpcfilter <ifile> Track file containing lpc filter coefficients\n\n"
99  "-firfilter <ifile> File containing a single set of FIR filter\n"
100  " coefficients\n\n"
101  "-inv_filter use filter coefficients for inverse filtering\n\n",
102  files, al);
103 
104  out_file = al.present("-o") ? al.val("-o") : (EST_String)"-";
105 
106  if (read_wave(sig, files.first(), al) != format_ok)
107  exit(-1);
108 
109  if (al.present("-s")) // rescale
110  {
111  float scale = al.fval("-s", 0);
112  sig.rescale(scale);
113  }
114  else if (al.present("-scaleN")) // rescale
115  {
116  float scale = al.fval("-scaleN", 0);
117  if ((scale < 0) || (scale > 1.0))
118  {
119  cerr << "ch_wave: -scaleN must be in range 0 to 1" << endl;
120  exit(-1);
121  }
122  sig.rescale(scale,1);
123  }
124 
125  // default is to filter before any resampling etc.
126  // (this may cause problems for multiplexed data !)
127 
128  if (al.present("-lpfilter"))
129  {
130  FIRlowpass_filter(sig, al.ival("-lpfilter"),al.ival("-forder"));
131  fsig = sig;
132  }
133  if (al.present("-hpfilter"))
134  {
135  FIRhighpass_filter(sig,al.ival("-hpfilter"),al.ival("-forder"));
136  fsig = sig;
137  }
138 
139  if (al.present("-lpcfilter"))
140  {
141  filter.load(al.val("-lpcfilter"));
142  if (al.present("-inv_filter"))
143  inv_lpc_filter_ola(sig, filter, fsig);
144  else
145 // frame_filter(sig, filter, fsig);
146  cout << "not done yet\n";
147  }
148  if (al.present("-firfilter"))
149  {
150  EST_FVector firfilter;
151  firfilter.load(al.val("-firfilter"));
152  if (al.present("-double"))
153  FIR_double_filter(sig, fsig, firfilter);
154  else
155  FIRfilter(sig, fsig, firfilter);
156  }
157 
158  if (write_wave(fsig, out_file, al) != write_ok)
159  {
160  cerr << "sigfilter: failed to write output to \"" << out_file
161  << "\"" << endl;
162  exit(-1);
163  }
164  return 0;
165 }
A class for storing digital waveforms. The waveform is stored as an array of 16 bit shorts...
Definition: EST_Wave.h:64
void FIRfilter(EST_Wave &in_sig, const EST_FVector &numerator, int delay_correction=0)
Definition: filter.cc:336
EST_read_status load(const EST_String &filename)
load vector from file filename.
Definition: EST_FMatrix.cc:692
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
Definition: EST_FMatrix.h:119
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:82
void FIRlowpass_filter(EST_Wave &sigin, int freq, int order=DEFAULT_FILTER_ORDER)
Definition: filter.cc:526
void frame_filter(EST_Wave &sig, EST_Track &lpc, EST_Wave &res)
float fval(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:104
void rescale(float gain, int normalize=0)
Definition: EST_Wave.cc:506
void FIR_double_filter(EST_Wave &in_sig, EST_Wave &out_sig, const EST_FVector &numerator)
Definition: filter.cc:408
EST_read_status load(const EST_String name, float ishift=0.0, float startt=0.0)
Definition: EST_Track.cc:1312
void inv_lpc_filter_ola(EST_Wave &in_sig, EST_Track &lpc, EST_Wave &out_sig)
Definition: filter.cc:102
The file was written successfully.
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:152
EST_String options_wave_input(void)
void FIRhighpass_filter(EST_Wave &in_sig, int freq, int order)
Definition: filter.cc:534
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145
int main(int argc, char *argv[])
EST_write_status write_wave(EST_Wave &sig, const EST_String &in_file, EST_Option &al)
EST_read_status read_wave(EST_Wave &sig, const EST_String &in_file, EST_Option &al)
EST_String options_wave_output(void)
#define format_ok
int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
void inv_filter_ola(EST_Wave &sig, EST_Track &lpc, EST_Wave &res)
EST_String
int parse_command_line(int argc, char *argv[], const EST_String &usage, EST_StrList &files, EST_Option &al, int make_stdio=1)
Definition: cmd_line.cc:101