1 /*******************************************************************************
2 
3 "A Collection of Useful C++ Classes for Digital Signal Processing"
4  By Vinnie Falco
5 
6 Official project location:
7 https://github.com/vinniefalco/DSPFilters
8 
9 See Documentation.cpp for contact information, notes, and bibliography.
10 
11 --------------------------------------------------------------------------------
12 
13 License: MIT License (http://www.opensource.org/licenses/mit-license.php)
14 Copyright (c) 2009 by Vinnie Falco
15 
16 Permission is hereby granted, free of charge, to any person obtaining a copy
17 of this software and associated documentation files (the "Software"), to deal
18 in the Software without restriction, including without limitation the rights
19 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20 copies of the Software, and to permit persons to whom the Software is
21 furnished to do so, subject to the following conditions:
22 
23 The above copyright notice and this permission notice shall be included in
24 all copies or substantial portions of the Software.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 THE SOFTWARE.
33 
34 *******************************************************************************/
35 
36 #include "DspFilters/Common.h"
37 #include "DspFilters/Design.h"
38 
39 #include <stdexcept>
40 #include <sstream>
41 #include <iostream>
42 #include <iomanip>
43 
44 namespace Dsp {
45 
ParamInfo()46 ParamInfo::ParamInfo ()
47 {
48   throw std::logic_error ("invalid usage of ParamInfo");
49 }
50 
clamp(double nativeValue) const51 double ParamInfo::clamp (double nativeValue) const
52 {
53   const double minValue = toNativeValue (0);
54   const double maxValue = toNativeValue (1);
55   if (nativeValue < minValue)
56     nativeValue = minValue;
57   else if (nativeValue > maxValue)
58     nativeValue = maxValue;
59   return nativeValue;
60 }
61 
62 //------------------------------------------------------------------------------
63 
Int_toControlValue(double nativeValue) const64 double ParamInfo::Int_toControlValue (double nativeValue) const
65 {
66   return (nativeValue - m_arg1) / (m_arg2 - m_arg1);
67 }
68 
Int_toNativeValue(double controlValue) const69 double ParamInfo::Int_toNativeValue (double controlValue) const
70 {
71   return std::floor (m_arg1 + controlValue * (m_arg2 - m_arg1) + 0.5);
72 }
73 
74 //------------------------------------------------------------------------------
75 
Real_toControlValue(double nativeValue) const76 double ParamInfo::Real_toControlValue (double nativeValue) const
77 {
78   return (nativeValue - m_arg1) / (m_arg2 - m_arg1);
79 }
80 
Real_toNativeValue(double controlValue) const81 double ParamInfo::Real_toNativeValue (double controlValue) const
82 {
83   return m_arg1 + controlValue * (m_arg2 - m_arg1);
84 }
85 
86 //------------------------------------------------------------------------------
87 
Log_toControlValue(double nativeValue) const88 double ParamInfo::Log_toControlValue (double nativeValue) const
89 {
90   const double base = 1.5;
91   double l0 = log (m_arg1) / log (base);
92   double l1 = log (m_arg2) / log (base);
93   return (log (nativeValue) / log(base) - l0) / (l1 - l0);
94 }
95 
Log_toNativeValue(double controlValue) const96 double ParamInfo::Log_toNativeValue (double controlValue) const
97 {
98   const double base = 1.5;
99   double l0 = log (m_arg1) / log (base);
100   double l1 = log (m_arg2) / log (base);
101   return pow (base, l0 + controlValue * (l1 - l0));
102 }
103 
104 //------------------------------------------------------------------------------
105 
Pow2_toControlValue(double nativeValue) const106 double ParamInfo::Pow2_toControlValue (double nativeValue) const
107 {
108   return ((log (nativeValue) / log (2.)) - m_arg1) / (m_arg2 - m_arg1);
109 }
110 
Pow2_toNativeValue(double controlValue) const111 double ParamInfo::Pow2_toNativeValue (double controlValue) const
112 {
113   return pow (2., (controlValue * (m_arg2 - m_arg1)) + m_arg1);
114 }
115 
116 //------------------------------------------------------------------------------
117 
Int_toString(double nativeValue) const118 std::string ParamInfo::Int_toString (double nativeValue) const
119 {
120   std::ostringstream os;
121   os << int (nativeValue);
122   return os.str();
123 }
124 
Hz_toString(double nativeValue) const125 std::string ParamInfo::Hz_toString (double nativeValue) const
126 {
127   std::ostringstream os;
128   os << int (nativeValue) << " Hz";
129   return os.str();
130 }
131 
Real_toString(double nativeValue) const132 std::string ParamInfo::Real_toString (double nativeValue) const
133 {
134   std::ostringstream os;
135   os << std::fixed << std::setprecision(3) << nativeValue;
136   return os.str();
137 }
138 
Db_toString(double nativeValue) const139 std::string ParamInfo::Db_toString (double nativeValue) const
140 {
141   const double af = fabs (nativeValue);
142   int prec;
143   if (af < 1)
144     prec = 3;
145   else if (af < 10)
146     prec = 2;
147   else
148     prec = 1;
149   std::ostringstream os;
150   os << std::fixed << std::setprecision (prec) << nativeValue << " dB";
151   return os.str();
152 }
153 
154 //------------------------------------------------------------------------------
155 
defaultSampleRateParam()156 ParamInfo ParamInfo::defaultSampleRateParam ()
157 {
158   return ParamInfo (idSampleRate, "Fs", "Sample Rate",
159                      11025, 192000, 44100,
160                      &ParamInfo::Real_toControlValue,
161                      &ParamInfo::Real_toNativeValue,
162                      &ParamInfo::Hz_toString);
163 }
164 
defaultCutoffFrequencyParam()165 ParamInfo ParamInfo::defaultCutoffFrequencyParam ()
166 {
167   return ParamInfo (idFrequency, "Fc", "Cutoff Frequency",
168                      10, 22040, 2000,
169                      &ParamInfo::Log_toControlValue,
170                      &ParamInfo::Log_toNativeValue,
171                      &ParamInfo::Hz_toString);
172 }
173 
defaultCenterFrequencyParam()174 ParamInfo ParamInfo::defaultCenterFrequencyParam ()
175 {
176   return ParamInfo (idFrequency, "Fc", "Center Frequency",
177                      10, 22040, 2000,
178                      &ParamInfo::Log_toControlValue,
179                      &ParamInfo::Log_toNativeValue,
180                      &ParamInfo::Hz_toString);
181 }
182 
defaultQParam()183 ParamInfo ParamInfo::defaultQParam ()
184 {
185   return ParamInfo (idQ, "Q", "Resonance",
186                      -4, 4, 1,
187                      &ParamInfo::Pow2_toControlValue,
188                      &ParamInfo::Pow2_toNativeValue,
189                      &ParamInfo::Real_toString);
190 }
191 
defaultBandwidthParam()192 ParamInfo ParamInfo::defaultBandwidthParam ()
193 {
194   return ParamInfo (idBandwidth, "BW", "Bandwidth (Octaves)",
195                      -4, 4, 1,
196                      &ParamInfo::Pow2_toControlValue,
197                      &ParamInfo::Pow2_toNativeValue,
198                      &ParamInfo::Real_toString);
199 }
200 
defaultBandwidthHzParam()201 ParamInfo ParamInfo::defaultBandwidthHzParam ()
202 {
203   return ParamInfo (idBandwidthHz, "BW", "Bandwidth (Hz)",
204                      10, 22040, 1720,
205                      &ParamInfo::Log_toControlValue,
206                      &ParamInfo::Log_toNativeValue,
207                      &ParamInfo::Hz_toString);
208 }
209 
defaultGainParam()210 ParamInfo ParamInfo::defaultGainParam ()
211 {
212   return ParamInfo (idGain, "Gain", "Gain",
213                      -24, 24, -6,
214                      &ParamInfo::Real_toControlValue,
215                      &ParamInfo::Real_toNativeValue,
216                      &ParamInfo::Db_toString);
217 }
218 
defaultSlopeParam()219 ParamInfo ParamInfo::defaultSlopeParam ()
220 {
221   return ParamInfo (idSlope, "Slope", "Slope",
222                      -2, 2, 1,
223                      &ParamInfo::Pow2_toControlValue,
224                      &ParamInfo::Pow2_toNativeValue,
225                      &ParamInfo::Real_toString);
226 }
227 
defaultRippleDbParam()228 ParamInfo ParamInfo::defaultRippleDbParam ()
229 {
230   return ParamInfo (idRippleDb, "Ripple", "Ripple dB",
231                      0.001, 12, 0.01,
232                      &ParamInfo::Real_toControlValue,
233                      &ParamInfo::Real_toNativeValue,
234                      &ParamInfo::Db_toString);
235 }
236 
defaultStopDbParam()237 ParamInfo ParamInfo::defaultStopDbParam ()
238 {
239   return ParamInfo (idStopDb, "Stop", "Stopband dB",
240                      3, 60, 48,
241                      &ParamInfo::Real_toControlValue,
242                      &ParamInfo::Real_toNativeValue,
243                      &ParamInfo::Db_toString);
244 }
245 
defaultRolloffParam()246 ParamInfo ParamInfo::defaultRolloffParam ()
247 {
248   return ParamInfo (idRolloff, "W", "Transition Width",
249                      -16, 4, 0,
250                      &ParamInfo::Real_toControlValue,
251                      &ParamInfo::Real_toNativeValue,
252                      &ParamInfo::Real_toString);
253 }
254 
defaultPoleRhoParam()255 ParamInfo ParamInfo::defaultPoleRhoParam ()
256 {
257   return ParamInfo (idPoleRho, "Pd", "Pole Distance",
258                      0, 1, 0.5,
259                      &ParamInfo::Real_toControlValue,
260                      &ParamInfo::Real_toNativeValue,
261                      &ParamInfo::Real_toString);
262 }
263 
defaultPoleThetaParam()264 ParamInfo ParamInfo::defaultPoleThetaParam ()
265 {
266   return ParamInfo (idPoleTheta, "Pa", "Pole Angle",
267                      0, doublePi, doublePi/2,
268                      &ParamInfo::Real_toControlValue,
269                      &ParamInfo::Real_toNativeValue,
270                      &ParamInfo::Real_toString);
271 }
272 
defaultZeroRhoParam()273 ParamInfo ParamInfo::defaultZeroRhoParam ()
274 {
275   return ParamInfo (idZeroRho, "Pd", "Zero Distance",
276                      0, 1, 0.5,
277                      &ParamInfo::Real_toControlValue,
278                      &ParamInfo::Real_toNativeValue,
279                      &ParamInfo::Real_toString);
280 }
281 
defaultZeroThetaParam()282 ParamInfo ParamInfo::defaultZeroThetaParam ()
283 {
284   return ParamInfo (idZeroTheta, "Pa", "Zero Angle",
285                      0, doublePi, doublePi/2,
286                      &ParamInfo::Real_toControlValue,
287                      &ParamInfo::Real_toNativeValue,
288                      &ParamInfo::Real_toString);
289 }
290 
defaultPoleRealParam()291 ParamInfo ParamInfo::defaultPoleRealParam ()
292 {
293   return ParamInfo (idPoleReal, "A1", "Pole Real",
294                      -1, 1, 0.25,
295                      &ParamInfo::Real_toControlValue,
296                      &ParamInfo::Real_toNativeValue,
297                      &ParamInfo::Real_toString);
298 }
299 
defaultZeroRealParam()300 ParamInfo ParamInfo::defaultZeroRealParam ()
301 {
302   return ParamInfo (idZeroReal, "B1", "Zero Real",
303                      -1, 1, -0.25,
304                      &ParamInfo::Real_toControlValue,
305                      &ParamInfo::Real_toNativeValue,
306                      &ParamInfo::Real_toString);
307 }
308 }
309 
310