aluebs@webrtc.org | 0c39e91 | 2014-12-18 22:22:04 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 0c39e91 | 2014-12-18 22:22:04 +0000 | [diff] [blame] | 16 | #include "webrtc/modules/audio_processing/beamformer/matrix.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 17 | #include "webrtc/rtc_base/checks.h" |
aluebs@webrtc.org | 0c39e91 | 2014-12-18 22:22:04 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
| 21 | using std::complex; |
| 22 | |
| 23 | // An extension of Matrix for operations that only work on a complex type. |
| 24 | template <typename T> |
| 25 | class ComplexMatrix : public Matrix<complex<T> > { |
| 26 | public: |
| 27 | ComplexMatrix() : Matrix<complex<T> >() {} |
| 28 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 29 | ComplexMatrix(size_t num_rows, size_t num_columns) |
aluebs@webrtc.org | 0c39e91 | 2014-12-18 22:22:04 +0000 | [diff] [blame] | 30 | : Matrix<complex<T> >(num_rows, num_columns) {} |
| 31 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 32 | ComplexMatrix(const complex<T>* data, size_t num_rows, size_t num_columns) |
aluebs@webrtc.org | 0c39e91 | 2014-12-18 22:22:04 +0000 | [diff] [blame] | 33 | : 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 Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 53 | size_t num_rows = this->num_rows(); |
aluebs@webrtc.org | 0c39e91 | 2014-12-18 22:22:04 +0000 | [diff] [blame] | 54 | 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) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 61 | RTC_CHECK_EQ(operand.num_rows(), this->num_columns()); |
| 62 | RTC_CHECK_EQ(operand.num_columns(), this->num_rows()); |
aluebs@webrtc.org | 0c39e91 | 2014-12-18 22:22:04 +0000 | [diff] [blame] | 63 | 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 Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 84 | for (size_t i = 0; i < this->num_rows(); ++i) { |
| 85 | for (size_t j = 0; j < this->num_columns(); ++j) { |
aluebs@webrtc.org | 0c39e91 | 2014-12-18 22:22:04 +0000 | [diff] [blame] | 86 | 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_ |