OpenCores
URL https://opencores.org/ocsvn/complexarithmetic/complexarithmetic/trunk

Subversion Repositories complexarithmetic

[/] [complexarithmetic/] [trunk/] [cplxopsphasor.sc.h] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 tamero
 
2
#include <systemc.h>
3
#ifndef _CPLXOPSPHASOR_H_
4
#define _CPLXOPSPHASOR_H_
5
 
6
template<typename T>
7
class complex
8
{
9
    T real, imag;
10
    /*initialization of the real and imaginal parts of the complex values as 0
11
    definition of copy constructor and assignment operator*/
12
public:
13
    complex() : real(0),imag(0)
14
    {
15
    }
16
    complex( const T& real_, const T& imag_ ) : real(real_), imag(imag_){}
17
    complex( const complex& other ) : real(other.real), imag(other.imag){}
18
    complex& operator = ( const complex& other )
19
    {
20
        real = other.real;
21
        imag = other.imag;
22
        return *this;
23
    }
24
 
25
    /*definiton of mutators and access member functions*/
26
    T get_real() const
27
    {
28
        return real;
29
    }
30
    T get_imag() const
31
    {
32
        return imag;
33
    }
34
    void set_real( const T& real_ )
35
    {
36
        real = real_;
37
    }
38
    void set_imag( const T& imag_ )
39
    {
40
        imag = imag_;
41
    }
42
 
43
    /*Definition of member operators*/
44
    complex& operator += ( const complex& other )
45
    {
46
        real += other.real;
47
        imag += other.imag;
48
        return *this;
49
    }
50
    /*Definition of complex aritmetic operators as friend Operators.*/
51
    friend complex operator + ( const complex& a, const complex& b )  //approved
52
    {
53
        complex result;
54
        result.set_real( a.get_real() + b.get_real() );
55
        result.set_imag( a.get_imag() + b.get_imag() );
56
        return result;
57
    }
58
    friend complex operator * ( const complex& a, const complex& b )  //approved
59
    {
60
        complex result;
61
        sc_uint<8> areal, aimag, breal, bimag;       //real and imaginary parts of the operands
62
        sc_uint<16> tempreal,tempimag;               //temporary outputs (16 BITS)
63
 
64
        /*complex multiplication of two complex numnbers a and b is defined by
65
        a*b=(areal*breal-aimag*bimag)+i(areal*bimag+aimag*breal)*/
66
 
67
        areal=a.get_real();aimag=a.get_imag();
68
        breal=b.get_real();bimag=b.get_imag();
69
        tempreal=areal*breal-aimag*bimag;
70
        tempimag=aimag*breal+areal*bimag;
71
        result.set_real(tempreal);
72
        result.set_imag(tempimag);
73
        return result;                              //the result is returned
74
    }
75
 
76
    friend complex operator / ( const complex& a, const complex& b ) //approved
77
    {
78
        complex result;
79
        sc_uint<4> count=0;
80
        sc_uint<8> areal, aimag, breal, bimag;                                                   //real and imaginary parts of the operands
81
        sc_uint<8> tempxa=0,tempya=0,tempxb=0,tempyb=0,anglea=0,angleb=0,angleresult,rresult,angle=90,us=128;    //,raparameters to accumulate the temporary results of the operation
82
//        sc_uint<16> rb;
83
        areal=a.get_real(); breal=b.get_real(); aimag=a.get_imag(); b.get_imag();                       //read real and imaginary parts of the complex operands
84
        /*the operands will first be converted from cartesian to polar representation using cordic techniques
85
       then the operands will be divided as a/b=ra/rb(anglea-angleb) */
86
        if (areal[7]==1)                    //determination of the initial angular position of the operands
87
        {
88
            anglea=180;
89
        }
90
        if (breal[7]==1)
91
        {
92
            angleb=180;
93
        }
94
        while (count<=6)                    //CORDIC vector mode operation for converting 
95
        {                                   //the operands from cartesian to polar representation
96
            angle=angle>>1;
97
                        count++;
98
                        tempxa = areal>>count;
99
            tempya = aimag>>count;
100
            tempxb = breal>>count;
101
            tempyb = bimag>>count;
102
           if ( aimag[7]==1 )              //CORDIC operation for the first operand
103
            {
104
                areal=areal-tempya;
105
                aimag=aimag+tempxa;
106
                anglea=anglea-angle;
107
            }
108
            else //if ( aimag[7]==0 )
109
            {
110
                areal=areal+tempya;
111
                aimag=aimag-tempxa;
112
                anglea=anglea+angle;
113
           }
114
            if ( bimag[7]==0 )             //CORDIC operation for the second operand
115
            {
116
                breal=breal-tempyb;
117
                bimag=bimag+tempxb;
118
                angleb=angleb-angle;
119
            }
120
            else //if ( bimag[7]==0 )
121
            {
122
                breal=breal+tempyb;
123
                bimag=bimag-tempxb;
124
                angleb=angleb+angle;
125
           }
126
 
127
        }
128
        rresult=areal/breal;                      //divide the absolute values
129
        angleresult=anglea-angleb;          //subtract the polar angles
130
        count=0;
131
        sc_uint<8> y=0,tempr,tempy;
132
                angle=90;
133
        while (count<=6)                    //CORDIC vector mode operation for converting 
134
        {                                   //the result from polar to cartesian representation
135
            count++;
136
                        tempr = rresult>>count;
137
            tempy = y>>count;
138
            angle=angle>>1;
139
            if (angleresult[7]==0)
140
            {
141
                rresult=rresult-tempy;
142
                y=y+tempr;
143
                angleresult=angleresult-angle;
144
            }
145
            else if (angleresult[7]==1)
146
            {
147
                rresult=rresult+tempy;
148
                y=y-tempr;
149
                angleresult=angleresult+angle;
150
            }
151
         }
152
        result.set_real(rresult);
153
        result.set_imag(angleresult);
154
        return result;
155
   }
156
 
157
    friend complex pol2car ( const complex& a)//approved
158
    {
159
        complex result;
160
        sc_int <4> count=0;
161
        sc_uint <8> r,angle;                 //absolute value and polar angle of the operand
162
        sc_uint <8> tempr,tempy,tempangle=90,y; //temporary values for polar to cartesian operation
163
        r=a.get_real();
164
        angle=a.get_imag();
165
         while (count<6) //count= 1; count < 7; count++)  //Cordic operation for polar to cartesian transformation
166
        {
167
            count++;
168
                        tempr = r >>count;
169
            tempy = y >>count;
170
            tempangle=tempangle>>1;
171
            if (angle[7]==0)
172
            {
173
                r=r-tempy;
174
                y=y+tempr;
175
                angle=angle-tempangle;
176
            }
177
            else if (angle[7]==1)
178
            {
179
                r=r+tempy;
180
                y=y-tempr;
181
                angle=angle+tempangle;
182
            }
183
        }
184
        result.set_real(r);
185
        result.set_imag(y);
186
        return (result);
187
    }
188
 
189
    friend complex sqrt ( const complex& a)     //approved
190
    {    //sqrt(x+iy)==1/2sqrt(2)[sqrt(sqrt(x^2+y^2)+x)+isgn(y)sqrt(sqrt(x^2+y^2)-x)].
191
        complex result;
192
        int cnt=0;//,cnt1=1;
193
        sc_uint<8> tempabs, tempreal, tempimag, sgn, areal, aimag, abs, base1, base2, sqrt1,sqrt2;//ra,
194
        sc_uint<8> tempareal,tempaimag;
195
                areal=a.get_real();
196
        aimag=a.get_imag();
197
        abs=a.get_real();
198
        while (cnt<6)        //CORDIC vector mode to evaluate the absolute value of a
199
        {
200
            cnt++;
201
            tempareal = areal >>cnt;
202
            tempaimag = aimag >>cnt;
203
            if ( aimag[7]==1)
204
            {
205
                abs=abs-tempaimag;
206
                aimag=aimag+tempabs;
207
            }
208
            if ( aimag[7]==0)
209
            {
210
                abs=abs+tempaimag;
211
                aimag=aimag-tempabs;
212
            }
213
        }
214
        tempreal=abs+a.get_real();                  //sqrt(x^2+y^2)+x
215
        tempimag=abs-a.get_real();                  //sqrt(x^2+y^2)-x
216
        sqrt1=0;base1=8;sqrt2=0;base2=8;
217
        for (cnt= 0; cnt < 2; cnt++)//while (cnt1<5)                             //evaluating the temporary square roots using numeric techniques
218
        {                                           //sqrt(sqrt(x^2+y^2)+x),sqrt(sqrt(x^2+y^2)-x)
219
                 sqrt1 = base1 + sqrt1;
220
                if  ( (base1 * base1) > tempreal )
221
                {
222
                        sqrt1 = base1 - sqrt1 ;     // base should not have been added, so we substract again
223
                }
224
                base1>>1 ;                        // shift 1 digit to the right = divide baimag 2
225
                sqrt2 = base2 + sqrt2 ;
226
                if  ( (sqrt2 * sqrt2) > tempimag )
227
                {
228
                        sqrt2 = base2 - sqrt2 ;
229
                }
230
                base2>>1 ;
231
        }
232
        sqrt1=sqrt1>>1;                             //multiply the tmeporary results with 0.5 instead of 0.7
233
        sqrt2=sqrt2>>1;                             //1/2*sqrt(2)=
234
        sqrt2[7]=aimag[7];                          //apply the sign of the imaginary part
235
        result.set_real(sqrt1);
236
        result.set_imag(sqrt2);
237
        return result;
238
    }
239
 
240
    friend complex operator - ( const complex& a, const complex& b )//approved
241
    {
242
        complex result;
243
        result.set_real( a.get_real() - b.get_real() );
244
        result.set_imag( a.get_imag() - b.get_imag() );
245
        return result;
246
    }
247
 
248
    friend complex conj ( const complex& a )//approved
249
    {
250
        complex result;
251
                sc_uint<8> tempa,tempb;
252
                tempa=a.get_imag();
253
                tempb=tempa;
254
                tempb[7]=!tempa[7];
255
        result.set_real( a.get_real());
256
        result.set_imag( tempb);
257
        return result;
258
    }
259
    /*
260
     * Predicate operators
261
 
262
     */
263
    friend bool operator == ( const complex& a, const complex& b )
264
    {
265
        return ( (a.get_real() == b.get_real()) && (a.get_real() == b.get_real()) );
266
    }
267
    friend bool operator != ( const complex& a, const complex& b )
268
    {
269
        return ( ! (a == b) );
270
    }
271
};
272
 
273
 
274
 
275
#endif
276
 
277
 
278
 
279
 
280
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.