4 #include <opencv2/opencv.hpp>
6 #include "cuda_runtime.h"
9 #include "cuda/cuda_error_check.cuh"
18 bool foreign_data = false;
20 ComplexMat() : cols(0), rows(0), n_channels(0) {}
21 ComplexMat(int _rows, int _cols, int _n_channels) : cols(_cols), rows(_rows), n_channels(_n_channels)
23 CudaSafeCall(cudaMalloc(&p_data, n_channels*cols*rows*sizeof(cufftComplex)));
26 ComplexMat(int _rows, int _cols, int _n_channels, int _n_scales) : cols(_cols), rows(_rows), n_channels(_n_channels), n_scales(_n_scales)
28 CudaSafeCall(cudaMalloc(&p_data, n_channels*cols*rows*sizeof(cufftComplex)));
31 ComplexMat(ComplexMat &&other)
35 n_channels = other.n_channels;
36 n_scales = other.n_scales;
37 p_data = other.p_data;
39 other.p_data = nullptr;
44 if(p_data != nullptr && !foreign_data){
45 CudaSafeCall(cudaFree(p_data));
50 void create(int _rows, int _cols, int _n_channels)
54 n_channels = _n_channels;
55 CudaSafeCall(cudaMalloc(&p_data, n_channels*cols*rows*sizeof(cufftComplex)));
58 void create(int _rows, int _cols, int _n_channels, int _n_scales)
62 n_channels = _n_channels;
64 CudaSafeCall(cudaMalloc(&p_data, n_channels*cols*rows*sizeof(cufftComplex)));
66 // cv::Mat API compatibility
67 cv::Size size() { return cv::Size(cols, rows); }
68 int channels() { return n_channels; }
69 int channels() const { return n_channels; }
71 float sqr_norm() const;
72 void sqr_norm(float *result) const;
74 ComplexMat sqr_mag() const;
76 ComplexMat conj() const;
78 ComplexMat sum_over_channels() const;
80 cufftComplex* get_p_data() const;
82 //element-wise per channel multiplication, division and addition
83 ComplexMat operator*(const ComplexMat & rhs) const;
84 ComplexMat operator/(const ComplexMat & rhs) const;
85 ComplexMat operator+(const ComplexMat & rhs) const;
87 //multiplying or adding constant
88 ComplexMat operator*(const float & rhs) const;
89 ComplexMat operator+(const float & rhs) const;
91 //multiplying element-wise multichannel by one channel mats (rhs mat is with one channel)
92 ComplexMat mul(const ComplexMat & rhs) const;
94 //multiplying element-wise multichannel by one channel mats (rhs mat is with multiple channel)
95 ComplexMat mul2(const ComplexMat & rhs) const;
97 friend std::ostream & operator<<(std::ostream & os, const ComplexMat & mat)
99 float *data_cpu = (float*) malloc(mat.rows*mat.cols*mat.n_channels*sizeof(cufftComplex));
100 CudaSafeCall(cudaMemcpy(data_cpu, mat.p_data, mat.rows*mat.cols*mat.n_channels*sizeof(cufftComplex), cudaMemcpyDeviceToHost));
101 //for (int i = 0; i < mat.n_channels; ++i){
102 for (int i = 0; i < 1; ++i){
103 os << "Channel " << i << std::endl;
104 for (int j = 0; j < mat.rows; ++j) {
105 for (int k = 0; k < 2*mat.cols-2; k+=2)
106 os << "(" << data_cpu[j*2*mat.cols + k] << "," << data_cpu[j*2*mat.cols + (k+1)] << ")" << ", ";
107 os << "(" << data_cpu[j*2*mat.cols + 2*mat.cols-2] << "," << data_cpu[j*2*mat.cols + 2*mat.cols-1] << ")" << std::endl;
114 void operator=(ComplexMat & rhs);
115 void operator=(ComplexMat && rhs);
119 mutable float *p_data = nullptr;
122 #endif // COMPLEXMAT_H