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