[324] | 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 | namespace core {
|
---|
| 21 | class Matrix;
|
---|
| 22 | }
|
---|
| 23 | namespace gui {
|
---|
| 24 | class LayoutPosition;
|
---|
| 25 | class SpinBox;
|
---|
| 26 | class DoubleSpinBox;
|
---|
| 27 | }
|
---|
| 28 | namespace filter {
|
---|
| 29 | class ButterworthLowPass;
|
---|
| 30 | }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | // Storage for Layout
|
---|
| 34 | // de-templatized for pimpl idom
|
---|
| 35 | // comes from iir from Bernd Porr
|
---|
| 36 | class LayoutStorage {
|
---|
| 37 | public:
|
---|
| 38 | LayoutStorage(int MaxPoles) {
|
---|
| 39 | this->MaxPoles = MaxPoles;
|
---|
| 40 | m_pairs = (Iir::PoleZeroPair *)malloc((MaxPoles + 1) / 2 *
|
---|
| 41 | sizeof(Iir::PoleZeroPair));
|
---|
| 42 | }
|
---|
| 43 | ~LayoutStorage() { free(m_pairs); }
|
---|
| 44 | operator Iir::LayoutBase() { return Iir::LayoutBase(MaxPoles, m_pairs); }
|
---|
| 45 |
|
---|
| 46 | private:
|
---|
| 47 | Iir::PoleZeroPair *m_pairs;
|
---|
| 48 | int MaxPoles;
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | // Storage for Cascade
|
---|
| 52 | // de-templatized for pimpl idom
|
---|
| 53 | // comes from iir from Bernd Porr
|
---|
| 54 | class CascadeStages {
|
---|
| 55 | public:
|
---|
| 56 | CascadeStages(int MaxStages) {
|
---|
| 57 | this->MaxStages = MaxStages;
|
---|
| 58 | m_stages =
|
---|
| 59 | (Iir::Cascade::Stage *)malloc(MaxStages * sizeof(Iir::Cascade::Stage));
|
---|
| 60 | m_states =
|
---|
| 61 | (Iir::DirectFormII *)malloc(MaxStages * sizeof(Iir::DirectFormII));
|
---|
| 62 | }
|
---|
| 63 | ~CascadeStages() {
|
---|
| 64 | free(m_stages);
|
---|
| 65 | free(m_states);
|
---|
| 66 | }
|
---|
| 67 | void reset() {
|
---|
| 68 | Iir::DirectFormII *state = m_states;
|
---|
| 69 | for (int i = MaxStages; --i >= 0; ++state)
|
---|
| 70 | state->reset();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | template <typename Sample> inline Sample filter(const Sample in) {
|
---|
| 74 | double out = in;
|
---|
| 75 | Iir::DirectFormII *state = m_states;
|
---|
| 76 | Iir::Biquad const *stage = m_stages;
|
---|
| 77 | for (int i = MaxStages; --i >= 0; ++state, ++stage)
|
---|
| 78 | out = state->process1(out, *stage);
|
---|
| 79 | return static_cast<Sample>(out);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | Iir::Cascade::Storage getCascadeStorage() {
|
---|
| 83 | return Iir::Cascade::Storage(MaxStages, m_stages);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | private:
|
---|
| 87 | int MaxStages;
|
---|
| 88 | Iir::Cascade::Stage *m_stages;
|
---|
| 89 | Iir::DirectFormII *m_states;
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 | // de-templatized for pimpl idom
|
---|
| 93 | // comes from iir from Bernd Porr
|
---|
| 94 | class PoleFilter : Iir::Butterworth::LowPassBase, public CascadeStages {
|
---|
| 95 | public:
|
---|
| 96 | PoleFilter(int MaxPoles) : CascadeStages((MaxPoles + 1) / 2) {
|
---|
| 97 | this->MaxPoles = MaxPoles;
|
---|
| 98 | m_analogStorage = new LayoutStorage(MaxPoles);
|
---|
| 99 | m_digitalStorage = new LayoutStorage(MaxPoles);
|
---|
| 100 | // This glues together the factored base classes
|
---|
| 101 | // with the templatized storage classes.
|
---|
| 102 | Iir::Butterworth::LowPassBase::setCascadeStorage(this->getCascadeStorage());
|
---|
| 103 | Iir::Butterworth::LowPassBase::setPrototypeStorage(*m_analogStorage,
|
---|
| 104 | *m_digitalStorage);
|
---|
| 105 | }
|
---|
| 106 | ~PoleFilter() {
|
---|
| 107 | delete m_analogStorage;
|
---|
| 108 | delete m_digitalStorage;
|
---|
| 109 | }
|
---|
| 110 | void setup(double sampleRate, double cutoffFrequency) {
|
---|
| 111 | Iir::Butterworth::LowPassBase::setup(MaxPoles, sampleRate, cutoffFrequency);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | private:
|
---|
| 115 | int MaxPoles;
|
---|
| 116 | LayoutStorage *m_analogStorage;
|
---|
| 117 | LayoutStorage *m_digitalStorage;
|
---|
| 118 | };
|
---|
| 119 |
|
---|
| 120 | class ButterworthLowPass_impl {
|
---|
| 121 | public:
|
---|
| 122 | ButterworthLowPass_impl(const flair::filter::ButterworthLowPass *self,
|
---|
| 123 | const flair::gui::LayoutPosition *position,
|
---|
| 124 | std::string name, uint32_t order,uint32_t nbRow,uint32_t nbCol);
|
---|
| 125 | ~ButterworthLowPass_impl();
|
---|
| 126 | void UpdateFrom(const flair::core::io_data *data);
|
---|
| 127 | flair::core::Matrix *output;
|
---|
| 128 |
|
---|
| 129 | private:
|
---|
| 130 | void settingsChanged(PoleFilter*f,float inputValue);
|
---|
| 131 | flair::gui::DoubleSpinBox *cutoff, *T;
|
---|
| 132 | PoleFilter **f;
|
---|
| 133 | uint32_t order;
|
---|
| 134 | uint32_t nbRow, nbCol;
|
---|
| 135 | const flair::filter::ButterworthLowPass *self;
|
---|
| 136 | };
|
---|
| 137 |
|
---|
| 138 | #endif // BUTTERWORTHLOWPASS_H_IMPL_H
|
---|