-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex_util.c
103 lines (93 loc) · 1.94 KB
/
complex_util.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*! \file complex_util.c
* some simple calculations using fftw_complex representation of complex numbers
*/
#include "complex_util.h"
#include "math.h"
/**
* Multiplying complex numbers
* @param[in] a
* @param[in] b
* @param[out] res=a*b
*/
fftw_complex* c_multiply(fftw_complex a,fftw_complex b,fftw_complex *res)
{
res[0][0] = a[0]*b[0] - a[1]*b[1];
res[0][1] = a[1]*b[0] + a[0]*b[1];
return(res);
}
/**
* Dividing complex numbers
* @param[in] a
* @param[in] b
* @param[out] res=a/b
*/
fftw_complex* c_divide(fftw_complex a,fftw_complex b,fftw_complex *res)
{
double bnorm;
bnorm = b[0]*b[0]+b[1]*b[1];
res[0][0] = (a[0]*b[0] + a[1]*b[1])/bnorm;
res[0][1] = (a[1]*b[0] - a[0]*b[1])/bnorm;
return(res);
}
/**
* Adding complex numbers
* @param[in] a
* @param[in] b
* @param[out] res=a+b
*/
fftw_complex* c_add(fftw_complex a,fftw_complex b,fftw_complex *res)
{
res[0][0] = a[0]+b[0];
res[0][1] = a[1]+b[1];
return(res);
}
/**
* Subtracting complex numbers
* @param[in] a
* @param[in] b
* @param[out] res=a-b
*/
fftw_complex* c_sub(fftw_complex a,fftw_complex b,fftw_complex *res)
{
res[0][0] = a[0]-b[0];
res[0][1] = a[1]-b[1];
return(res);
}
/**
* Getting the angle of a complex number in the complex plane
* @param[in] a
* @result angle in radians
*/
double c_phase(fftw_complex a)
{
double res;
res = atan2(a[1],a[0]);
return(res);
}
/**
* Getting the magnitude of a complex number
* @param[in] a
* @result |a|
*/
double c_mag(fftw_complex a)
{
double res;
res = sqrt(a[0]*a[0]+a[1]*a[1]);
return(res);
}
/**
* raising a complex number to an arbitrary power
* @param[in] a the complex number
* @param[in] pw the power
* @param[out] res=a^pw
*/
fftw_complex* c_pow(fftw_complex a, float pw,fftw_complex *res)
{
double phase = c_phase(a);
double mag = c_mag(a);
phase*=pw;
mag = pow(mag,pw);
res[0][0] = mag*cos(phase);
res[0][1] = mag*sin(phase);
return(res);
}