10c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org/*
20c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
30c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org *
40c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org *  Use of this source code is governed by a BSD-style license
50c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org *  that can be found in the LICENSE file in the root of the source
60c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org *  tree. An additional intellectual property rights grant can be found
70c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org *  in the file PATENTS.  All contributing project authors may
80c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
90c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org */
100c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
110c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_COMPLEX_MATRIX_H_
120c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org#define WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_COMPLEX_MATRIX_H_
130c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
140c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org#include <complex>
150c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
160c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org#include "webrtc/base/checks.h"
1700b8f6b3643332cce1ee711715f7fbb824d793cakwiberg@webrtc.org#include "webrtc/base/scoped_ptr.h"
180c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org#include "webrtc/modules/audio_processing/beamformer/matrix.h"
190c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
200c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.orgnamespace webrtc {
210c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
220c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.orgusing std::complex;
230c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
240c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org// An extension of Matrix for operations that only work on a complex type.
250c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.orgtemplate <typename T>
260c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.orgclass ComplexMatrix : public Matrix<complex<T> > {
270c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org public:
280c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  ComplexMatrix() : Matrix<complex<T> >() {}
290c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
306955870806624479723addfae6dcf5d13968796cPeter Kasting  ComplexMatrix(size_t num_rows, size_t num_columns)
310c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org      : Matrix<complex<T> >(num_rows, num_columns) {}
320c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
336955870806624479723addfae6dcf5d13968796cPeter Kasting  ComplexMatrix(const complex<T>* data, size_t num_rows, size_t num_columns)
340c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org      : Matrix<complex<T> >(data, num_rows, num_columns) {}
350c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
360c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  // Complex Matrix operations.
370c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  ComplexMatrix& PointwiseConjugate() {
380c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    complex<T>* const data = this->data();
390c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    size_t size = this->num_rows() * this->num_columns();
400c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    for (size_t i = 0; i < size; ++i) {
410c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org      data[i] = conj(data[i]);
420c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    }
430c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
440c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    return *this;
450c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  }
460c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
470c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  ComplexMatrix& PointwiseConjugate(const ComplexMatrix& operand) {
480c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    this->CopyFrom(operand);
490c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    return PointwiseConjugate();
500c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  }
510c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
520c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  ComplexMatrix& ConjugateTranspose() {
530c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    this->CopyDataToScratch();
546955870806624479723addfae6dcf5d13968796cPeter Kasting    size_t num_rows = this->num_rows();
550c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    this->SetNumRows(this->num_columns());
560c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    this->SetNumColumns(num_rows);
570c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    this->Resize();
580c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    return ConjugateTranspose(this->scratch_elements());
590c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  }
600c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
610c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  ComplexMatrix& ConjugateTranspose(const ComplexMatrix& operand) {
6291d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg    RTC_CHECK_EQ(operand.num_rows(), this->num_columns());
6391d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg    RTC_CHECK_EQ(operand.num_columns(), this->num_rows());
640c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    return ConjugateTranspose(operand.elements());
650c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  }
660c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
670c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  ComplexMatrix& ZeroImag() {
680c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    complex<T>* const data = this->data();
690c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    size_t size = this->num_rows() * this->num_columns();
700c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    for (size_t i = 0; i < size; ++i) {
710c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org      data[i] = complex<T>(data[i].real(), 0);
720c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    }
730c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
740c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    return *this;
750c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  }
760c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
770c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  ComplexMatrix& ZeroImag(const ComplexMatrix& operand) {
780c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    this->CopyFrom(operand);
790c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    return ZeroImag();
800c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  }
810c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
820c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org private:
830c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  ComplexMatrix& ConjugateTranspose(const complex<T>* const* src) {
840c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    complex<T>* const* elements = this->elements();
856955870806624479723addfae6dcf5d13968796cPeter Kasting    for (size_t i = 0; i < this->num_rows(); ++i) {
866955870806624479723addfae6dcf5d13968796cPeter Kasting      for (size_t j = 0; j < this->num_columns(); ++j) {
870c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org        elements[i][j] = conj(src[j][i]);
880c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org      }
890c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    }
900c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
910c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org    return *this;
920c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org  }
930c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org};
940c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
950c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org}  // namespace webrtc
960c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org
970c39e91cc827f222ab227bad09cf9199163d28f8aluebs@webrtc.org#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_COMPLEX_MATRIX_H_
98