| 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"cxx . . $ `<file>/fon/Sampled.h`/ |
| 20 | #include "Vector_extensions.h"cxx . . $ `<file>/dwtools/Vector_extensions.h`/ |
| 21 | |
| 22 | #include "enums_getText.h"cxx . . $ `<file>/melder/enums_getText.h`/ |
| 23 | #include "Vector_extensions_enums.h"cxx . . $ `<file>/dwtools/Vector_extensions_enums.h`/ |
| 24 | #include "enums_getValue.h"cxx . . $ `<file>/melder/enums_getValue.h`/ |
| 25 | #include "Vector_extensions_enums.h"cxx . . $ `<file>/dwtools/Vector_extensions_enums.h`/ |
| 26 | |
| 27 | double Vector_getNearestLevelCrossingcxx . . $ Vector_getNearestLevelCrossing(aa6ba2edce277310). (Vectorcxx . . $ Vector# me, integercxx . . $ integer# channel, double position, double level, kVectorSearchDirection searchDirection) { |
| 28 | if (position < mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. || position > mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.) |
| 29 | return undefinedcxx . . $ undefined.; |
| 30 | VECcxx . . $ VEC# amplitude = mycxx . . $ `melder/melder.h:44:9`! channelcxx . . $ structVector#channel(516a12db1224da40). (channel); |
| 31 | if (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. == 1) |
| 32 | return ( amplitude [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] == level ? mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. : undefinedcxx . . $ undefined. ); |
| 33 | integercxx . . $ integer# leftSample = Sampled_xToLowIndexcxx . . $ Sampled_xToLowIndex(405c2491bc3636e9). (me, position); |
| 34 | Melder_clipcxx . . $ Melder_clip(971c667ea412f82c). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., & leftSample, mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - 1); |
| 35 | const integercxx . . $ integer# rightSample = leftSample + 1; |
| 36 | |
| 37 | auto interpolateLinear = [&] (integercxx . . $ integer# i1) -> double { |
| 38 | const double x1 = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, i1); // x2 = x1 + dx |
| 39 | const double y1 = amplitude [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i1], y2 = amplitude [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i1 + 1]; |
| 40 | /* |
| 41 | y = x1 + (x2 - x1) * (y1 - level) / (y1 - y2) = x1 + dx * (y1 - level) / (y1 - y2) |
| 42 | */ |
| 43 | return x1 + mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. * (y1 - level) / (y1 - y2); // y1 <> y2! |
| 44 | }; |
| 45 | /* |
| 46 | Are we already at a level crossing? |
| 47 | */ |
| 48 | if (leftSample >= 1 && rightSample <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. && |
| 49 | (amplitude [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).leftSample] >= level) != (amplitude [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).rightSample] >= level)) |
| 50 | { |
| 51 | const double crossing = interpolateLinear (cxx . . $ Vector_getNearestLevelCrossing(aa6ba2edce277310).$anonymous_type_0#`operator()`(9db9ea2d2e621dac).leftSample); |
| 52 | return searchDirection == kVectorSearchDirection::LEFT ? |
| 53 | ( crossing <= position ? crossing : undefinedcxx . . $ undefined. ) : |
| 54 | ( crossing >= position ? crossing : undefinedcxx . . $ undefined. ); |
| 55 | } |
| 56 | |
| 57 | double leftCrossing = undefinedcxx . . $ undefined.; |
| 58 | if (searchDirection == kVectorSearchDirection::LEFT || searchDirection == kVectorSearchDirection::NEAREST) { |
| 59 | for (integercxx . . $ integer# ileft = leftSample - 1; ileft >= 1; ileft --) |
| 60 | if ((amplitude [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ileft] >= level) != (amplitude [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ileft + 1] >= level)) { |
| 61 | leftCrossing = interpolateLinear (cxx . . $ Vector_getNearestLevelCrossing(aa6ba2edce277310).$anonymous_type_0#`operator()`(9db9ea2d2e621dac).ileft); |
| 62 | break; |
| 63 | } |
| 64 | if (searchDirection == kVectorSearchDirection::LEFT) |
| 65 | return leftCrossing; |
| 66 | } |
| 67 | |
| 68 | double rightCrossing = undefinedcxx . . $ undefined.; |
| 69 | if (searchDirection == kVectorSearchDirection::RIGHT || searchDirection == kVectorSearchDirection::NEAREST) { |
| 70 | for (integercxx . . $ integer# iright = rightSample + 1; iright <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; iright ++) |
| 71 | if ((amplitude [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iright] >= level) != (amplitude [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iright - 1] >= level)) { |
| 72 | rightCrossing = interpolateLinear (cxx . . $ Vector_getNearestLevelCrossing(aa6ba2edce277310).$anonymous_type_0#`operator()`(9db9ea2d2e621dac).iright - 1); |
| 73 | break; |
| 74 | } |
| 75 | if (searchDirection == kVectorSearchDirection::RIGHT) |
| 76 | return rightCrossing; |
| 77 | } |
| 78 | |
| 79 | return |
| 80 | isdefinedcxx . . $ isdefined(f8197d536af80c96). (leftCrossing) && isdefinedcxx . . $ isdefined(f8197d536af80c96). (rightCrossing) ? |
| 81 | ( position - leftCrossing < rightCrossing - position ? leftCrossing : rightCrossing ) |
| 82 | : isdefinedcxx . . $ isdefined(f8197d536af80c96). (leftCrossing) ? leftCrossing |
| 83 | : isdefinedcxx . . $ isdefined(f8197d536af80c96). (rightCrossing) ? rightCrossing |
| 84 | : undefinedcxx . . $ undefined.; |
| 85 | } |
| 86 | |
| 87 | /* End of file Vector_extensions.cpp */ |