summaryrefslogtreecommitdiff
path: root/comp_prim.h
diff options
context:
space:
mode:
Diffstat (limited to 'comp_prim.h')
-rw-r--r--comp_prim.h109
1 files changed, 53 insertions, 56 deletions
diff --git a/comp_prim.h b/comp_prim.h
index d0f070a..e0166d3 100644
--- a/comp_prim.h
+++ b/comp_prim.h
@@ -28,114 +28,111 @@
28#ifndef __COMP_PRIM__ 28#ifndef __COMP_PRIM__
29#define __COMP_PRIM__ 29#define __COMP_PRIM__
30 30
31#include <math.h>
32
31/*---------------------------------------------------------------------------*\ 33/*---------------------------------------------------------------------------*\
32 34
33 FUNCTIONS 35 FUNCTIONS
34 36
35\*---------------------------------------------------------------------------*/ 37\*---------------------------------------------------------------------------*/
36 38
37inline static COMP cneg(COMP a) 39inline static COMP cneg(COMP a) {
38{ 40 COMP res;
39 COMP res;
40 41
41 res.real = -a.real; 42 res.real = -a.real;
42 res.imag = -a.imag; 43 res.imag = -a.imag;
43 44
44 return res; 45 return res;
45} 46}
46 47
47inline static COMP cconj(COMP a) 48inline static COMP cconj(COMP a) {
48{ 49 COMP res;
49 COMP res;
50 50
51 res.real = a.real; 51 res.real = a.real;
52 res.imag = -a.imag; 52 res.imag = -a.imag;
53 53
54 return res; 54 return res;
55} 55}
56 56
57inline static COMP cmult(COMP a, COMP b) 57inline static COMP cmult(COMP a, COMP b) {
58{ 58 COMP res;
59 COMP res;
60 59
61 res.real = a.real*b.real - a.imag*b.imag; 60 res.real = a.real * b.real - a.imag * b.imag;
62 res.imag = a.real*b.imag + a.imag*b.real; 61 res.imag = a.real * b.imag + a.imag * b.real;
63 62
64 return res; 63 return res;
65} 64}
66 65
67inline static COMP fcmult(float a, COMP b) 66inline static COMP fcmult(float a, COMP b) {
68{ 67 COMP res;
69 COMP res;
70 68
71 res.real = a*b.real; 69 res.real = a * b.real;
72 res.imag = a*b.imag; 70 res.imag = a * b.imag;
73 71
74 return res; 72 return res;
75} 73}
76 74
77inline static COMP cadd(COMP a, COMP b) 75inline static COMP cadd(COMP a, COMP b) {
78{ 76 COMP res;
79 COMP res;
80 77
81 res.real = a.real + b.real; 78 res.real = a.real + b.real;
82 res.imag = a.imag + b.imag; 79 res.imag = a.imag + b.imag;
83 80
84 return res; 81 return res;
85} 82}
86 83
87inline static float cabsolute(COMP a) 84inline static float cabsolute(COMP a) {
88{ 85 return sqrtf((a.real * a.real) + (a.imag * a.imag));
89 return sqrtf((a.real * a.real) + (a.imag * a.imag) );
90} 86}
91 87
92/* 88/*
93 * Euler's formula in a new convenient function 89 * Euler's formula in a new convenient function
94 */ 90 */
95inline static COMP comp_exp_j(float phi){ 91inline static COMP comp_exp_j(float phi) {
96 COMP res; 92 COMP res;
97 res.real = cosf(phi); 93 res.real = cosf(phi);
98 res.imag = sinf(phi); 94 res.imag = sinf(phi);
99 return res; 95 return res;
100} 96}
101 97
102/* 98/*
103 * Quick and easy complex 0 99 * Quick and easy complex 0
104 */ 100 */
105inline static COMP comp0(){ 101inline static COMP comp0() {
106 COMP res; 102 COMP res;
107 res.real = 0; 103 res.real = 0;
108 res.imag = 0; 104 res.imag = 0;
109 return res; 105 return res;
110} 106}
111 107
112/* 108/*
113 * Quick and easy complex subtract 109 * Quick and easy complex subtract
114 */ 110 */
115inline static COMP csub(COMP a, COMP b){ 111inline static COMP csub(COMP a, COMP b) {
116 COMP res; 112 COMP res;
117 res.real = a.real-b.real; 113 res.real = a.real - b.real;
118 res.imag = a.imag-b.imag; 114 res.imag = a.imag - b.imag;
119 return res; 115 return res;
120} 116}
121 117
122/* 118/*
123 * Compare the magnitude of a and b. if |a|>|b|, return true, otw false. 119 * Compare the magnitude of a and b. if |a|>|b|, return true, otw false.
124 * This needs no square roots 120 * This needs no square roots
125 */ 121 */
126inline static int comp_mag_gt(COMP a,COMP b){ 122inline static int comp_mag_gt(COMP a, COMP b) {
127 return ((a.real*a.real)+(a.imag*a.imag)) > ((b.real*b.real)+(b.imag*b.imag)); 123 return ((a.real * a.real) + (a.imag * a.imag)) >
124 ((b.real * b.real) + (b.imag * b.imag));
128} 125}
129 126
130/* 127/*
131 * Normalize a complex number's magnitude to 1 128 * Normalize a complex number's magnitude to 1
132 */ 129 */
133inline static COMP comp_normalize(COMP a){ 130inline static COMP comp_normalize(COMP a) {
134 COMP b; 131 COMP b;
135 float av = cabsolute(a); 132 float av = cabsolute(a);
136 b.real = a.real/av; 133 b.real = a.real / av;
137 b.imag = a.imag/av; 134 b.imag = a.imag / av;
138 return b; 135 return b;
139} 136}
140 137
141#endif 138#endif