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/Cascade.h"
38 
39 namespace Dsp {
40 
Cascade()41 Cascade::Cascade ()
42   : m_numStages (0)
43   , m_maxStages (0)
44   , m_stageArray (0)
45 {
46 }
47 
setCascadeStorage(const Storage & storage)48 void Cascade::setCascadeStorage (const Storage& storage)
49 {
50   m_numStages = 0;
51   m_maxStages = storage.maxStages;
52   m_stageArray = storage.stageArray;
53 }
54 
response(double normalizedFrequency) const55 complex_t Cascade::response (double normalizedFrequency) const
56 {
57   double w = 2 * doublePi * normalizedFrequency;
58   const complex_t czn1 = std::polar (1., -w);
59   const complex_t czn2 = std::polar (1., -2 * w);
60   complex_t ch (1);
61   complex_t cbot (1);
62 
63   const Biquad* stage = m_stageArray;
64   for (int i = m_numStages; --i >=0; ++stage)
65   {
66     complex_t cb (1);
67     complex_t ct    (stage->getB0()/stage->getA0());
68     ct = addmul (ct, stage->getB1()/stage->getA0(), czn1);
69     ct = addmul (ct, stage->getB2()/stage->getA0(), czn2);
70     cb = addmul (cb, stage->getA1()/stage->getA0(), czn1);
71     cb = addmul (cb, stage->getA2()/stage->getA0(), czn2);
72     ch   *= ct;
73     cbot *= cb;
74   }
75 
76   return ch / cbot;
77 }
78 
getPoleZeros() const79 std::vector<PoleZeroPair> Cascade::getPoleZeros () const
80 {
81   std::vector<PoleZeroPair> vpz;
82   vpz.reserve (m_numStages);
83 
84   const Stage* stage = m_stageArray;
85   for (int i = m_numStages; --i >=0;)
86   {
87     BiquadPoleState bps (*stage++);
88     assert (!bps.isSinglePole() || i == 0);
89     vpz.push_back (bps);
90   }
91 
92   return vpz;
93 }
94 
applyScale(double scale)95 void Cascade::applyScale (double scale)
96 {
97   // For higher order filters it might be helpful
98   // to spread this factor between all the stages.
99   assert (m_numStages > 0);
100   m_stageArray->applyScale (scale);
101 }
102 
setLayout(const LayoutBase & proto)103 void Cascade::setLayout (const LayoutBase& proto)
104 {
105   const int numPoles = proto.getNumPoles();
106   m_numStages = (numPoles + 1)/ 2;
107   assert (m_numStages <= m_maxStages);
108 
109   Biquad* stage = m_stageArray;
110   for (int i = 0; i < m_numStages; ++i, ++stage)
111     stage->setPoleZeroPair (proto[i]);
112 
113   applyScale (proto.getNormalGain() /
114               std::abs (response (proto.getNormalW() / (2 * doublePi))));
115 }
116 
117 }
118 
119