| 1 | /* NUMfilter.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2008,2011,2012,2014-2020 Paul Boersma |
| 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 | #include "melder.h" |
| 20 | |
| 21 | void NUMfbtoa (double formant, double bandwidth, double dt, double *a1, double *a2) { |
| 22 | *a1 = 2.0 * exp (- NUMpi * bandwidth * dt) * cos (2.0 * NUMpi * formant * dt); |
| 23 | *a2 = exp (- 2.0 * NUMpi * bandwidth * dt); |
| 24 | } |
| 25 | |
| 26 | void VECfilterSecondOrderSection_a_inplace (VECVU const& x, double a1, double a2) { |
| 27 | x [2] += a1 * x [1]; |
| 28 | for (integer i = 3; i <= x.size; i ++) |
| 29 | x [i] += a1 * x [i - 1] - a2 * x [i - 2]; |
| 30 | } |
| 31 | |
| 32 | void VECfilterSecondOrderSection_fb_inplace (VECVU const& x, double dt, double formant, double bandwidth) { |
| 33 | double a1, a2; |
| 34 | NUMfbtoa (formant, bandwidth, dt, & a1, & a2); |
| 35 | VECfilterSecondOrderSection_a_inplace (x, a1, a2); |
| 36 | } |
| 37 | |
| 38 | double NUMftopreemphasis (double f, double dt) { |
| 39 | return exp (- 2.0 * NUMpi * f * dt); |
| 40 | } |
| 41 | |
| 42 | void VECpreemphasize_a_inplace (VECVU const& x, double preemphasis) { |
| 43 | for (integer i = x.size; i >= 2; i --) |
| 44 | x [i] -= preemphasis * x [i - 1]; |
| 45 | } |
| 46 | |
| 47 | void VECdeemphasize_a_inplace (VECVU const& x, double preemphasis) { |
| 48 | for (integer i = 2; i <= x.size; i ++) |
| 49 | x [i] += preemphasis * x [i - 1]; |
| 50 | } |
| 51 | |
| 52 | void VECpreemphasize_f_inplace (VECVU const& x, double dt, double frequency) { |
| 53 | VECpreemphasize_a_inplace (x, NUMftopreemphasis (frequency, dt)); |
| 54 | } |
| 55 | |
| 56 | void VECdeemphasize_f_inplace (VECVU const& x, double dt, double frequency) { |
| 57 | VECdeemphasize_a_inplace (x, NUMftopreemphasis (frequency, dt)); |
| 58 | } |
| 59 | |
| 60 | /* End of file NUMfilter.cpp */ |