| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // This file is part of INSTINCT, the INS Toolkit for Integrated | ||
| 2 | // Navigation Concepts and Training by the Institute of Navigation of | ||
| 3 | // the University of Stuttgart, Germany. | ||
| 4 | // | ||
| 5 | // This Source Code Form is subject to the terms of the Mozilla Public | ||
| 6 | // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| 7 | // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| 8 | |||
| 9 | /// @file BDCSVD.hpp | ||
| 10 | /// @brief Bidiagonal Divide and Conquer SVD Curve Fit | ||
| 11 | /// @author T. Topp (topp@ins.uni-stuttgart.de) | ||
| 12 | /// @date 2023-11-06 | ||
| 13 | |||
| 14 | #pragma once | ||
| 15 | |||
| 16 | #include <cstddef> | ||
| 17 | #include <vector> | ||
| 18 | #include <cmath> | ||
| 19 | #include "util/Assert.h" | ||
| 20 | #include "util/Eigen.hpp" | ||
| 21 | #include "util/Logger.hpp" | ||
| 22 | |||
| 23 | namespace NAV | ||
| 24 | { | ||
| 25 | |||
| 26 | /// @brief Bidiagonal Divide and Conquer SVD Curve Fitting | ||
| 27 | /// @tparam Scalar Data type to store | ||
| 28 | template<typename Scalar> | ||
| 29 | class BDCSVD | ||
| 30 | { | ||
| 31 | public: | ||
| 32 | /// @brief Constructor | ||
| 33 | BDCSVD() = delete; | ||
| 34 | |||
| 35 | /// @brief Calculates the polynomial coefficients in order a0 + a1 * x + a2 * x^2 + ... | ||
| 36 | /// @param[in] x X data to fit | ||
| 37 | /// @param[in] y Y data to fit | ||
| 38 | /// @param[in] polynomialDegree Polynomial degree to fit | ||
| 39 | template<typename DerivedX, typename DerivedY> | ||
| 40 | 23540 | [[nodiscard]] static Eigen::VectorX<Scalar> calcCoefficients(const Eigen::MatrixBase<DerivedX>& x, const Eigen::MatrixBase<DerivedY>& y, size_t polynomialDegree = 2) | |
| 41 | { | ||
| 42 | 23540 | auto effectiveDegree = static_cast<Eigen::Index>(std::min(polynomialDegree, static_cast<size_t>(x.rows()) - 1)); | |
| 43 | |||
| 44 | 23540 | auto n = x.size(); | |
| 45 |
1/2✓ Branch 1 taken 23540 times.
✗ Branch 2 not taken.
|
23540 | Eigen::MatrixX<Scalar> H = Eigen::MatrixX<Scalar>(n, effectiveDegree + 1); |
| 46 |
2/4✓ Branch 2 taken 23540 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 23540 times.
✗ Branch 6 not taken.
|
23540 | Eigen::ArrayX<Scalar> xpow = Eigen::ArrayX<Scalar>::Ones(x.rows()); |
| 47 |
2/2✓ Branch 0 taken 70530 times.
✓ Branch 1 taken 23540 times.
|
94070 | for (int i = 0; i < effectiveDegree + 1; i++) |
| 48 | { | ||
| 49 |
3/6✓ Branch 1 taken 70530 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 70530 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 70530 times.
✗ Branch 8 not taken.
|
70530 | H.col(i) = xpow.matrix(); |
| 50 |
2/4✓ Branch 1 taken 70530 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 70530 times.
✗ Branch 5 not taken.
|
70530 | xpow *= x.array(); |
| 51 | } | ||
| 52 | |||
| 53 | ; | ||
| 54 |
3/6✓ Branch 1 taken 23540 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 23540 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 23540 times.
✗ Branch 8 not taken.
|
47080 | return Eigen::BDCSVD<Eigen::MatrixXd>(H, Eigen::ComputeThinU | Eigen::ComputeThinV).solve(y); |
| 55 | 23540 | } | |
| 56 | }; | ||
| 57 | |||
| 58 | } // namespace NAV | ||
| 59 |