-
Notifications
You must be signed in to change notification settings - Fork 4
/
amplitude_2d.cpp
87 lines (72 loc) · 2.48 KB
/
amplitude_2d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Software: Discrete Fourier Transform 1D (for real and complex signals)
// Author: Hy Truong Son
// Position: PhD Student
// Institution: Department of Computer Science, The University of Chicago
// Email: [email protected], [email protected]
// Website: http://people.inf.elte.hu/hytruongson/
// Copyright 2016 (c) Hy Truong Son. All rights reserved.
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <iterator>
#include <algorithm>
#include <ctime>
#include "mex.h"
using namespace std;
int M, N;
double **Re;
double **Im;
double **Amplitude;
void vector2matrix(double *input, int nRows, int nCols, double **output) {
for (int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
output[i][j] = input[j * nRows + i];
}
}
}
void matrix2vector(double **input, int nRows, int nCols, double *output) {
for (int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
output[j * nRows + i] = input[i][j];
}
}
}
void mexFunction(int nOutputs, mxArray *output_pointers[], int nInputs, const mxArray *input_pointers[]) {
if (nInputs != 2) {
std::cerr << "Exactly 2 input parameters!" << std::endl;
return;
}
if (nOutputs != 1) {
std::cerr << "Exactly 1 output parameters!" << std::endl;
return;
}
if ((mxGetM(input_pointers[0]) != mxGetM(input_pointers[1])) || (mxGetN(input_pointers[0]) != mxGetN(input_pointers[1]))) {
std::cerr << "The size of the real part and the imaginary part must be the same!" << std::endl;
return;
}
int M = mxGetM(input_pointers[0]);
int N = mxGetN(input_pointers[0]);
Re = new double* [M];
Im = new double* [N];
Amplitude = new double* [M];
for (int row = 0; row < M; ++row) {
Re[row] = new double [N];
Im[row] = new double [N];
Amplitude[row] = new double [N];
}
vector2matrix(mxGetPr(input_pointers[0]), M, N, Re);
vector2matrix(mxGetPr(input_pointers[1]), M, N, Im);
for (int row = 0; row < M; ++row) {
for (int column = 0; column < N; ++column) {
Amplitude[row][column] = sqrt(Re[row][column] * Re[row][column] + Im[row][column] * Im[row][column]);
}
}
output_pointers[0] = mxCreateDoubleMatrix(M, N, mxREAL);
matrix2vector(Amplitude, M, N, mxGetPr(output_pointers[0]));
}