blob: 1080f18bb454632f9878728acccc3fbaa5f85c93 [file] [log] [blame]
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +00001/*
2 * Copyright (c) 2014 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 WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_COMPLEX_MATRIX_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_COMPLEX_MATRIX_H_
13
14#include <complex>
15
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000016#include "webrtc/modules/audio_processing/beamformer/matrix.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/checks.h"
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000018
19namespace webrtc {
20
21using std::complex;
22
23// An extension of Matrix for operations that only work on a complex type.
24template <typename T>
25class ComplexMatrix : public Matrix<complex<T> > {
26 public:
27 ComplexMatrix() : Matrix<complex<T> >() {}
28
Peter Kasting69558702016-01-12 16:26:35 -080029 ComplexMatrix(size_t num_rows, size_t num_columns)
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000030 : Matrix<complex<T> >(num_rows, num_columns) {}
31
Peter Kasting69558702016-01-12 16:26:35 -080032 ComplexMatrix(const complex<T>* data, size_t num_rows, size_t num_columns)
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000033 : Matrix<complex<T> >(data, num_rows, num_columns) {}
34
35 // Complex Matrix operations.
36 ComplexMatrix& PointwiseConjugate() {
37 complex<T>* const data = this->data();
38 size_t size = this->num_rows() * this->num_columns();
39 for (size_t i = 0; i < size; ++i) {
40 data[i] = conj(data[i]);
41 }
42
43 return *this;
44 }
45
46 ComplexMatrix& PointwiseConjugate(const ComplexMatrix& operand) {
47 this->CopyFrom(operand);
48 return PointwiseConjugate();
49 }
50
51 ComplexMatrix& ConjugateTranspose() {
52 this->CopyDataToScratch();
Peter Kasting69558702016-01-12 16:26:35 -080053 size_t num_rows = this->num_rows();
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000054 this->SetNumRows(this->num_columns());
55 this->SetNumColumns(num_rows);
56 this->Resize();
57 return ConjugateTranspose(this->scratch_elements());
58 }
59
60 ComplexMatrix& ConjugateTranspose(const ComplexMatrix& operand) {
henrikg91d6ede2015-09-17 00:24:34 -070061 RTC_CHECK_EQ(operand.num_rows(), this->num_columns());
62 RTC_CHECK_EQ(operand.num_columns(), this->num_rows());
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000063 return ConjugateTranspose(operand.elements());
64 }
65
66 ComplexMatrix& ZeroImag() {
67 complex<T>* const data = this->data();
68 size_t size = this->num_rows() * this->num_columns();
69 for (size_t i = 0; i < size; ++i) {
70 data[i] = complex<T>(data[i].real(), 0);
71 }
72
73 return *this;
74 }
75
76 ComplexMatrix& ZeroImag(const ComplexMatrix& operand) {
77 this->CopyFrom(operand);
78 return ZeroImag();
79 }
80
81 private:
82 ComplexMatrix& ConjugateTranspose(const complex<T>* const* src) {
83 complex<T>* const* elements = this->elements();
Peter Kasting69558702016-01-12 16:26:35 -080084 for (size_t i = 0; i < this->num_rows(); ++i) {
85 for (size_t j = 0; j < this->num_columns(); ++j) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000086 elements[i][j] = conj(src[j][i]);
87 }
88 }
89
90 return *this;
91 }
92};
93
94} // namespace webrtc
95
96#endif // WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_COMPLEX_MATRIX_H_