aboutsummaryrefslogtreecommitdiff
path: root/webrtc/modules/audio_processing/beamformer/complex_matrix.h
blob: bfa3563b898cfdbe9520b6abde3f823d39faea3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_COMPLEX_MATRIX_H_
#define WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_COMPLEX_MATRIX_H_

#include <complex>

#include "webrtc/base/checks.h"
#include "webrtc/base/scoped_ptr.h"
#include "webrtc/modules/audio_processing/beamformer/matrix.h"

namespace webrtc {

using std::complex;

// An extension of Matrix for operations that only work on a complex type.
template <typename T>
class ComplexMatrix : public Matrix<complex<T> > {
 public:
  ComplexMatrix() : Matrix<complex<T> >() {}

  ComplexMatrix(int num_rows, int num_columns)
      : Matrix<complex<T> >(num_rows, num_columns) {}

  ComplexMatrix(const complex<T>* data, int num_rows, int num_columns)
      : Matrix<complex<T> >(data, num_rows, num_columns) {}

  // Complex Matrix operations.
  ComplexMatrix& PointwiseConjugate() {
    complex<T>* const data = this->data();
    size_t size = this->num_rows() * this->num_columns();
    for (size_t i = 0; i < size; ++i) {
      data[i] = conj(data[i]);
    }

    return *this;
  }

  ComplexMatrix& PointwiseConjugate(const ComplexMatrix& operand) {
    this->CopyFrom(operand);
    return PointwiseConjugate();
  }

  ComplexMatrix& ConjugateTranspose() {
    this->CopyDataToScratch();
    int num_rows = this->num_rows();
    this->SetNumRows(this->num_columns());
    this->SetNumColumns(num_rows);
    this->Resize();
    return ConjugateTranspose(this->scratch_elements());
  }

  ComplexMatrix& ConjugateTranspose(const ComplexMatrix& operand) {
    RTC_CHECK_EQ(operand.num_rows(), this->num_columns());
    RTC_CHECK_EQ(operand.num_columns(), this->num_rows());
    return ConjugateTranspose(operand.elements());
  }

  ComplexMatrix& ZeroImag() {
    complex<T>* const data = this->data();
    size_t size = this->num_rows() * this->num_columns();
    for (size_t i = 0; i < size; ++i) {
      data[i] = complex<T>(data[i].real(), 0);
    }

    return *this;
  }

  ComplexMatrix& ZeroImag(const ComplexMatrix& operand) {
    this->CopyFrom(operand);
    return ZeroImag();
  }

 private:
  ComplexMatrix& ConjugateTranspose(const complex<T>* const* src) {
    complex<T>* const* elements = this->elements();
    for (int i = 0; i < this->num_rows(); ++i) {
      for (int j = 0; j < this->num_columns(); ++j) {
        elements[i][j] = conj(src[j][i]);
      }
    }

    return *this;
  }
};

}  // namespace webrtc

#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_COMPLEX_MATRIX_H_