| 1 |
// ---------------------------------------------------------------------------- |
| 2 |
// |
| 3 |
// Copyright (C) 2003-2013 Fons Adriaensen <[email protected]> |
| 4 |
// |
| 5 |
// This program 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 3 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// This program is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// 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 program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
// |
| 18 |
// ---------------------------------------------------------------------------- |
| 19 |
|
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <math.h> |
| 24 |
#include "invfilt.h" |
| 25 |
|
| 26 |
|
| 27 |
#define FPI 3.141592f |
| 28 |
|
| 29 |
|
| 30 |
Invfilt::Invfilt (void) : |
| 31 |
_lfft (0), |
| 32 |
_targ (0), |
| 33 |
_bias (0), |
| 34 |
_fwd_plan (0), |
| 35 |
_rev_plan (0) |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
Invfilt::~Invfilt (void) |
| 41 |
{ |
| 42 |
fini (); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
void Invfilt::fini (void) |
| 47 |
{ |
| 48 |
delete[] _td1; |
| 49 |
delete[] _td2; |
| 50 |
delete[] _fd1; |
| 51 |
delete[] _fd2; |
| 52 |
delete[] _targ; |
| 53 |
delete[] _bias; |
| 54 |
_targ = 0; |
| 55 |
_bias = 0; |
| 56 |
if (_fwd_plan) fftwf_destroy_plan (_fwd_plan); |
| 57 |
if (_rev_plan) fftwf_destroy_plan (_rev_plan); |
| 58 |
_fwd_plan = 0; |
| 59 |
_rev_plan = 0; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
void Invfilt::init (float rate, float tmax) |
| 64 |
{ |
| 65 |
int n1, n2; |
| 66 |
|
| 67 |
_rate = rate; |
| 68 |
_tmax = tmax; |
| 69 |
n1 = (int)(2 * rate * tmax + 0.5f); |
| 70 |
for (n2 = 256; n2 < n1; n2 <<= 1); |
| 71 |
if (n2 != _lfft) |
| 72 |
{ |
| 73 |
fini (); |
| 74 |
_lfft = n2; |
| 75 |
_td1 = (float *) fftwf_malloc (_lfft * sizeof (float)); |
| 76 |
_td2 = (float *) fftwf_malloc (_lfft * sizeof (float)); |
| 77 |
_fd1 = (fftwf_complex *) fftwf_malloc ((_lfft / 2 + 1) * sizeof (fftwf_complex)); |
| 78 |
_fd2 = (fftwf_complex *) fftwf_malloc ((_lfft / 2 + 1) * sizeof (fftwf_complex)); |
| 79 |
_fwd_plan = fftwf_plan_dft_r2c_1d (_lfft, _td1, _fd1, FFTW_ESTIMATE); |
| 80 |
_rev_plan = fftwf_plan_dft_c2r_1d (_lfft, _fd1, _td1, FFTW_ESTIMATE); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
void Invfilt::setwin (float tneg, float thif, float tlof) |
| 86 |
{ |
| 87 |
_tneg = tneg; |
| 88 |
_thif = thif; |
| 89 |
_tlof = tlof; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
void Invfilt::settarg_log (float *v, int n, float f0, float f1) |
| 94 |
{ |
| 95 |
int k; |
| 96 |
float r; |
| 97 |
|
| 98 |
if (v) |
| 99 |
{ |
| 100 |
k = _lfft / 2 + 1; |
| 101 |
if (! _targ) _targ = new float [k]; |
| 102 |
r = log (f1 / f0); |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
delete[] _targ; |
| 107 |
_targ = 0; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
void Invfilt::setbias_log (float *v, int n, float f0, float f1) |
| 113 |
{ |
| 114 |
int k; |
| 115 |
float r; |
| 116 |
|
| 117 |
if (v) |
| 118 |
{ |
| 119 |
k = _lfft / 2 + 1; |
| 120 |
if (! _bias) _bias = new float [k]; |
| 121 |
r = log (f1 / f0); |
| 122 |
} |
| 123 |
else |
| 124 |
{ |
| 125 |
delete[] _bias; |
| 126 |
_bias = 0; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
float Invfilt::rcoswin (float x, float p) |
| 132 |
{ |
| 133 |
x = fabsf (x); |
| 134 |
if (x >= 1.0f) return 0; |
| 135 |
return powf (0.5f * (1 + cosf (FPI * x)), p); |
| 136 |
} |
| 137 |
|