diff options
Diffstat (limited to 'comp_prim.h')
-rw-r--r-- | comp_prim.h | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/comp_prim.h b/comp_prim.h new file mode 100644 index 0000000..d0f070a --- /dev/null +++ b/comp_prim.h | |||
@@ -0,0 +1,141 @@ | |||
1 | /*---------------------------------------------------------------------------*\ | ||
2 | |||
3 | FILE........: comp_prim.h | ||
4 | AUTHOR......: David Rowe | ||
5 | DATE CREATED: Marh 2015 | ||
6 | |||
7 | Complex number maths primitives. | ||
8 | |||
9 | \*---------------------------------------------------------------------------*/ | ||
10 | |||
11 | /* | ||
12 | Copyright (C) 2015 David Rowe | ||
13 | |||
14 | All rights reserved. | ||
15 | |||
16 | This program is free software; you can redistribute it and/or modify | ||
17 | it under the terms of the GNU Lesser General Public License version 2.1, as | ||
18 | published by the Free Software Foundation. This program is | ||
19 | distributed in the hope that it will be useful, but WITHOUT ANY | ||
20 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
21 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | ||
22 | License for more details. | ||
23 | |||
24 | You should have received a copy of the GNU Lesser General Public License | ||
25 | along with this program; if not, see <http://www.gnu.org/licenses/>. | ||
26 | */ | ||
27 | |||
28 | #ifndef __COMP_PRIM__ | ||
29 | #define __COMP_PRIM__ | ||
30 | |||
31 | /*---------------------------------------------------------------------------*\ | ||
32 | |||
33 | FUNCTIONS | ||
34 | |||
35 | \*---------------------------------------------------------------------------*/ | ||
36 | |||
37 | inline static COMP cneg(COMP a) | ||
38 | { | ||
39 | COMP res; | ||
40 | |||
41 | res.real = -a.real; | ||
42 | res.imag = -a.imag; | ||
43 | |||
44 | return res; | ||
45 | } | ||
46 | |||
47 | inline static COMP cconj(COMP a) | ||
48 | { | ||
49 | COMP res; | ||
50 | |||
51 | res.real = a.real; | ||
52 | res.imag = -a.imag; | ||
53 | |||
54 | return res; | ||
55 | } | ||
56 | |||
57 | inline static COMP cmult(COMP a, COMP b) | ||
58 | { | ||
59 | COMP res; | ||
60 | |||
61 | res.real = a.real*b.real - a.imag*b.imag; | ||
62 | res.imag = a.real*b.imag + a.imag*b.real; | ||
63 | |||
64 | return res; | ||
65 | } | ||
66 | |||
67 | inline static COMP fcmult(float a, COMP b) | ||
68 | { | ||
69 | COMP res; | ||
70 | |||
71 | res.real = a*b.real; | ||
72 | res.imag = a*b.imag; | ||
73 | |||
74 | return res; | ||
75 | } | ||
76 | |||
77 | inline static COMP cadd(COMP a, COMP b) | ||
78 | { | ||
79 | COMP res; | ||
80 | |||
81 | res.real = a.real + b.real; | ||
82 | res.imag = a.imag + b.imag; | ||
83 | |||
84 | return res; | ||
85 | } | ||
86 | |||
87 | inline static float cabsolute(COMP a) | ||
88 | { | ||
89 | return sqrtf((a.real * a.real) + (a.imag * a.imag) ); | ||
90 | } | ||
91 | |||
92 | /* | ||
93 | * Euler's formula in a new convenient function | ||
94 | */ | ||
95 | inline static COMP comp_exp_j(float phi){ | ||
96 | COMP res; | ||
97 | res.real = cosf(phi); | ||
98 | res.imag = sinf(phi); | ||
99 | return res; | ||
100 | } | ||
101 | |||
102 | /* | ||
103 | * Quick and easy complex 0 | ||
104 | */ | ||
105 | inline static COMP comp0(){ | ||
106 | COMP res; | ||
107 | res.real = 0; | ||
108 | res.imag = 0; | ||
109 | return res; | ||
110 | } | ||
111 | |||
112 | /* | ||
113 | * Quick and easy complex subtract | ||
114 | */ | ||
115 | inline static COMP csub(COMP a, COMP b){ | ||
116 | COMP res; | ||
117 | res.real = a.real-b.real; | ||
118 | res.imag = a.imag-b.imag; | ||
119 | return res; | ||
120 | } | ||
121 | |||
122 | /* | ||
123 | * Compare the magnitude of a and b. if |a|>|b|, return true, otw false. | ||
124 | * This needs no square roots | ||
125 | */ | ||
126 | inline 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)); | ||
128 | } | ||
129 | |||
130 | /* | ||
131 | * Normalize a complex number's magnitude to 1 | ||
132 | */ | ||
133 | inline static COMP comp_normalize(COMP a){ | ||
134 | COMP b; | ||
135 | float av = cabsolute(a); | ||
136 | b.real = a.real/av; | ||
137 | b.imag = a.imag/av; | ||
138 | return b; | ||
139 | } | ||
140 | |||
141 | #endif | ||