Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
design_filter_main.cc
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, Simon King */
34 /* Date : 1995-99 */
35 /*-----------------------------------------------------------------------*/
36 /* Design FIR filter */
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 "sigpr/EST_filter_design.h"
47 
48 
49 int main (int argc, char *argv[])
50 {
51  EST_FVector fresponse, filter;
52  EST_String in_file("-"), out_file("-"), op_file(""), test;
53  EST_Option al;
55  int forder;
56 
57  parse_command_line
58  (argc, argv,
59  EST_String("[input file0] -o [output file]\n") +
60  "Summary: filter waveform files\n"
61  "use \"-\" to make input and output files stdin/out\n"
62  "-h Options help\n"
63  "-forder <int> Order of FIR filter. This must be ODD.\n"
64  " Sensible values range \n"
65  " from 19 (quick but with a shallow rolloff) to 199 \n"
66  " (slow but with a steep rolloff). The default is 199.\n\n"
67  "-double Design a filter suitable for double (zero-phase)\n"
68  " filtering\n\n"
69  "-o <ofile> output filter file\n",
70  files, al);
71 
72  out_file = al.present("-o") ? al.val("-o") : (EST_String)"-";
73 
74  if (fresponse.load(files.first()) != format_ok)
75  exit(-1);
76 
77  forder = al.present("-forder") ? al.ival("-forder") : 199;
78 
79  if (al.present("-double"))
80  for (int i = 0; i < fresponse.length(); i++)
81  fresponse[i] = sqrt(fresponse[i]);
82 
83  // user gives freq response for freq range 0...half sampling freq
84  // we need to make a mirror image of this
85 
86  int l = fresponse.length() * 2;
87  EST_FVector full_fresponse(l);
88 
89  for(int i = 0;i<fresponse.length();i++)
90  {
91  full_fresponse[i] = fresponse(i);
92  full_fresponse[l-1-i] = fresponse(i);
93  }
94 
95  filter = design_FIR_filter(full_fresponse, forder);
96  filter.save(out_file, "est_ascii");
97 }
98 
99 /** @name Example
100 
101 <title>Designing a bandpass filter</title>
102 
103 The frequency response vector must be placed in a file, in either
104 ascii of EST headered format. For example:
105 <screen>
106 <para>EST_File fvector</para>
107 <para>version 1</para>
108 <para>DataType ascii</para>
109 <para>length 128</para>
110 <para>EST_Header_End</para>
111 <para>0.0</para>
112 <para>0.0</para>
113 <para>.....[etc]</para>
114 <para>1.0</para>
115 <para>1.0</para>
116 <para>1.0</para>
117 <para>.....[etc]</para>
118 <para>0.0</para>
119 <para>0.0</para>
120 <para>0.0</para>
121 <para>.....[etc]</para>
122 </screen>
123 And the filter is simply designed using
124 </para>
125 <para>
126 <screen>
127 $ design_filter -o filter.coefficients filter.freq_response
128 </screen>
129 </para>
130 <para>
131 where filter.freq_response is the above file, and filter.coefficients
132 is the output file which can be used by \ref sigfilter .
133 </para>
134 
135 */
136  //@{
137  //@}
138 
139 //@}
const T & first() const
return const reference to first item in list
Definition: EST_TList.h:154
EST_FVector design_FIR_filter(const EST_FVector &freq_response, int filter_order)
Definition: filter.cc:416
EST_read_status load(const EST_String &filename)
load vector from file filename.
Definition: EST_FMatrix.cc:681
A vector class for floating point numbers. EST_FVector x should be used instead of float *x wherever ...
Definition: EST_FMatrix.h:118
int ival(const EST_String &rkey, int m=1) const
Definition: EST_Option.cc:76
EST_write_status save(const EST_String &filename, const EST_String &type)
save vector to file filename.
Definition: EST_FMatrix.cc:833
INLINE int length() const
number of items in vector.
Definition: EST_TVector.h:250
const int present(const K &rkey) const
Returns true if key is present.
Definition: EST_TKVL.cc:222
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
Definition: EST_TKVL.cc:145