/* PZT_complex_math.h, part of PoZeTools version 0.52, march 30, 2005. Copyright (c) 2002-2005 Pieter Suurmond Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Any person wishing to distribute modifications to the Software is requested to send the modifications to the original developer so that they can be incorporated into the canonical version. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND ON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. All 6 complex math routines below write the result in the struct that the second argument points to. */ #define kPZT_onePi ((double) 3.1415926535897931) #define kPZT_twoPi ((double) 6.2831853071795862) #define kPZT_epsilon ((double) 0.000000005) /* Magnitudes below this are regarded as zero. */ typedef struct { double re; double im; } complexCartesian; typedef struct { double ang; double mag; } complexPolar; typedef struct /* Used by POZE objects. */ { int number; complexCartesian* cartesian; } cartesian_array; /*----------------------------------------------------------------------------*/ /* Multiplies a by b and stores the result in b. */ void polarMultiply(const complexPolar* a, complexPolar* b); /*----------------------------------------------------------------------------*/ /* Divides b by a and stores the result in *b. */ void polarDivide(const complexPolar* a, complexPolar* b); /*----------------------------------------------------------------------------*/ /* Adds a to b and stores the result in *b. */ void cartesianAdd(const complexCartesian* a, complexCartesian* b); /*----------------------------------------------------------------------------*/ /* Subtracts a from b and stores the result in *b. */ void cartesianSub(const complexCartesian* a, complexCartesian* b); /*----------------------------------------------------------------------------*/ /* Multiplies a by b and stores the result in *b. */ void cartesianMultiply(const complexCartesian* a, complexCartesian* b); /*----------------------------------------------------------------------------*/ /* Divides b by a and leaves the result in *b. */ void cartesianDivide(const complexCartesian* a, complexCartesian* b); /*----------------------------------------------------------------------------*/ /* Returns the length of c. */ double cartesianMagnitude(const complexCartesian* c); /*----------------------------------------------------------------------------*/ /* Reads cartesian in c and writes polar in p. Resulting angle varies between -PI and +PI, resulting magnitude will always be >= zero. */ void cartesianToPolar(const complexCartesian* c, complexPolar* p); /*----------------------------------------------------------------------------*/ /* Reads polar in p and writes cartesian in c. */ void polarToCartesian(const complexPolar* p, complexCartesian* c); /*----------------------------------------------------------------------------*/ double phaseUnwrap(double prev_angle, double curr_angle); /* May be used after atan2() for example. Needs a previous angle. Assumes no poles or zero ly between previous and current angle, or we might miss some phase discontinuity. */