aboutsummaryrefslogtreecommitdiff
path: root/include/dspi/DSPIcomplex.h
blob: ad3e041338e5c087467f7b082c3f1823e45c4443 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 *   DSPIcomplex.h - Quick and dirty inline class for complex numbers 
 *   (mainly to compute filter poles/zeros, not to be used inside loops)
 *   Copyright (c) 2000 by Tom Schouten
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef DSPIcomplex_h
#define DSPIcomplex_h

#include <math.h>
#include <iostream>

class DSPIcomplex
{
    public:
        inline DSPIcomplex() {_r = _i = 0;}
        inline DSPIcomplex(const float &a, const float &b) {setCart(a, b);}
        inline DSPIcomplex(const float &phasor) {setAngle(phasor);}
        
        inline void setAngle(const float &angle) {_r = cos(angle); _i = sin(angle);}
        inline void setPolar(const float &phasor, const float &norm) 
        {_r = norm * cos(phasor); _i = norm * sin(phasor);}
        inline void setCart(const float &a, const float &b) {_r = a; _i = b;}
        
        inline const float& r() const {return _r;}
        inline const float& i() const {return _i;}
        
        inline float norm2() const {return _r*_r+_i*_i;}
        inline float norm() const {return sqrt(norm2());}
        inline void normalize() {float n = 1.0f / norm(); _r *= n; _i *= n;}
        
        inline DSPIcomplex conj() const {return DSPIcomplex(_r, -_i);}

        inline float angle() const {return atan2(_i, _r);}


        inline DSPIcomplex operator+ (const DSPIcomplex &a) const
        {
            return DSPIcomplex(_r + a.r(), _i + a.i());
        }
        inline DSPIcomplex operator+ (float f) const
        {
            return DSPIcomplex(_r + f, _i);
        }
        inline DSPIcomplex operator- (const DSPIcomplex &a) const
        {
            return DSPIcomplex(_r - a.r(), _i - a.i());
        }
        inline DSPIcomplex operator- (float f) const
        {
            return DSPIcomplex(_r - f, _i);
        }

        inline DSPIcomplex operator* (const DSPIcomplex &a) const 
        {
            return DSPIcomplex(_r * a.r() - _i * a.i(), _i * a.r() + _r * a.i());
        }
        inline DSPIcomplex operator* (float f) const
        {
            return DSPIcomplex(_r * f, _i * f);
        }
        inline DSPIcomplex operator/ (const DSPIcomplex &a) const 
        {
            float n_t = 1.0f / a.norm2();
            return DSPIcomplex(n_t * (_r * a.r() + _i * a.i()), n_t * (_i * a.r() - _r * a.i()));
        }
        inline DSPIcomplex operator/ (float f) const 
        {
            float n_t = 1.0f / f;
            return DSPIcomplex(n_t * _r, n_t * _i);
        }
        
        inline friend std::ostream& operator<< (std::ostream& o, DSPIcomplex& a)
        {
            return o << "(" << a.r() << "," << a.i() << ")";
        }

        inline friend DSPIcomplex operator+ (float f, DSPIcomplex& a)
        {
            return(DSPIcomplex(a.r() + f, a.i()));
        }
        
        inline friend DSPIcomplex operator- (float f, DSPIcomplex& a)
        {
            return(DSPIcomplex(f - a.r(), - a.i()));
        }
        
        inline friend DSPIcomplex operator/ (float f, DSPIcomplex& a)
        {
            return(DSPIcomplex(f,0) / a);
        }
        
        // ????
        inline friend DSPIcomplex operator* (float f, DSPIcomplex& a)
        {
            return(DSPIcomplex(f*a.r(), f*a.i()));
        }
        
        
        inline DSPIcomplex& operator *= (float f)
        {
            _r *= f;
            _i *= f;
            return *this;
        }

        inline DSPIcomplex& operator /= (float f)
        {
            _r /= f;
            _i /= f;
            return *this;
        }

        inline DSPIcomplex& operator *= (DSPIcomplex& a)
        {
            float r_t = _r * a.r() - _i * a.i();
                   _i = _r * a.i() + _i * a.r();
                   _r = r_t;
                   
            return *this;
        }

        inline DSPIcomplex& operator /= (DSPIcomplex& a)
        {
            float n_t = a.norm2();
            float r_t = n_t * (_r * a.r() + _i * a.i());
                   _i = n_t * (_i * a.r() - _r * a.i());
                   _r = r_t;
                   
            return *this;
        }

        
        float _r;
        float _i;
};


// COMPLEX LOG

inline DSPIcomplex dspilog(DSPIcomplex a) /* complex log */
{
    float r_t = log(a.norm());
    float i_t = a.angle();
    return DSPIcomplex(r_t, i_t);
}

// COMPLEX EXP

inline DSPIcomplex dspiexp(DSPIcomplex a) /* complex exp */
{
    return (DSPIcomplex(a.i()) * exp(a.r()));
}

// BILINEAR TRANSFORM analog -> digital

inline DSPIcomplex bilin_stoz(DSPIcomplex a)
{
    DSPIcomplex a2 = a * 0.5f;
    return((1.0f + a2)/(1.0f - a2));
}    

// BILINEAR TRANSFORM digital -> analog

inline DSPIcomplex bilin_ztos(DSPIcomplex a)
{
    return ((a - 1.0f) / (a + 1.0f))*2.0f;
}

// not really a complex function but a nice complement to the bilinear routines

inline float bilin_prewarp(float freq)
{
    return 2.0f * tan(M_PI * freq);
}    

#endif //DSPIcomplex_h