| 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"cxx . . $ `<file>/dwtools/Resonator.h`/ |
| 26 | |
| 27 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Filter, Daata, 0); |
| 28 | |
| 29 | void structFiltercxx . . $ structFilter# :: v_resetMemorycxx . . $ structFilter#v_resetMemory(49f6e7a06ebc5aa8). () { |
| 30 | p1cxx . . $ structFilter#p1. = p2cxx . . $ structFilter#p2. = 0.0; |
| 31 | } |
| 32 | |
| 33 | void structFiltercxx . . $ structFilter# :: v_setCoefficientscxx . . $ structFilter#v_setCoefficients(bc12c9ec15586e6a). (double frequency, double bandwidth) { |
| 34 | const double r = expcxx . . $ exp(fee4f1d22bc8e617). (-NUMpicxx . . $ NUMpi. * samplingPeriodcxx . . $ structFilter#samplingPeriod. * bandwidth); |
| 35 | ccxx . . $ structFilter#c. = -(r * r); |
| 36 | bcxx . . $ structFilter#b. = 2.0 * r * coscxx . . $ cos(fee4f1d22bc8e617). (2.0 * NUMpicxx . . $ NUMpi. * frequency * samplingPeriodcxx . . $ structFilter#samplingPeriod.); |
| 37 | acxx . . $ structFilter#a. = 1.0 - bcxx . . $ structFilter#b. - ccxx . . $ structFilter#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 structFiltercxx . . $ structFilter# :: v_getOutputcxx . . $ structFilter#v_getOutput(447c67c6676f75c3). (double input) { |
| 44 | double output = acxx . . $ structFilter#a. * input + bcxx . . $ structFilter#b. * p1cxx . . $ structFilter#p1. + ccxx . . $ structFilter#c. * p2cxx . . $ structFilter#p2.; |
| 45 | p2cxx . . $ structFilter#p2. = p1cxx . . $ structFilter#p1.; |
| 46 | p1cxx . . $ structFilter#p1. = output; |
| 47 | return output; |
| 48 | } |
| 49 | |
| 50 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Resonator, Filter, 0); |
| 51 | |
| 52 | void structResonatorcxx . . $ structResonator# :: v_setCoefficientscxx . . $ structResonator#v_setCoefficients(bc12c9ec15586e6a). (double frequency, double bandwidth) { |
| 53 | structFiltercxx . . $ structFilter# :: v_setCoefficientscxx . . $ structFilter#v_setCoefficients(bc12c9ec15586e6a). (frequency, bandwidth); |
| 54 | if (! normaliseAtDCcxx . . $ structResonator#normaliseAtDC.) |
| 55 | acxx . . $ structFilter#a. = (1.0 + ccxx . . $ structFilter#c.) * sincxx . . $ sin(fee4f1d22bc8e617). (2.0 * NUMpicxx . . $ NUMpi. * frequency * samplingPeriodcxx . . $ structFilter#samplingPeriod.); // normalization: H(f) = 0 dB |
| 56 | } |
| 57 | |
| 58 | autoResonatorcxx . . $ autoResonator# Resonator_createcxx . . $ Resonator_create(15e8c0c410711d65). (double samplingPeriod, bool normaliseAtDC) { |
| 59 | try { |
| 60 | autoResonatorcxx . . $ autoResonator# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Resonator); |
| 61 | mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structFilter#a. = 1.0; // all-pass |
| 62 | mycxx . . $ `melder/melder.h:44:9`! samplingPeriodcxx . . $ structFilter#samplingPeriod. = samplingPeriod; |
| 63 | mycxx . . $ `melder/melder.h:44:9`! normaliseAtDCcxx . . $ structResonator#normaliseAtDC. = normaliseAtDC; |
| 64 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 65 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 66 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Resonator not created."); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (AntiResonator, Filter, 0); |
| 71 | |
| 72 | void structAntiResonatorcxx . . $ structAntiResonator# :: v_setCoefficientscxx . . $ structAntiResonator#v_setCoefficients(bc12c9ec15586e6a). (double frequency, double bandwidth) { |
| 73 | if (frequency <= 0.0 && bandwidth <= 0.0) { |
| 74 | acxx . . $ structFilter#a. = 1.0; |
| 75 | bcxx . . $ structFilter#b. = -2.0; |
| 76 | ccxx . . $ structFilter#c. = 1.0; // all-pass except dc |
| 77 | } else { |
| 78 | structFiltercxx . . $ structFilter# :: v_setCoefficientscxx . . $ structFilter#v_setCoefficients(bc12c9ec15586e6a). (frequency, bandwidth); |
| 79 | acxx . . $ structFilter#a. = 1.0 / (1.0 - bcxx . . $ structFilter#b. - ccxx . . $ structFilter#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 structAntiResonatorcxx . . $ structAntiResonator# :: v_getOutputcxx . . $ structAntiResonator#v_getOutput(447c67c6676f75c3). (double input) { |
| 88 | const double output = acxx . . $ structFilter#a. * (input - bcxx . . $ structFilter#b. * p1cxx . . $ structFilter#p1. - ccxx . . $ structFilter#c. * p2cxx . . $ structFilter#p2.); |
| 89 | p2cxx . . $ structFilter#p2. = p1cxx . . $ structFilter#p1.; |
| 90 | p1cxx . . $ structFilter#p1. = input; |
| 91 | return output; |
| 92 | } |
| 93 | |
| 94 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (ConstantGainResonator, Filter, 0); |
| 95 | |
| 96 | void structConstantGainResonatorcxx . . $ structConstantGainResonator# :: v_resetMemorycxx . . $ structConstantGainResonator#v_resetMemory(49f6e7a06ebc5aa8). () { |
| 97 | p1cxx . . $ structFilter#p1. = p2cxx . . $ structFilter#p2. = p3cxx . . $ structConstantGainResonator#p3. = p4cxx . . $ structConstantGainResonator#p4. = 0.0; |
| 98 | } |
| 99 | |
| 100 | void structConstantGainResonatorcxx . . $ structConstantGainResonator# :: v_setCoefficientscxx . . $ structConstantGainResonator#v_setCoefficients(bc12c9ec15586e6a). (double frequency, double bandwidth) { |
| 101 | structFiltercxx . . $ structFilter# :: v_setCoefficientscxx . . $ structFilter#v_setCoefficients(bc12c9ec15586e6a). (frequency, bandwidth); |
| 102 | const double r = expcxx . . $ exp(fee4f1d22bc8e617). (-NUMpicxx . . $ NUMpi. * samplingPeriodcxx . . $ structFilter#samplingPeriod. * bandwidth); |
| 103 | acxx . . $ structFilter#a. = 1.0 - r; |
| 104 | dcxx . . $ structConstantGainResonator#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 structConstantGainResonatorcxx . . $ structConstantGainResonator# :: v_getOutputcxx . . $ structConstantGainResonator#v_getOutput(447c67c6676f75c3). (double input) { |
| 111 | const double output = acxx . . $ structFilter#a. * (input + dcxx . . $ structConstantGainResonator#d. * p4cxx . . $ structConstantGainResonator#p4.) + bcxx . . $ structFilter#b. * p1cxx . . $ structFilter#p1. + ccxx . . $ structFilter#c. * p2cxx . . $ structFilter#p2.; |
| 112 | p2cxx . . $ structFilter#p2. = p1cxx . . $ structFilter#p1.; |
| 113 | p1cxx . . $ structFilter#p1. = output; |
| 114 | p4cxx . . $ structConstantGainResonator#p4. = p3cxx . . $ structConstantGainResonator#p3.; |
| 115 | p3cxx . . $ structConstantGainResonator#p3. = input; |
| 116 | return output; |
| 117 | } |
| 118 | |
| 119 | autoConstantGainResonatorcxx . . $ autoConstantGainResonator# ConstantGainResonator_createcxx . . $ ConstantGainResonator_create(ab00f84e06522bfd). (double samplingPeriod) { |
| 120 | try { |
| 121 | autoConstantGainResonatorcxx . . $ autoConstantGainResonator# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (ConstantGainResonator); |
| 122 | mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structFilter#a. = 1.0; // all-pass |
| 123 | mycxx . . $ `melder/melder.h:44:9`! samplingPeriodcxx . . $ structFilter#samplingPeriod. = samplingPeriod; |
| 124 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 125 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 126 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"ConstantGainResonator not created."); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | autoAntiResonatorcxx . . $ autoAntiResonator# AntiResonator_createcxx . . $ AntiResonator_create(473e4a3e2f0be5f0). (double samplingPeriod) { |
| 131 | try { |
| 132 | autoAntiResonatorcxx . . $ autoAntiResonator# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (AntiResonator); |
| 133 | mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structFilter#a. = 1.0; // all-pass |
| 134 | mycxx . . $ `melder/melder.h:44:9`! samplingPeriodcxx . . $ structFilter#samplingPeriod. = samplingPeriod; |
| 135 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 136 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 137 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"AntiResonator not created."); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void Filter_setCoefficientscxx . . $ Filter_setCoefficients(8c9469244f9d62e9). (Filtercxx . . $ Filter# me, double frequency, double bandwidth) { |
| 142 | mycxx . . $ `melder/melder.h:44:9`! v_setCoefficientscxx . . $ structFilter#v_setCoefficients(bc12c9ec15586e6a). (frequency, bandwidth); |
| 143 | } |
| 144 | |
| 145 | double Filter_getOutputcxx . . $ Filter_getOutput(3b7ebd8f53a54cf6). (Filtercxx . . $ Filter# me, double input) { |
| 146 | return mycxx . . $ `melder/melder.h:44:9`! v_getOutputcxx . . $ structFilter#v_getOutput(447c67c6676f75c3). (input); |
| 147 | } |
| 148 | |
| 149 | void Filter_resetMemorycxx . . $ Filter_resetMemory(ab2cc5e70e4bc2ca). (Filtercxx . . $ Filter# me) { |
| 150 | mycxx . . $ `melder/melder.h:44:9`! v_resetMemorycxx . . $ structFilter#v_resetMemory(49f6e7a06ebc5aa8). (); |
| 151 | } |
| 152 | |
| 153 | /* End of file Resonator.cpp */ |