| 1 | #ifndef _Function_h_ |
| 2 | #define _Function_h_ |
| 3 | /* Function.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2012,2015-2018,2020,2023-2025 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "Collection.h" |
| 22 | |
| 23 | #include "Function_def.h" |
| 24 | |
| 25 | Collection_define (FunctionList, OrderedOf, Function) { |
| 26 | }; |
| 27 | |
| 28 | /* |
| 29 | An object of type Function represents a function f (x, ...) on the domain [xmin, xmax] * .... |
| 30 | Class invariants: |
| 31 | xmax >= xmin; |
| 32 | xmin, xmax are constant; |
| 33 | */ |
| 34 | |
| 35 | void Function_init (Function me, double xmin, double xmax); |
| 36 | /* |
| 37 | Preconditions: |
| 38 | xmin <= xmax; |
| 39 | Postconditions: |
| 40 | result -> xmin == xmin; |
| 41 | result -> xmax == xmax; |
| 42 | */ |
| 43 | |
| 44 | int Function_getDomainQuantity (constFunction me); // as input for MelderQuantity_getXXX |
| 45 | |
| 46 | /* |
| 47 | A function value is often expressed in some unit, such as: |
| 48 | Pa, Hz, dB, sones. |
| 49 | In the following, `ilevel` is for multidimensional functions; it could be the row number of a matrix, |
| 50 | or pitch height (Hz) vs. pitch strength (dimensionless), and so on. |
| 51 | `unit` is an enumerated type that has to be defined in the header files of the descendant class, |
| 52 | starting from 0, which should be the default unit; e.g. for pitch: 0 = Hz, 1 = logHz, 2 = semitones, 3 = mel. |
| 53 | */ |
| 54 | int Function_getMinimumUnit (constFunction me, integer ilevel); |
| 55 | int Function_getMaximumUnit (constFunction me, integer ilevel); |
| 56 | |
| 57 | #define Function_UNIT_TEXT_SHORT 0x0000'0001 |
| 58 | #define Function_UNIT_TEXT_GRAPHICAL 0x0000'0002 |
| 59 | #define Function_UNIT_TEXT_MENU 0x0000'0004 |
| 60 | conststring32 Function_getUnitText (constFunction me, integer level, int unit, uint32 flags); |
| 61 | |
| 62 | bool Function_isUnitLogarithmic (constFunction me, integer level, int unit); |
| 63 | |
| 64 | double Function_convertStandardToSpecialUnit (constFunction me, double value, integer level, int unit); |
| 65 | double Function_convertSpecialToStandardUnit (constFunction me, double value, integer level, int unit); |
| 66 | double Function_convertToNonlogarithmic (constFunction me, double value, integer level, int unit); |
| 67 | |
| 68 | /* |
| 69 | The domain of a function can be changed by windowing. |
| 70 | Here follow some window functions. |
| 71 | */ |
| 72 | |
| 73 | #define Function_RECTANGULAR 0 |
| 74 | #define Function_TRIANGULAR 1 |
| 75 | #define Function_PARABOLIC 2 |
| 76 | #define Function_HANNING 3 |
| 77 | #define Function_HAMMING 4 |
| 78 | #define Function_POTTER 5 |
| 79 | #define Function_KAISER12 6 |
| 80 | #define Function_KAISER20 7 |
| 81 | #define Function_GAUSSIAN 8 |
| 82 | |
| 83 | double Function_window (double tim, int windowType); |
| 84 | /* |
| 85 | Return value: |
| 86 | a number between 0 and 1, zero outside the "domain": |
| 87 | domain [-0.5, 0.5]: rectangular, triangular, parabolic, hanning, hamming; |
| 88 | domain [-0.77, 0.77]: potter, kaiser12; |
| 89 | domain [-1, 1]: kaiser20; |
| 90 | domain [-inf, +inf]: gaussian. |
| 91 | Rectangular: 1; |
| 92 | Triangular ("Parzen"): 1 - tim / 0.5 |
| 93 | Parabolic ("Welch"): 1 - (tim / 0.5) ^ 2 |
| 94 | Hanning (raised cosine): 0.5 + 0.5 * cos (2 * pi * tim) |
| 95 | Hamming (raised cosine): 0.54 + 0.46 * cos (2 * pi * tim) |
| 96 | Gaussian: exp (- (pi * tim) ^ 2) |
| 97 | Kaiser12: besselI0 (12 * sqrt (1 - (tim / 0.77) ^ 2)) / besselI0 (12) |
| 98 | Kaiser20: besselI0 (20.24 * sqrt (1 - tim ^ 2)) / besselI0 (20.24) |
| 99 | Usage: |
| 100 | the preferred window for almost every application is Kaiser20; |
| 101 | its only shortcoming is the usual double computation time. |
| 102 | Properties: |
| 103 | Highest sidelobe: |
| 104 | Rectangular: -10 dB |
| 105 | Hanning: -30 dB |
| 106 | Hamming: -40 dB |
| 107 | Potter, Kaiser12: -90 dB |
| 108 | Kaiser20: -180 dB |
| 109 | Gaussian: -inf dB |
| 110 | Height at 'tim' is +0.5 or -0.5 (limit from below): |
| 111 | Rectangular: 1 |
| 112 | Triangular, Parabolic, Hanning: 0 |
| 113 | Hamming: 0.08 |
| 114 | Gaussian: 0.085 |
| 115 | Kaiser20: 0.0715 |
| 116 | Potter: ... |
| 117 | Area: |
| 118 | Rectangular: 1 |
| 119 | Parabolic: 2/3 |
| 120 | Gaussian: 1 / sqrt (pi) |
| 121 | Kaiser20: 0.9813318115591122859 / sqrt (pi) |
| 122 | Hamming: 0.54 |
| 123 | Triangular, Hanning: 1/2 |
| 124 | Bandwidth (-20 dB): |
| 125 | ... |
| 126 | */ |
| 127 | |
| 128 | /* |
| 129 | Procedures to adapt a range to the extent of the function domain. |
| 130 | */ |
| 131 | void Function_unidirectionalAutowindow ( |
| 132 | constFunction me, |
| 133 | double *xmin, |
| 134 | double *xmax |
| 135 | ); |
| 136 | void Function_bidirectionalAutowindow ( |
| 137 | constFunction me, |
| 138 | double *x1, |
| 139 | double *x2 |
| 140 | ); |
| 141 | bool Function_intersectRangeWithDomain ( |
| 142 | constFunction me, |
| 143 | double *x1, |
| 144 | double *x2 |
| 145 | ); |
| 146 | |
| 147 | void Function_shiftXBy ( |
| 148 | mutableFunction me, |
| 149 | double shift |
| 150 | ); |
| 151 | void Function_shiftXTo (mutableFunction me, double xfrom, double xto); |
| 152 | void Function_scaleXBy (mutableFunction me, double factor); |
| 153 | void Function_scaleXTo (mutableFunction me, double xminto, double xmaxto); |
| 154 | |
| 155 | /* End of file Function.h */ |
| 156 | #endif |