| 1 | /* Function.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2012,2015-2018,2020,2022-2025 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 3 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 "Function.h" |
| 20 | |
| 21 | #include "oo_DESTROY.h" |
| 22 | #include "Function_def.h" |
| 23 | #include "oo_COPY.h" |
| 24 | #include "Function_def.h" |
| 25 | #include "oo_EQUAL.h" |
| 26 | #include "Function_def.h" |
| 27 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 28 | #include "Function_def.h" |
| 29 | #include "oo_WRITE_TEXT.h" |
| 30 | #include "Function_def.h" |
| 31 | #include "oo_READ_TEXT.h" |
| 32 | #include "Function_def.h" |
| 33 | #include "oo_WRITE_BINARY.h" |
| 34 | #include "Function_def.h" |
| 35 | #include "oo_READ_BINARY.h" |
| 36 | #include "Function_def.h" |
| 37 | #include "oo_DESCRIPTION.h" |
| 38 | #include "Function_def.h" |
| 39 | |
| 40 | Thing_implement (Function, Daata, 0); |
| 41 | |
| 42 | Thing_implement (FunctionList, Ordered, 0); |
| 43 | |
| 44 | void structFunction :: v1_info () { |
| 45 | Function_Parent :: v1_info (); |
| 46 | MelderInfo_writeLine (U"Domain:"); |
| 47 | MelderInfo_writeLine (U" xmin: ", xmin); |
| 48 | MelderInfo_writeLine (U" xmax: ", xmax); |
| 49 | } |
| 50 | |
| 51 | void structFunction :: v_shiftX (double xfrom, double xto) { |
| 52 | NUMshift (& xmin, xfrom, xto); |
| 53 | NUMshift (& xmax, xfrom, xto); |
| 54 | } |
| 55 | |
| 56 | void structFunction :: v_scaleX (double xminfrom, double xmaxfrom, double xminto, double xmaxto) { |
| 57 | NUMscale (& xmin, xminfrom, xmaxfrom, xminto, xmaxto); |
| 58 | NUMscale (& xmax, xminfrom, xmaxfrom, xminto, xmaxto); |
| 59 | } |
| 60 | |
| 61 | void Function_init (mutableFunction me, double xmin_, double xmax_) { |
| 62 | my xmin = xmin_; |
| 63 | my xmax = xmax_; |
| 64 | } |
| 65 | |
| 66 | int Function_getMinimumUnit (constFunction me, integer level) { |
| 67 | return my v_getMinimumUnit (level); |
| 68 | } |
| 69 | |
| 70 | int Function_getMaximumUnit (constFunction me, integer level) { |
| 71 | return my v_getMaximumUnit (level); |
| 72 | } |
| 73 | |
| 74 | int Function_getDomainQuantity (constFunction me) { |
| 75 | return my v_domainQuantity (); |
| 76 | } |
| 77 | |
| 78 | conststring32 Function_getUnitText (constFunction me, integer level, int unit, uint32 flags) { |
| 79 | Melder_assert (unit >= my v_getMinimumUnit (level) && unit <= my v_getMaximumUnit (level)); |
| 80 | return my v_getUnitText (level, unit, flags); |
| 81 | } |
| 82 | |
| 83 | bool Function_isUnitLogarithmic (constFunction me, integer level, int unit) { |
| 84 | Melder_assert (unit >= my v_getMinimumUnit (level) && unit <= my v_getMaximumUnit (level)); |
| 85 | return my v_isUnitLogarithmic (level, unit); |
| 86 | } |
| 87 | |
| 88 | double Function_convertStandardToSpecialUnit (constFunction me, double value, integer level, int unit) { |
| 89 | return isdefined (value) ? my v_convertStandardToSpecialUnit (value, level, unit) : undefined; |
| 90 | } |
| 91 | |
| 92 | double Function_convertSpecialToStandardUnit (constFunction me, double value, integer level, int unit) { |
| 93 | return isdefined (value) ? my v_convertSpecialToStandardUnit (value, level, unit) : undefined; |
| 94 | } |
| 95 | |
| 96 | double Function_convertToNonlogarithmic (constFunction me, double value, integer level, int unit) { |
| 97 | return isdefined (value) && my v_isUnitLogarithmic (level, unit) ? pow (10.0, value) : value; |
| 98 | } |
| 99 | |
| 100 | void Function_shiftXBy (const mutableFunction me, const double shift) { |
| 101 | my v_shiftX (0.0, shift); |
| 102 | } |
| 103 | |
| 104 | void Function_shiftXTo (const mutableFunction me, const double xfrom, const double xto) { |
| 105 | my v_shiftX (xfrom, xto); |
| 106 | } |
| 107 | |
| 108 | void Function_scaleXBy (const mutableFunction me, const double factor) { |
| 109 | my v_scaleX (0.0, 1.0, 0.0, factor); |
| 110 | } |
| 111 | |
| 112 | void Function_scaleXTo (const mutableFunction me, double xminto, double xmaxto) { |
| 113 | my v_scaleX (my xmin, my xmax, xminto, xmaxto); |
| 114 | } |
| 115 | |
| 116 | double Function_window (double tim, int windowType) { |
| 117 | static double one_by_bessi_0_12, one_by_bessi_0_20; |
| 118 | switch (windowType) { |
| 119 | case Function_RECTANGULAR: |
| 120 | if (tim < -0.5 || tim > 0.5) return 0.0; |
| 121 | return 1; |
| 122 | case Function_TRIANGULAR: |
| 123 | if (tim < -0.5 || tim > 0.5) return 0.0; |
| 124 | return 1 - tim - tim; |
| 125 | case Function_PARABOLIC: |
| 126 | if (tim < -0.5 || tim > 0.5) return 0.0; |
| 127 | return 1 - 4 * tim * tim; |
| 128 | case Function_HANNING: |
| 129 | if (tim < -0.5 || tim > 0.5) return 0.0; |
| 130 | return 0.5 + 0.5 * cos (2 * NUMpi * tim); |
| 131 | case Function_HAMMING: |
| 132 | if (tim < -0.5 || tim > 0.5) return 0.0; |
| 133 | return 0.54 + 0.46 * cos (2 * NUMpi * tim); |
| 134 | case Function_POTTER: |
| 135 | if (tim < -0.77 || tim > 0.77) return 0.0; |
| 136 | return 0.54 + 0.46 * cos (2 * NUMpi * tim); |
| 137 | case Function_KAISER12: |
| 138 | if (tim < -0.77 || tim > 0.77) return 0.0; |
| 139 | if (one_by_bessi_0_12 == 0.0) one_by_bessi_0_12 = 1.0 / NUMbessel_i0_f (12); |
| 140 | return NUMbessel_i0_f (12 * sqrt (1 - (1.0 / 0.77 / 0.77) * tim * tim)) * one_by_bessi_0_12; |
| 141 | case Function_KAISER20: |
| 142 | if (tim <= -1 || tim >= 1) return 0.0; |
| 143 | if (one_by_bessi_0_20 == 0.0) one_by_bessi_0_20 = 1.0 / NUMbessel_i0_f (20.24); |
| 144 | return NUMbessel_i0_f (20.24 * sqrt (1 - tim * tim)) * one_by_bessi_0_20; |
| 145 | case Function_GAUSSIAN: |
| 146 | return exp ((- NUMpi * NUMpi) * tim * tim); |
| 147 | default: |
| 148 | return 0.0; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void Function_unidirectionalAutowindow (const constFunction me, double *const xmin, double *const xmax) { |
| 153 | if (*xmin >= *xmax) { |
| 154 | *xmin = my xmin; |
| 155 | *xmax = my xmax; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void Function_bidirectionalAutowindow (const constFunction me, double *const x1, double *const x2) { |
| 160 | if (*x1 == *x2) { |
| 161 | *x1 = my xmin; |
| 162 | *x2 = my xmax; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | bool Function_intersectRangeWithDomain (const constFunction me, double *const x1, double *const x2) { |
| 167 | if (*x1 == *x2) |
| 168 | return false; |
| 169 | if (*x1 < *x2) { |
| 170 | if (*x1 < my xmin) |
| 171 | *x1 = my xmin; // intersect requested range with logical domain // TODO: try Melder_clipLeft() |
| 172 | if (*x2 > my xmax) |
| 173 | *x2 = my xmax; |
| 174 | if (*x2 <= *x1) |
| 175 | return false; // requested range and logical domain do not intersect |
| 176 | } else { |
| 177 | if (*x2 < my xmin) |
| 178 | *x2 = my xmin; // intersect requested range with logical domain |
| 179 | if (*x1 > my xmax) |
| 180 | *x1 = my xmax; |
| 181 | if (*x1 <= *x2) |
| 182 | return false; // requested range and logical domain do not intersect |
| 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | /* End of file Function.cpp */ |