| 1 | /* Vector_extensions.cpp |
| 2 | * |
| 3 | * Copyright (C) 2022 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. See the GNU |
| 13 | * 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 "Sampled.h" |
| 20 | #include "Vector_extensions.h" |
| 21 | |
| 22 | #include "enums_getText.h" |
| 23 | #include "Vector_extensions_enums.h" |
| 24 | #include "enums_getValue.h" |
| 25 | #include "Vector_extensions_enums.h" |
| 26 | |
| 27 | double Vector_getNearestLevelCrossing (Vector me, integer channel, double position, double level, kVectorSearchDirection searchDirection) { |
| 28 | if (position < my xmin || position > my xmax) |
| 29 | return undefined; |
| 30 | VEC amplitude = my channel (channel); |
| 31 | if (my nx == 1) |
| 32 | return ( amplitude [1] == level ? my x1 : undefined ); |
| 33 | integer leftSample = Sampled_xToLowIndex (me, position); |
| 34 | Melder_clip (1_integer, & leftSample, my nx - 1); |
| 35 | const integer rightSample = leftSample + 1; |
| 36 | |
| 37 | auto interpolateLinear = [&] (integer i1) -> double { |
| 38 | const double x1 = Sampled_indexToX (me, i1); // x2 = x1 + dx |
| 39 | const double y1 = amplitude [i1], y2 = amplitude [i1 + 1]; |
| 40 | /* |
| 41 | y = x1 + (x2 - x1) * (y1 - level) / (y1 - y2) = x1 + dx * (y1 - level) / (y1 - y2) |
| 42 | */ |
| 43 | return x1 + my dx * (y1 - level) / (y1 - y2); // y1 <> y2! |
| 44 | }; |
| 45 | /* |
| 46 | Are we already at a level crossing? |
| 47 | */ |
| 48 | if (leftSample >= 1 && rightSample <= my nx && |
| 49 | (amplitude [leftSample] >= level) != (amplitude [rightSample] >= level)) |
| 50 | { |
| 51 | const double crossing = interpolateLinear (leftSample); |
| 52 | return searchDirection == kVectorSearchDirection::LEFT ? |
| 53 | ( crossing <= position ? crossing : undefined ) : |
| 54 | ( crossing >= position ? crossing : undefined ); |
| 55 | } |
| 56 | |
| 57 | double leftCrossing = undefined; |
| 58 | if (searchDirection == kVectorSearchDirection::LEFT || searchDirection == kVectorSearchDirection::NEAREST) { |
| 59 | for (integer ileft = leftSample - 1; ileft >= 1; ileft --) |
| 60 | if ((amplitude [ileft] >= level) != (amplitude [ileft + 1] >= level)) { |
| 61 | leftCrossing = interpolateLinear (ileft); |
| 62 | break; |
| 63 | } |
| 64 | if (searchDirection == kVectorSearchDirection::LEFT) |
| 65 | return leftCrossing; |
| 66 | } |
| 67 | |
| 68 | double rightCrossing = undefined; |
| 69 | if (searchDirection == kVectorSearchDirection::RIGHT || searchDirection == kVectorSearchDirection::NEAREST) { |
| 70 | for (integer iright = rightSample + 1; iright <= my nx; iright ++) |
| 71 | if ((amplitude [iright] >= level) != (amplitude [iright - 1] >= level)) { |
| 72 | rightCrossing = interpolateLinear (iright - 1); |
| 73 | break; |
| 74 | } |
| 75 | if (searchDirection == kVectorSearchDirection::RIGHT) |
| 76 | return rightCrossing; |
| 77 | } |
| 78 | |
| 79 | return |
| 80 | isdefined (leftCrossing) && isdefined (rightCrossing) ? |
| 81 | ( position - leftCrossing < rightCrossing - position ? leftCrossing : rightCrossing ) |
| 82 | : isdefined (leftCrossing) ? leftCrossing |
| 83 | : isdefined (rightCrossing) ? rightCrossing |
| 84 | : undefined; |
| 85 | } |
| 86 | |
| 87 | /* End of file Vector_extensions.cpp */ |