Magnus Jedvert | 1927dfa | 2018-09-11 12:56:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef RTC_TOOLS_FRAME_ANALYZER_LINEAR_LEAST_SQUARES_H_ |
| 12 | #define RTC_TOOLS_FRAME_ANALYZER_LINEAR_LEAST_SQUARES_H_ |
| 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
Magnus Jedvert | 1927dfa | 2018-09-11 12:56:06 +0200 | [diff] [blame] | 16 | #include <valarray> |
| 17 | #include <vector> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | |
Magnus Jedvert | 1927dfa | 2018-09-11 12:56:06 +0200 | [diff] [blame] | 19 | #include "absl/types/optional.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | namespace test { |
| 23 | |
| 24 | // This class is used for finding a matrix b that roughly solves the equation: |
| 25 | // y = x * b. This is generally impossible to do exactly, so the problem is |
| 26 | // rephrased as finding the matrix b that minimizes the difference: |
| 27 | // |y - x * b|^2. Calling multiple AddObservations() is equivalent to |
| 28 | // concatenating the observation vectors and calling AddObservations() once. The |
| 29 | // reason for doing it incrementally is that we can't store the raw YUV values |
| 30 | // for a whole video file in memory at once. This class has a constant memory |
| 31 | // footprint, regardless how may times AddObservations() is called. |
| 32 | class IncrementalLinearLeastSquares { |
| 33 | public: |
| 34 | IncrementalLinearLeastSquares(); |
| 35 | ~IncrementalLinearLeastSquares(); |
| 36 | |
| 37 | // Add a number of observations. The subvectors of x and y must have the same |
| 38 | // length. |
| 39 | void AddObservations(const std::vector<std::vector<uint8_t>>& x, |
| 40 | const std::vector<std::vector<uint8_t>>& y); |
| 41 | |
| 42 | // Calculate and return the best linear solution, given the observations so |
| 43 | // far. |
| 44 | std::vector<std::vector<double>> GetBestSolution() const; |
| 45 | |
| 46 | private: |
| 47 | // Running sum of x^T * x. |
| 48 | absl::optional<std::valarray<std::valarray<uint64_t>>> sum_xx; |
| 49 | // Running sum of x^T * y. |
| 50 | absl::optional<std::valarray<std::valarray<uint64_t>>> sum_xy; |
| 51 | }; |
| 52 | |
| 53 | } // namespace test |
| 54 | } // namespace webrtc |
| 55 | |
| 56 | #endif // RTC_TOOLS_FRAME_ANALYZER_LINEAR_LEAST_SQUARES_H_ |