]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/cxops.c
Bug fixes to jsdr, keyer
[dttsp.git] / jDttSP / cxops.c
1 /* cxops.c
2 This file is part of a program that implements a Software-Defined Radio.
3
4 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 The authors can be reached by email at
21
22 ab2kt@arrl.net
23 or
24 rwmcgwier@comcast.net
25
26 or by paper mail at
27
28 The DTTS Microwave Society
29 6 Kathleen Place
30 Bridgewater, NJ 08807
31 */
32
33 #include <cxops.h>
34
35 // useful constants
36
37 COMPLEX cxzero = {0.0, 0.0};
38 COMPLEX cxone = {1.0, 0.0};
39 COMPLEX cxJ = {0.0, 1.0};
40 COMPLEX cxminusone = {-1.0, 0.0};
41 COMPLEX cxminusJ = {0.0, -1.0};
42
43 // scalar
44
45 COMPLEX
46 Cscl(COMPLEX x, REAL a) {
47   COMPLEX z;
48   c_re(z) = c_re(x) * a;
49   c_im(z) = c_im(x) * a;
50   return z;
51 }
52
53 COMPLEX
54 Cadd(COMPLEX x, COMPLEX y) {
55   COMPLEX z;
56   c_re(z) = c_re(x) + c_re(y);
57   c_im(z) = c_im(x) + c_im(y);
58   return z;
59 }
60
61 COMPLEX
62 Csub(COMPLEX x, COMPLEX y) {
63   COMPLEX z;
64   c_re(z) = c_re(x) - c_re(y);
65   c_im(z) = c_im(x) - c_im(y);
66   return z;
67 }
68
69 COMPLEX
70 Cmul(COMPLEX x, COMPLEX y) {
71   COMPLEX z;
72   c_re(z) = c_re(x) * c_re(y) - c_im(x) * c_im(y);
73   c_im(z) = c_im(x) * c_re(y) + c_re(x) * c_im(y);
74   return z;
75 }
76
77 COMPLEX
78 Cdiv(COMPLEX x, COMPLEX y) {
79   REAL d = sqr(c_re(y)) + sqr(c_im(y));
80   COMPLEX z;
81   c_re(z) = (c_re(x) * c_re(y) + c_im(x) * c_im(y)) / d;
82   c_im(z) = (c_re(y) * c_im(x) - c_im(y) * c_re(x)) / d;
83   return z;
84 }
85
86 REAL
87 Cmag(COMPLEX z) { return sqrt(sqr(z.re) + sqr(z.im)); }
88
89 REAL
90 Cabs(COMPLEX z) { return sqrt(sqr(z.re) + sqr(z.im)); }
91
92 REAL
93 Csqrmag(COMPLEX z) { return sqr(z.re) + sqr(z.im); }
94
95 COMPLEX
96 Cmplx(REAL x, IMAG y) {
97   COMPLEX z;
98   z.re = x, z.im = y;
99   return z;
100 }
101
102 COMPLEX
103 Conjg(COMPLEX z) { return Cmplx(z.re, -z.im); }
104
105 COMPLEX
106 Cexp(COMPLEX z) {
107   REAL r = exp(z.re);
108   return Cmplx(r * cos(z.im), r * sin(z.im));
109 }
110
111 COMPLEX
112 Cp2r(COMPLEX z) {
113   return Cmplx(z.re * cos(z.im), z.re * sin(z.im));
114 }
115
116 COMPLEX
117 Cr2p(COMPLEX z) {
118   return Cmplx(sqrt(sqr(z.re) + sqr(z.im)), ATAN2(z.im, z.re));
119 }