tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
toeplitz.h
Go to the documentation of this file.
1#ifndef FUSED_ALGORITHMS_TOEPLITZ_H
2#define FUSED_ALGORITHMS_TOEPLITZ_H
3
4#include "config.h"
6#include "matrix_traits.h"
8#include "math/math_utils.h" // math::abs
9
66namespace matrix_algorithms
67{
68
70
90 template <typename T, my_size_t N>
92 const FusedVector<T, N> &r,
93 const FusedVector<T, N> &b)
94 {
95 static_assert(is_floating_point_v<T>,
96 "levinson_durbin requires a floating-point scalar type");
97
98 // Check r(0) != 0
99 if (math::abs(r(0)) <= T(PRECISION_TOLERANCE))
100 {
101 return Unexpected{MatrixStatus::Singular};
102 }
103
104 // N = 1: trivial case
105 if constexpr (N == 1)
106 {
107 FusedVector<T, 1> x(T(0));
108 x(0) = b(0) / r(0);
109 return move(x);
110 }
111 else
112 {
113 // Forward vector (prediction coefficients)
114 FusedVector<T, N> f(T(0));
115 // Backward vector
116 FusedVector<T, N> f_prev(T(0));
117 // Solution vector
118 FusedVector<T, N> x(T(0));
119 FusedVector<T, N> x_prev(T(0));
120
121 // Initialize order 1
122 T err = r(0);
123 x(0) = b(0) / err;
124
125 f(0) = -r(1) / r(0);
126 err = r(0) * (T(1) - f(0) * f(0));
127
128 if (err <= T(PRECISION_TOLERANCE))
129 {
130 return Unexpected{MatrixStatus::NotConverged};
131 }
132
133 // Update x for order 1
134 // x = x_prev + f(0) * reversed(x_prev) + b(1)/err correction
135 {
136 T x0_prev = x(0);
137 T delta = b(1) - r(1) * x0_prev;
138 x(0) = x0_prev + (delta / err) * f(0);
139 x(1) = delta / err;
140 }
141
142 // Iterate for orders 2 … N−1
143 for (my_size_t m = 1; m + 1 < N; ++m)
144 {
145 // Save previous forward vector
146 for (my_size_t k = 0; k <= m - 1; ++k)
147 {
148 f_prev(k) = f(k);
149 }
150
151 // Compute reflection coefficient λ
152 T lambda_num = r(m + 1);
153 for (my_size_t k = 0; k < m; ++k)
154 {
155 lambda_num += f_prev(k) * r(m - k);
156 }
157 T lambda = -lambda_num / err;
158
159 // Update forward vector: f(k) = f_prev(k) + λ·f_prev(m−1−k)
160 for (my_size_t k = 0; k < m; ++k)
161 {
162 f(k) = f_prev(k) + lambda * f_prev(m - 1 - k);
163 }
164 f(m) = lambda;
165
166 // Update error
167 err *= (T(1) - lambda * lambda);
168
169 if (err <= T(PRECISION_TOLERANCE))
170 {
171 return Unexpected{MatrixStatus::NotConverged};
172 }
173
174 // Save previous solution
175 for (my_size_t k = 0; k <= m; ++k)
176 {
177 x_prev(k) = x(k);
178 }
179
180 // Compute delta for the new equation
181 T delta = b(m + 1);
182 for (my_size_t k = 0; k <= m; ++k)
183 {
184 delta -= r(m + 1 - k) * x_prev(k);
185 }
186
187 T correction = delta / err;
188
189 // Update solution: x(k) = x_prev(k) + correction·f_reversed
190 for (my_size_t k = 0; k <= m; ++k)
191 {
192 x(k) = x_prev(k) + correction * f(m - k);
193 }
194 x(m + 1) = correction;
195 }
196
197 return move(x);
198 }
199 }
200
201} // namespace matrix_algorithms
202
203#endif // FUSED_ALGORITHMS_TOEPLITZ_H
A discriminated union holding either a success value or an error.
Definition expected.h:86
Definition fused_vector.h:9
Global configuration for the tesseract tensor library.
#define my_size_t
Size/index type used throughout the library.
Definition config.h:126
#define PRECISION_TOLERANCE
Tolerance for floating-point comparisons (e.g. symmetry checks, Cholesky).
Definition config.h:117
A minimal, STL-free expected/result type for failable operations.
Runtime property descriptors and error codes for matrices.
constexpr T abs(T x) noexcept
Compute the absolute value of a numeric value.
Definition math_utils.h:48
Definition cholesky.h:52
Expected< FusedVector< T, N >, MatrixStatus > levinson_durbin(const FusedVector< T, N > &r, const FusedVector< T, N > &b)
Solve a symmetric Toeplitz system Tx = b using Levinson-Durbin.
Definition toeplitz.h:91
MatrixStatus
Error codes for matrix decomposition and solver algorithms.
Definition matrix_traits.h:33
constexpr remove_reference_t< T > && move(T &&t) noexcept
Cast to rvalue reference (replacement for std::move).
Definition simple_type_traits.h:178
Tag type for constructing an Expected in the error state.
Definition expected.h:30