| 1 | /* Resonator.cpp |
| 2 | * |
| 3 | * Copyright (C) 2008-2019 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * djmw 20081029 |
| 21 | * djmw 20081124 +ConstantGainResonator |
| 22 | * djmw 20110304 Thing_new |
| 23 | */ |
| 24 | |
| 25 | #include "Resonator.h" |
| 26 | |
| 27 | Thing_implement (Filter, Daata, 0); |
| 28 | |
| 29 | void structFilter :: v_resetMemory () { |
| 30 | p1 = p2 = 0.0; |
| 31 | } |
| 32 | |
| 33 | void structFilter :: v_setCoefficients (double frequency, double bandwidth) { |
| 34 | const double r = exp (-NUMpi * samplingPeriod * bandwidth); |
| 35 | c = -(r * r); |
| 36 | b = 2.0 * r * cos (2.0 * NUMpi * frequency * samplingPeriod); |
| 37 | a = 1.0 - b - c; // normalization: H(0) = 0 dB |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | y [n] = a * x [n] + b * y [n-1] + c * y [n-2] |
| 42 | */ |
| 43 | double structFilter :: v_getOutput (double input) { |
| 44 | double output = a * input + b * p1 + c * p2; |
| 45 | p2 = p1; |
| 46 | p1 = output; |
| 47 | return output; |
| 48 | } |
| 49 | |
| 50 | Thing_implement (Resonator, Filter, 0); |
| 51 | |
| 52 | void structResonator :: v_setCoefficients (double frequency, double bandwidth) { |
| 53 | structFilter :: v_setCoefficients (frequency, bandwidth); |
| 54 | if (! normaliseAtDC) |
| 55 | a = (1.0 + c) * sin (2.0 * NUMpi * frequency * samplingPeriod); // normalization: H(f) = 0 dB |
| 56 | } |
| 57 | |
| 58 | autoResonator Resonator_create (double samplingPeriod, bool normaliseAtDC) { |
| 59 | try { |
| 60 | autoResonator me = Thing_new (Resonator); |
| 61 | my a = 1.0; // all-pass |
| 62 | my samplingPeriod = samplingPeriod; |
| 63 | my normaliseAtDC = normaliseAtDC; |
| 64 | return me; |
| 65 | } catch (MelderError) { |
| 66 | Melder_throw (U"Resonator not created."); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | Thing_implement (AntiResonator, Filter, 0); |
| 71 | |
| 72 | void structAntiResonator :: v_setCoefficients (double frequency, double bandwidth) { |
| 73 | if (frequency <= 0.0 && bandwidth <= 0.0) { |
| 74 | a = 1.0; |
| 75 | b = -2.0; |
| 76 | c = 1.0; // all-pass except dc |
| 77 | } else { |
| 78 | structFilter :: v_setCoefficients (frequency, bandwidth); |
| 79 | a = 1.0 / (1.0 - b - c); |
| 80 | //c *= - a; b *= - a; incorporated in the getOutput function |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | y [n] = a * (x [n] - b * x [n-1] - c * x [n-2]) |
| 86 | */ |
| 87 | double structAntiResonator :: v_getOutput (double input) { |
| 88 | const double output = a * (input - b * p1 - c * p2); |
| 89 | p2 = p1; |
| 90 | p1 = input; |
| 91 | return output; |
| 92 | } |
| 93 | |
| 94 | Thing_implement (ConstantGainResonator, Filter, 0); |
| 95 | |
| 96 | void structConstantGainResonator :: v_resetMemory () { |
| 97 | p1 = p2 = p3 = p4 = 0.0; |
| 98 | } |
| 99 | |
| 100 | void structConstantGainResonator :: v_setCoefficients (double frequency, double bandwidth) { |
| 101 | structFilter :: v_setCoefficients (frequency, bandwidth); |
| 102 | const double r = exp (-NUMpi * samplingPeriod * bandwidth); |
| 103 | a = 1.0 - r; |
| 104 | d = -r; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | y [n] = a * (x [n] + d * x [n-2]) + b * y [n-1] + c * y [n-2] |
| 109 | */ |
| 110 | double structConstantGainResonator :: v_getOutput (double input) { |
| 111 | const double output = a * (input + d * p4) + b * p1 + c * p2; |
| 112 | p2 = p1; |
| 113 | p1 = output; |
| 114 | p4 = p3; |
| 115 | p3 = input; |
| 116 | return output; |
| 117 | } |
| 118 | |
| 119 | autoConstantGainResonator ConstantGainResonator_create (double samplingPeriod) { |
| 120 | try { |
| 121 | autoConstantGainResonator me = Thing_new (ConstantGainResonator); |
| 122 | my a = 1.0; // all-pass |
| 123 | my samplingPeriod = samplingPeriod; |
| 124 | return me; |
| 125 | } catch (MelderError) { |
| 126 | Melder_throw (U"ConstantGainResonator not created."); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | autoAntiResonator AntiResonator_create (double samplingPeriod) { |
| 131 | try { |
| 132 | autoAntiResonator me = Thing_new (AntiResonator); |
| 133 | my a = 1.0; // all-pass |
| 134 | my samplingPeriod = samplingPeriod; |
| 135 | return me; |
| 136 | } catch (MelderError) { |
| 137 | Melder_throw (U"AntiResonator not created."); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void Filter_setCoefficients (Filter me, double frequency, double bandwidth) { |
| 142 | my v_setCoefficients (frequency, bandwidth); |
| 143 | } |
| 144 | |
| 145 | double Filter_getOutput (Filter me, double input) { |
| 146 | return my v_getOutput (input); |
| 147 | } |
| 148 | |
| 149 | void Filter_resetMemory (Filter me) { |
| 150 | my v_resetMemory (); |
| 151 | } |
| 152 | |
| 153 | /* End of file Resonator.cpp */ |