4 #include <opencv2/opencv.hpp>
6 #include "cuda_runtime.h"
9 #include "cuda/cuda_error_check.cuh"
17 bool foreign_data = false;
18 cudaStream_t stream = nullptr;
20 ComplexMat() : cols(0), rows(0), n_channels(0) {}
21 ComplexMat(int _rows, int _cols, int _n_channels, cudaStream_t _stream)
22 : cols(_cols), rows(_rows), n_channels(_n_channels), stream(_stream)
24 CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
27 ComplexMat(int _rows, int _cols, int _n_channels, int _n_scales, cudaStream_t _stream)
28 : cols(_cols), rows(_rows), n_channels(_n_channels), n_scales(_n_scales), stream(_stream)
30 CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
33 ComplexMat(ComplexMat &&other)
37 n_channels = other.n_channels;
38 n_scales = other.n_scales;
39 p_data = other.p_data;
40 stream = other.stream;
42 other.p_data = nullptr;
47 if (p_data != nullptr && !foreign_data) {
48 CudaSafeCall(cudaFree(p_data));
53 void create(int _rows, int _cols, int _n_channels, cudaStream_t _stream = nullptr)
57 n_channels = _n_channels;
59 CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
62 void create(int _rows, int _cols, int _n_channels, int _n_scales, cudaStream_t _stream = nullptr)
66 n_channels = _n_channels;
69 CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
71 // cv::Mat API compatibility
72 cv::Size size() { return cv::Size(cols, rows); }
73 int channels() { return n_channels; }
74 int channels() const { return n_channels; }
76 void sqr_norm(float *result) const;
78 ComplexMat sqr_mag() const;
80 ComplexMat conj() const;
82 ComplexMat sum_over_channels() const;
84 cufftComplex *get_p_data() const;
86 // element-wise per channel multiplication, division and addition
87 ComplexMat operator*(const ComplexMat &rhs) const;
88 ComplexMat operator/(const ComplexMat &rhs) const;
89 ComplexMat operator+(const ComplexMat &rhs) const;
91 // multiplying or adding constant
92 ComplexMat operator*(const float &rhs) const;
93 ComplexMat operator+(const float &rhs) const;
95 // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
96 ComplexMat mul(const ComplexMat &rhs) const;
98 // multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
99 ComplexMat mul2(const ComplexMat &rhs) const;
101 friend std::ostream &operator<<(std::ostream &os, const ComplexMat &mat)
103 float *data_cpu = (float *)malloc(mat.rows * mat.cols * mat.n_channels * sizeof(cufftComplex));
104 CudaSafeCall(cudaMemcpy(data_cpu, mat.p_data, mat.rows * mat.cols * mat.n_channels * sizeof(cufftComplex),
105 cudaMemcpyDeviceToHost));
106 // for (int i = 0; i < mat.n_channels; ++i){
107 for (int i = 0; i < 1; ++i) {
108 os << "Channel " << i << std::endl;
109 for (int j = 0; j < mat.rows; ++j) {
110 for (int k = 0; k < 2 * mat.cols - 2; k += 2)
111 os << "(" << data_cpu[j * 2 * mat.cols + k] << "," << data_cpu[j * 2 * mat.cols + (k + 1)] << ")"
113 os << "(" << data_cpu[j * 2 * mat.cols + 2 * mat.cols - 2] << ","
114 << data_cpu[j * 2 * mat.cols + 2 * mat.cols - 1] << ")" << std::endl;
121 void operator=(ComplexMat &rhs);
122 void operator=(ComplexMat &&rhs);
125 mutable float *p_data = nullptr;
128 #endif // COMPLEXMAT_H