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