blob: 1b07dc1bde640afb792f1dc7625b3e63d8b582b3 [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
14#include <valarray>
15#include <vector>
16#include "absl/types/optional.h"
17
18namespace webrtc {
19namespace test {
20
21// This class is used for finding a matrix b that roughly solves the equation:
22// y = x * b. This is generally impossible to do exactly, so the problem is
23// rephrased as finding the matrix b that minimizes the difference:
24// |y - x * b|^2. Calling multiple AddObservations() is equivalent to
25// concatenating the observation vectors and calling AddObservations() once. The
26// reason for doing it incrementally is that we can't store the raw YUV values
27// for a whole video file in memory at once. This class has a constant memory
28// footprint, regardless how may times AddObservations() is called.
29class IncrementalLinearLeastSquares {
30 public:
31 IncrementalLinearLeastSquares();
32 ~IncrementalLinearLeastSquares();
33
34 // Add a number of observations. The subvectors of x and y must have the same
35 // length.
36 void AddObservations(const std::vector<std::vector<uint8_t>>& x,
37 const std::vector<std::vector<uint8_t>>& y);
38
39 // Calculate and return the best linear solution, given the observations so
40 // far.
41 std::vector<std::vector<double>> GetBestSolution() const;
42
43 private:
44 // Running sum of x^T * x.
45 absl::optional<std::valarray<std::valarray<uint64_t>>> sum_xx;
46 // Running sum of x^T * y.
47 absl::optional<std::valarray<std::valarray<uint64_t>>> sum_xy;
48};
49
50} // namespace test
51} // namespace webrtc
52
53#endif // RTC_TOOLS_FRAME_ANALYZER_LINEAR_LEAST_SQUARES_H_