source: flair-src/trunk/lib/FlairFilter/src/unexported/ButterworthLowPass_impl.h @ 7

Last change on this file since 7 was 7, checked in by Sanahuja Guillaume, 5 years ago

filter and meta

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