4 #include <opencv2/opencv.hpp>
7 #include "cuda_runtime.h"
10 #include "cuda/cuda_error_check.cuh"
18 bool foreign_data = false;
20 ComplexMat() : cols(0), rows(0), n_channels(0) {}
21 ComplexMat(uint _rows, uint _cols, uint _n_channels)
22 : cols(_cols), rows(_rows), n_channels(_n_channels)
24 CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
27 ComplexMat(uint _rows, uint _cols, uint _n_channels, uint _n_scales)
28 : cols(_cols), rows(_rows), n_channels(_n_channels), n_scales(_n_scales)
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;
41 other.p_data = nullptr;
46 if (p_data != nullptr && !foreign_data) {
47 CudaSafeCall(cudaFree(p_data));
52 void create(uint _rows, uint _cols, uint _n_channels)
56 n_channels = _n_channels;
57 CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
60 void create(uint _rows, uint _cols, uint _n_channels, uint _n_scales)
64 n_channels = _n_channels;
66 CudaSafeCall(cudaMalloc(&p_data, n_channels * cols * rows * sizeof(cufftComplex)));
68 // cv::Mat API compatibility
69 cv::Size size() { return cv::Size(cols, rows); }
70 int channels() { return n_channels; }
71 int channels() const { return n_channels; }
73 void sqr_norm(DynMem &result) const;
75 ComplexMat sqr_mag() const;
77 ComplexMat conj() const;
79 ComplexMat sum_over_channels() const;
81 cufftComplex *get_p_data() const;
83 // element-wise per channel multiplication, division and addition
84 ComplexMat operator*(const ComplexMat &rhs) const;
85 ComplexMat operator/(const ComplexMat &rhs) const;
86 ComplexMat operator+(const ComplexMat &rhs) const;
88 // multiplying or adding constant
89 ComplexMat operator*(const float &rhs) const;
90 ComplexMat operator+(const float &rhs) const;
92 // multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
93 ComplexMat mul(const ComplexMat &rhs) const;
95 // multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
96 ComplexMat mul2(const ComplexMat &rhs) const;
98 friend std::ostream &operator<<(std::ostream &os, const ComplexMat &mat)
100 float *data_cpu = reinterpret_cast<float*>(malloc(mat.rows * mat.cols * mat.n_channels * sizeof(cufftComplex)));
101 CudaSafeCall(cudaMemcpy(data_cpu, mat.p_data, mat.rows * mat.cols * mat.n_channels * sizeof(cufftComplex),
102 cudaMemcpyDeviceToHost));
103 // for (int i = 0; i < mat.n_channels; ++i){
104 for (int i = 0; i < 1; ++i) {
105 os << "Channel " << i << std::endl;
106 for (uint j = 0; j < mat.rows; ++j) {
107 for (uint k = 0; k < 2 * mat.cols - 2; k += 2)
108 os << "(" << data_cpu[j * 2 * mat.cols + k] << "," << data_cpu[j * 2 * mat.cols + (k + 1)] << ")"
110 os << "(" << data_cpu[j * 2 * mat.cols + 2 * mat.cols - 2] << ","
111 << data_cpu[j * 2 * mat.cols + 2 * mat.cols - 1] << ")" << std::endl;
118 void operator=(ComplexMat &rhs);
119 void operator=(ComplexMat &&rhs);
122 mutable float *p_data = nullptr;
125 #endif // COMPLEXMAT_H