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