blob: 7006db1d65cdce580b81ca140feba7bbd51a6d78 [file] [log] [blame]
Magnus Jedvert1927dfa2018-09-11 12:56:06 +02001/*
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 Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Magnus Jedvert1927dfa2018-09-11 12:56:06 +020016#include <valarray>
17#include <vector>
Yves Gerey3e707812018-11-28 16:47:49 +010018
Magnus Jedvert1927dfa2018-09-11 12:56:06 +020019#include "absl/types/optional.h"
20
21namespace webrtc {
22namespace 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.
32class 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_