1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | /*!
|
---|
6 | * \file ButterworthLowPass_impl.h
|
---|
7 | * \brief Classe permettant le calcul d'un filtre passe bas de Butterworth
|
---|
8 | * \author Guillaume Sanahuja, Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
9 | * \date 2011/05/01
|
---|
10 | * \version 4.0
|
---|
11 | */
|
---|
12 |
|
---|
13 | #ifndef BUTTERWORTHLOWPASS_H_IMPL_H
|
---|
14 | #define BUTTERWORTHLOWPASS_H_IMPL_H
|
---|
15 |
|
---|
16 | #include <IODevice.h>
|
---|
17 | #include <Butterworth.h>
|
---|
18 |
|
---|
19 | namespace flair
|
---|
20 | {
|
---|
21 | namespace core
|
---|
22 | {
|
---|
23 | class cvmatrix;
|
---|
24 | }
|
---|
25 | namespace gui
|
---|
26 | {
|
---|
27 | class LayoutPosition;
|
---|
28 | class SpinBox;
|
---|
29 | class DoubleSpinBox;
|
---|
30 | }
|
---|
31 | namespace filter
|
---|
32 | {
|
---|
33 | class ButterworthLowPass;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | // Storage for Layout
|
---|
38 | //de-templatized for pimpl idom
|
---|
39 | //comes from iir from Bernd Porr
|
---|
40 | class LayoutStorage
|
---|
41 | {
|
---|
42 | public:
|
---|
43 | LayoutStorage(int MaxPoles)
|
---|
44 | {
|
---|
45 | this->MaxPoles=MaxPoles;
|
---|
46 | m_pairs=(Iir::PoleZeroPair*)malloc((MaxPoles+1)/2*sizeof(Iir::PoleZeroPair));
|
---|
47 | }
|
---|
48 | ~LayoutStorage()
|
---|
49 | {
|
---|
50 | free(m_pairs);
|
---|
51 | }
|
---|
52 | operator Iir::LayoutBase ()
|
---|
53 | {
|
---|
54 | return Iir::LayoutBase (MaxPoles, m_pairs);
|
---|
55 | }
|
---|
56 |
|
---|
57 | private:
|
---|
58 | Iir::PoleZeroPair *m_pairs;
|
---|
59 | int MaxPoles;
|
---|
60 | };
|
---|
61 |
|
---|
62 | // Storage for Cascade
|
---|
63 | //de-templatized for pimpl idom
|
---|
64 | //comes from iir from Bernd Porr
|
---|
65 | class CascadeStages
|
---|
66 | {
|
---|
67 | public:
|
---|
68 | CascadeStages(int MaxStages)
|
---|
69 | {
|
---|
70 | this->MaxStages=MaxStages;
|
---|
71 | m_stages=(Iir::Cascade::Stage*)malloc(MaxStages*sizeof(Iir::Cascade::Stage));
|
---|
72 | m_states=(Iir::DirectFormII*)malloc(MaxStages*sizeof(Iir::DirectFormII));
|
---|
73 | }
|
---|
74 | ~CascadeStages()
|
---|
75 | {
|
---|
76 | free(m_stages);
|
---|
77 | free(m_states);
|
---|
78 | }
|
---|
79 | void reset ()
|
---|
80 | {
|
---|
81 | Iir::DirectFormII* state = m_states;
|
---|
82 | for (int i = MaxStages; --i >= 0; ++state)
|
---|
83 | state->reset();
|
---|
84 | }
|
---|
85 |
|
---|
86 | template <typename Sample>
|
---|
87 | inline Sample filter(const Sample in)
|
---|
88 | {
|
---|
89 | double out = in;
|
---|
90 | Iir::DirectFormII* state = m_states;
|
---|
91 | Iir::Biquad const* stage = m_stages;
|
---|
92 | for (int i = MaxStages; --i >= 0; ++state, ++stage)
|
---|
93 | out = state->process1 (out, *stage);
|
---|
94 | return static_cast<Sample> (out);
|
---|
95 | }
|
---|
96 |
|
---|
97 | Iir::Cascade::Storage getCascadeStorage()
|
---|
98 | {
|
---|
99 | return Iir::Cascade::Storage (MaxStages, m_stages);
|
---|
100 | }
|
---|
101 |
|
---|
102 | private:
|
---|
103 | int MaxStages;
|
---|
104 | Iir::Cascade::Stage *m_stages;
|
---|
105 | Iir::DirectFormII *m_states;
|
---|
106 | };
|
---|
107 |
|
---|
108 | //de-templatized for pimpl idom
|
---|
109 | //comes from iir from Bernd Porr
|
---|
110 | class PoleFilter : Iir::Butterworth::LowPassBase,public CascadeStages
|
---|
111 | {
|
---|
112 | public:
|
---|
113 | PoleFilter (int MaxPoles):CascadeStages((MaxPoles + 1) / 2)
|
---|
114 | {
|
---|
115 | this->MaxPoles=MaxPoles;
|
---|
116 | m_analogStorage=new LayoutStorage(MaxPoles);
|
---|
117 | m_digitalStorage=new LayoutStorage(MaxPoles);
|
---|
118 | // This glues together the factored base classes
|
---|
119 | // with the templatized storage classes.
|
---|
120 | Iir::Butterworth::LowPassBase::setCascadeStorage (this->getCascadeStorage());
|
---|
121 | Iir::Butterworth::LowPassBase::setPrototypeStorage (*m_analogStorage,*m_digitalStorage);
|
---|
122 | }
|
---|
123 | ~PoleFilter()
|
---|
124 | {
|
---|
125 | delete m_analogStorage;
|
---|
126 | delete m_digitalStorage;
|
---|
127 | }
|
---|
128 | void setup (double sampleRate,double cutoffFrequency)
|
---|
129 | {
|
---|
130 | Iir::Butterworth::LowPassBase::setup (MaxPoles, sampleRate,cutoffFrequency);
|
---|
131 | }
|
---|
132 |
|
---|
133 | private:
|
---|
134 | int MaxPoles;
|
---|
135 | LayoutStorage* m_analogStorage;
|
---|
136 | LayoutStorage* m_digitalStorage;
|
---|
137 | };
|
---|
138 |
|
---|
139 |
|
---|
140 | class ButterworthLowPass_impl
|
---|
141 | {
|
---|
142 | public:
|
---|
143 | ButterworthLowPass_impl(flair::filter::ButterworthLowPass* self,const flair::gui::LayoutPosition* position,std::string name,int order);
|
---|
144 | ~ButterworthLowPass_impl();
|
---|
145 | void UpdateFrom(const flair::core::io_data *data);
|
---|
146 | flair::core::cvmatrix *output;
|
---|
147 |
|
---|
148 | private:
|
---|
149 | flair::gui::DoubleSpinBox *cutoff,*T;
|
---|
150 | PoleFilter* f;
|
---|
151 | bool first_update;
|
---|
152 | flair::core::Time previous_time;
|
---|
153 | };
|
---|
154 |
|
---|
155 | #endif // BUTTERWORTHLOWPASS_H_IMPL_H
|
---|