| 1 | /* NUMcomplex.cpp |
| 2 | * |
| 3 | * Copyright (C) 2017-2020, 2026 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <cmath> |
| 20 | #include "NUMcomplex.h"cxx . . $ `<file>/dwsys/NUMcomplex.h`/ |
| 21 | |
| 22 | void NUMextremacxx . . $ NUMextrema(bf89198a6522795f). (constCOMPVECcxx . . $ constCOMPVEC# const& v, double *min_re, double *max_re, double *min_im, double *max_im) { |
| 23 | integercxx . . $ integer# ndiv2 = v.sizecxx . . $ constvector#size. / 2; |
| 24 | const integercxx . . $ integer# start = v.sizecxx . . $ constvector#size. & 1 ? 2 : 1; // even or odd? |
| 25 | if (min_re || max_re) { |
| 26 | double max = v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1].realcxx . . $ std/complex#real(21b6c25e127938e2).(), min = max; |
| 27 | for (integercxx . . $ integer# i = start; i <= ndiv2; i ++) { |
| 28 | const integercxx . . $ integer# i2 = 2 * i, i1 = i2 - 1; |
| 29 | if (v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).11].realcxx . . $ std/complex#real(21b6c25e127938e2).() >= v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i2].realcxx . . $ std/complex#real(21b6c25e127938e2).()) |
| 30 | if (v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i1].realcxx . . $ std/complex#real(21b6c25e127938e2).() > max) |
| 31 | max = v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i1].realcxx . . $ std/complex#real(21b6c25e127938e2).(); |
| 32 | if (v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).11].realcxx . . $ std/complex#real(21b6c25e127938e2).() <= v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i2].realcxx . . $ std/complex#real(21b6c25e127938e2).()) |
| 33 | if (v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i1].realcxx . . $ std/complex#real(21b6c25e127938e2).() < min) |
| 34 | min = v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i1].realcxx . . $ std/complex#real(21b6c25e127938e2).(); |
| 35 | } |
| 36 | if (min_re) |
| 37 | *min_re = min; |
| 38 | if (max_re) |
| 39 | *max_re = max; |
| 40 | } |
| 41 | if (min_im || max_im) { |
| 42 | double max = v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(), min = max; |
| 43 | for (integercxx . . $ integer# i = start; i <= ndiv2; i ++) { |
| 44 | const integercxx . . $ integer# i2 = 2 * i, i1 = i2 - 1; |
| 45 | if (v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).11].imagcxx . . $ std/complex#imag(21b6c25e127938e2).() >= v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i2].imagcxx . . $ std/complex#imag(21b6c25e127938e2).()) |
| 46 | if (v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i1].imagcxx . . $ std/complex#imag(21b6c25e127938e2).() > max) |
| 47 | max = v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i1].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(); |
| 48 | if (v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).11].imagcxx . . $ std/complex#imag(21b6c25e127938e2).() <= v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i2].imagcxx . . $ std/complex#imag(21b6c25e127938e2).()) |
| 49 | if (v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i1].imagcxx . . $ std/complex#imag(21b6c25e127938e2).() < min) |
| 50 | min = v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i1].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(); |
| 51 | } |
| 52 | if (min_im) |
| 53 | *min_im = min; |
| 54 | if (max_im) |
| 55 | *max_im = max; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | The code to calculate the complex incomplete gamma function was translated from fortran code into c++ by David Weenink. |
| 61 | The fortran code is from the following article: |
| 62 | Eric Kostlan & Dmitry Gokhman, A program for calculating the incomplete gamma function. |
| 63 | Technical report, Dept. of Mathematics, Univ. of California, Berkeley, 1987. |
| 64 | |
| 65 | Their algorithm calcutes the complex incomplete gamma function Γ(α,x) by using the following formula: |
| 66 | (1) Γ(α,x)= exp(-x)x^α / h(α,x), |
| 67 | where h(α,x) is a continued fraction: |
| 68 | (2) h(α,x)=x+(1-α) |
| 69 | ----- |
| 70 | 1+1 |
| 71 | ------- |
| 72 | x+(2-α) |
| 73 | ------ |
| 74 | 1+2 |
| 75 | ------ |
| 76 | x+(3-α) |
| 77 | ------- |
| 78 | 1+... |
| 79 | Efficient calculation of h(α,x) is possible because consecutive terms of a continued fraction can be computed by means of |
| 80 | a two term linear recursion relation. |
| 81 | If for the sequences {p [k]} and {q [k]}, k=0,1,2,... the following difference equations hold |
| 82 | (3) y [k+2]=x*y [k+1]+((k+2)/2)* y [k] for k even |
| 83 | =y [k+1]((k+3)/2-α)*y [k] for k is odd |
| 84 | and initial conditions p [0]=x, p [1]=x+1-α, q [0]=1, q [1]=1, then p [k]/q [k] is the k-th term |
| 85 | in the continued fraction for h(α,x). |
| 86 | |
| 87 | The value of h(α+1,x) can be reduced to h(α,x) |
| 88 | (4) x/h(α+1,x) = α/h(α,x)+1 by using the following relation between gamma functions: |
| 89 | Γ(α+1,x)=αΓ(α,x)+x^α exp(-x). |
| 90 | If x is near the negative real axis then x can be moved to some new value y: |
| 91 | (5) Γ(α,y)-Γ(α,x)=sum(n=0,Infinity,(-1)^n(x^(α+n)-y^(α+n))/(n!(α+n)), |
| 92 | where in the code below a valye of y=1 is used to facilitate the computation. |
| 93 | |
| 94 | Numerical considerations: |
| 95 | 1. In formula (5) Kostlan & Gokhman use y=1 to facilitate the computation |
| 96 | 2. If (α,x) is near (−n ,0), where n is a non-negative integer, but x is not equal to zero, |
| 97 | the n-th term in (5) can turn out to be 0.0/0.0. They replace this term with -log(x)-(α+n)log(x)²/2. |
| 98 | Essentially they are helping the computer to calculate (1-x^(α+n))/(α+n). |
| 99 | |
| 100 | Problems: |
| 101 | Kostlan & Gokhman notice three intrinsic problems that any algorithm for calculating Γ(α,x) must face: |
| 102 | 1. For x=0 and α a nonpositive integer Γ(α,x)=∞. |
| 103 | 2. If α is not an integer Γ(α,x) is a multi-valued function with a branch point at x=0 |
| 104 | 3. As we approach ∞ along certain directions computation of Γ(α,x) is limited because of the inability to compute x^α accurately. |
| 105 | |
| 106 | */ |
| 107 | |
| 108 | static double norm1cxx . . $ norm1(2c56a730ee6e61ad). (const dcomplexcxx . . $ dcomplex# *x) { |
| 109 | return fabscxx . . $ fabs(fee4f1d22bc8e617). (realcxx . . $ std/real(bb57395c51337e15). (*x)) + fabscxx . . $ fabs(fee4f1d22bc8e617). (imagcxx . . $ std/imag(bb57395c51337e15). (*x)); |
| 110 | } |
| 111 | |
| 112 | static void xShiftTermcxx . . $ xShiftTerm(86f5ac97c10c65ed). (const dcomplexcxx . . $ dcomplex# *alpha, const dcomplexcxx . . $ dcomplex# *x, integercxx . . $ integer# i, dcomplexcxx . . $ dcomplex# *p, dcomplexcxx . . $ dcomplex# *q) { |
| 113 | // Calculate p*q = (-1)^i (1-x^(alpha+i))/(alpha+i)i! |
| 114 | constexpr dcomplexcxx . . $ dcomplex# zero = 0.0cxx . . $ std/complex#complex(bc12c9ec15586e6a).; |
| 115 | constexpr double tol = 3e-7, xlim = 39.0; |
| 116 | const double di = i; |
| 117 | |
| 118 | if (i == 0) |
| 119 | *q =cxx . . $ std/complex#`operator=`(3b07e393e346b962). 1.0; |
| 120 | const dcomplexcxx . . $ dcomplex# alphai = *alpha +cxx . . $ std/operator+(70c2920da18d026d). di; |
| 121 | if (*x ==cxx . . $ std/`operator==`(a126df8de2e660da). zero) { |
| 122 | *p =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). 1.0 /cxx . . $ std/`operator/`(16b4b0889fd250c1). alphai; |
| 123 | if (i != 0) |
| 124 | *q /=cxx . . $ std/complex#`operator/=`(3b07e393e346b962). -di; |
| 125 | return; |
| 126 | } |
| 127 | const dcomplexcxx . . $ dcomplex# cdlx = logcxx . . $ std/log(a358fd9634d01384). (*x); |
| 128 | /* |
| 129 | If (1-x**alphai) = -x**alphai, |
| 130 | then change the inductive scheme to avoid overflow. |
| 131 | */ |
| 132 | if (realcxx . . $ std/real(bb57395c51337e15). (alphai *cxx . . $ std/`operator*`(804c2413f71972e2). cdlx) > xlim && i != 0) { |
| 133 | *p *=cxx . . $ std/complex#`operator*=`(f1f782a256ae8b93). (alphai -cxx . . $ std/operator-(70c2920da18d026d). 1.0) /cxx . . $ std/`operator/`(804c2413f71972e2). alphai; |
| 134 | *q *=cxx . . $ std/complex#`operator*=`(f1f782a256ae8b93). -cxx . . $ std/operator-(a358fd9634d01384). *x /cxx . . $ std/`operator/`(70c2920da18d026d). di; |
| 135 | return; |
| 136 | } |
| 137 | if (norm1cxx . . $ norm1(2c56a730ee6e61ad). (& alphai) > tol) |
| 138 | *p =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). (1.0 -cxx . . $ std/operator-(16b4b0889fd250c1). stdcxx . . $ std/::powcxx . . $ std/pow(804c2413f71972e2). (*x, alphai)) /cxx . . $ std/`operator/`(804c2413f71972e2). alphai; |
| 139 | else |
| 140 | *p =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). -cxx . . $ std/operator-(a358fd9634d01384).cdlx *cxx . . $ std/`operator*`(804c2413f71972e2). (1.0 +cxx . . $ std/operator+(16b4b0889fd250c1). cdlx *cxx . . $ std/`operator*`(804c2413f71972e2). alphai *cxx . . $ std/`operator*`(70c2920da18d026d). 0.5); |
| 141 | if (i == 0) |
| 142 | *q /=cxx . . $ std/complex#`operator/=`(3b07e393e346b962). - di; |
| 143 | } |
| 144 | |
| 145 | static void continuedFractionExpansioncxx . . $ continuedFractionExpansion(4828e5cc0daad400). (const dcomplexcxx . . $ dcomplex# *alpha, const dcomplexcxx . . $ dcomplex# *x, dcomplexcxx . . $ dcomplex# *result) { |
| 146 | constexpr double tol1 = 1e10, tol2 = 1e-10, error = 1e-18; |
| 147 | constexpr dcomplexcxx . . $ dcomplex# zerocxx . . $ std/complex#complex(bc12c9ec15586e6a). (0.0,0.0); |
| 148 | dcomplexcxx . . $ dcomplex# q0 = 1.0cxx . . $ std/complex#complex(bc12c9ec15586e6a)., q1 = 1.0cxx . . $ std/complex#complex(bc12c9ec15586e6a)., p0 = *cxx . . $ std/complex#complex(20ef1611af677bca).x; |
| 149 | dcomplexcxx . . $ dcomplex# p1 = *x +cxx . . $ std/operator+(70c2920da18d026d). 1.0 -cxx . . $ std/operator-(804c2413f71972e2). *alpha, r0cxx . . $ std/complex#complex(bc12c9ec15586e6a).; |
| 150 | for (integercxx . . $ integer# i = 1; i <= 100000; i++) { |
| 151 | const double di = i; |
| 152 | if (p0 !=cxx . . $ std/`operator!=`(a126df8de2e660da). zero && q0 !=cxx . . $ std/`operator!=`(a126df8de2e660da). zero && q1 !=cxx . . $ std/`operator!=`(a126df8de2e660da). zero) { |
| 153 | r0 =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). p0 /cxx . . $ std/`operator/`(804c2413f71972e2). q0; |
| 154 | *result =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). p1 /cxx . . $ std/`operator/`(804c2413f71972e2). q1; |
| 155 | const dcomplexcxx . . $ dcomplex# r0mr1 = r0 -cxx . . $ std/operator-(804c2413f71972e2). *result; |
| 156 | if (norm1cxx . . $ norm1(2c56a730ee6e61ad). (& r0mr1) < norm1cxx . . $ norm1(2c56a730ee6e61ad). (result) * error) |
| 157 | return; |
| 158 | /* |
| 159 | Renormalize to avoid underflow or overflow |
| 160 | */ |
| 161 | if (norm1cxx . . $ norm1(2c56a730ee6e61ad). (& p0) > tol1 || norm1cxx . . $ norm1(2c56a730ee6e61ad). (& p0) < tol2 || norm1cxx . . $ norm1(2c56a730ee6e61ad). (& q0) > tol1 || norm1cxx . . $ norm1(2c56a730ee6e61ad). (& q0) < tol2) { |
| 162 | const dcomplexcxx . . $ dcomplex# factor = p0 *cxx . . $ std/`operator*`(804c2413f71972e2). q0; |
| 163 | p0 /=cxx . . $ std/complex#`operator/=`(f1f782a256ae8b93). factor; |
| 164 | q0 /=cxx . . $ std/complex#`operator/=`(f1f782a256ae8b93). factor; |
| 165 | p1 /=cxx . . $ std/complex#`operator/=`(f1f782a256ae8b93). factor; |
| 166 | q1 /=cxx . . $ std/complex#`operator/=`(f1f782a256ae8b93). factor; |
| 167 | } |
| 168 | p0 =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). *x *cxx . . $ std/`operator*`(804c2413f71972e2). p1 +cxx . . $ std/operator+(804c2413f71972e2). di *cxx . . $ std/`operator*`(16b4b0889fd250c1). p0; // y [k+2] = x * y [k+1] + (k+2)/2 * y [k] with k even |
| 169 | q0 =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). *x *cxx . . $ std/`operator*`(804c2413f71972e2). q1 +cxx . . $ std/operator+(804c2413f71972e2). di *cxx . . $ std/`operator*`(16b4b0889fd250c1). q0; |
| 170 | p1 =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). p0 +cxx . . $ std/operator+(804c2413f71972e2). (di + 1.0 -cxx . . $ std/operator-(16b4b0889fd250c1). *alpha) *cxx . . $ std/`operator*`(804c2413f71972e2). p1; // y [k+2] = y [k+1] + ((k+3)/2 - alpha) * y [k] with k odd |
| 171 | q1 =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). q0 +cxx . . $ std/operator+(804c2413f71972e2). (di + 1.0 -cxx . . $ std/operator-(16b4b0889fd250c1). *alpha) *cxx . . $ std/`operator*`(804c2413f71972e2). q1; |
| 172 | } else { |
| 173 | // We should not come here at all! |
| 174 | *result =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). 0.5 *cxx . . $ std/`operator*`(16b4b0889fd250c1). (r0 +cxx . . $ std/operator+(804c2413f71972e2). *result); |
| 175 | return; |
| 176 | } |
| 177 | } |
| 178 | // We should not come here at all! |
| 179 | *result =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). 0.5 *cxx . . $ std/`operator*`(16b4b0889fd250c1). (r0 +cxx . . $ std/operator+(804c2413f71972e2). *result); |
| 180 | } |
| 181 | |
| 182 | static void shiftAlphaByOnecxx . . $ shiftAlphaByOne(4828e5cc0daad400). (const dcomplexcxx . . $ dcomplex# *alpha, const dcomplexcxx . . $ dcomplex# *x, dcomplexcxx . . $ dcomplex# *result) { |
| 183 | constexpr dcomplexcxx . . $ dcomplex# onecxx . . $ std/complex#complex(bc12c9ec15586e6a). (1.0, 0.0); |
| 184 | const integercxx . . $ integer# n = (integercxx . . $ integer#) (realcxx . . $ std/real(bb57395c51337e15). (*alpha) - realcxx . . $ std/real(bb57395c51337e15). (*x)); |
| 185 | if (n > 0) { |
| 186 | dcomplexcxx . . $ dcomplex# cn = ncxx . . $ std/complex#complex(bc12c9ec15586e6a). + 1; |
| 187 | dcomplexcxx . . $ dcomplex# term = one /cxx . . $ std/`operator/`(804c2413f71972e2). *x; |
| 188 | dcomplexcxx . . $ dcomplex# sum = termcxx . . $ std/complex#complex(20ef1611af677bca).; |
| 189 | const dcomplexcxx . . $ dcomplex# alpha1 = *alpha -cxx . . $ std/operator-(804c2413f71972e2). cn; |
| 190 | for (integercxx . . $ integer# i = 1; i <= n; i ++) { |
| 191 | cn =cxx . . $ std/complex#`operator=`(3b07e393e346b962). n - i + 1; |
| 192 | term *=cxx . . $ std/complex#`operator*=`(f1f782a256ae8b93). (alpha1 +cxx . . $ std/operator+(804c2413f71972e2). cn) /cxx . . $ std/`operator/`(804c2413f71972e2). *x; |
| 193 | sum +=cxx . . $ std/complex#`operator+=`(f1f782a256ae8b93). term; |
| 194 | } |
| 195 | continuedFractionExpansioncxx . . $ continuedFractionExpansion(4828e5cc0daad400). (& alpha1, x, result); |
| 196 | sum +=cxx . . $ std/complex#`operator+=`(f1f782a256ae8b93). term *cxx . . $ std/`operator*`(804c2413f71972e2). alpha1 /cxx . . $ std/`operator/`(804c2413f71972e2). *result; |
| 197 | *result =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). one /cxx . . $ std/`operator/`(804c2413f71972e2). sum; |
| 198 | } else { |
| 199 | continuedFractionExpansioncxx . . $ continuedFractionExpansion(4828e5cc0daad400). (alpha, x, result); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // Gamma [alpha,x] = integral{x, infty, t^(alpha-1)exp(-t)dt}, Gamma [alpha]= Gamma [alpha,0] |
| 204 | dcomplexcxx . . $ dcomplex# NUMincompleteGammaFunctioncxx . . $ NUMincompleteGammaFunction(ce4e3faad6c748f3). (const dcomplexcxx . . $ dcomplex# alpha, const dcomplexcxx . . $ dcomplex# x) { |
| 205 | dcomplexcxx . . $ dcomplex# resultcxx . . $ std/complex#complex(bc12c9ec15586e6a).; |
| 206 | constexpr double xlim = 1.0; |
| 207 | constexpr integercxx . . $ integer# ibuf = 34; |
| 208 | constexpr dcomplexcxx . . $ dcomplex# re = 0.36787944117144232cxx . . $ std/complex#complex(bc12c9ec15586e6a)., one = 1.0cxx . . $ std/complex#complex(bc12c9ec15586e6a).; |
| 209 | dcomplexcxx . . $ dcomplex# pcxx . . $ std/complex#complex(bc12c9ec15586e6a)., qcxx . . $ std/complex#complex(bc12c9ec15586e6a)., rcxx . . $ std/complex#complex(bc12c9ec15586e6a).; |
| 210 | if (norm1cxx . . $ norm1(2c56a730ee6e61ad). (& x) < xlim || (realcxx . . $ std/real(bb57395c51337e15). (x) < 0.0 && fabscxx . . $ fabs(fee4f1d22bc8e617). (imagcxx . . $ std/imag(bb57395c51337e15). (x)) < xlim)) { |
| 211 | shiftAlphaByOnecxx . . $ shiftAlphaByOne(4828e5cc0daad400). (& alpha, & one, & r); |
| 212 | result =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). re /cxx . . $ std/`operator/`(804c2413f71972e2). r; |
| 213 | const integercxx . . $ integer# ilim = (integercxx . . $ integer#) realcxx . . $ std/real(bb57395c51337e15). (x /cxx . . $ std/`operator/`(804c2413f71972e2). re); |
| 214 | for (integercxx . . $ integer# i = 0; i <= ibuf - ilim; i++) { |
| 215 | xShiftTermcxx . . $ xShiftTerm(86f5ac97c10c65ed). (& alpha, & x, i, & p, & q); |
| 216 | result +=cxx . . $ std/complex#`operator+=`(f1f782a256ae8b93). p *cxx . . $ std/`operator*`(804c2413f71972e2). q; |
| 217 | } |
| 218 | } else { |
| 219 | shiftAlphaByOnecxx . . $ shiftAlphaByOne(4828e5cc0daad400). (& alpha, & x, & r); |
| 220 | result =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). expcxx . . $ std/exp(a358fd9634d01384). (-cxx . . $ std/operator-(a358fd9634d01384).x +cxx . . $ std/operator+(804c2413f71972e2). alpha *cxx . . $ std/`operator*`(804c2413f71972e2). logcxx . . $ std/log(a358fd9634d01384). (x)) /cxx . . $ std/`operator/`(804c2413f71972e2). r; |
| 221 | } |
| 222 | return {cxx . . $ std/complex#complex(bc12c9ec15586e6a). realcxx . . $ std/real(bb57395c51337e15). (result), imagcxx . . $ std/imag(bb57395c51337e15). (result) }; |
| 223 | } |
| 224 | |
| 225 | // End of translated fortran code |
| 226 | |
| 227 | /* |
| 228 | * We have to scale the amplitude "a" of the gammatone such that the response is 0 dB at the peak (w=w0); |
| 229 | * |
| 230 | * To calculate the spectrum of the truncated gammatone |
| 231 | * g(t) = t^(n-1)*exp(-b*t)cos(w0*t+phi) for 0 < t <= T |
| 232 | * g(t) = 0 for t > T |
| 233 | * we can write |
| 234 | * g(t)= t^(n-1)*exp(-b*t)(exp(I*(w0*t+phi))+exp(-I*(w0*t+phi)))/2 |
| 235 | * = (gp(t)+gm(t))/2, where |
| 236 | * gp(t) = t^(n-1)*exp(-b*t + I*(w0*t+phi)) |
| 237 | * gm(t) = t^(n-1)*exp(-b*t - I*(w0*t+phi)) |
| 238 | * |
| 239 | * Laplace [g(t)]= Laplace [g(t)]-Laplace [g(t+T)],where |
| 240 | * g(t+T) = g2(t) = (t+T)^(n-1)*exp(-b*(t+T))cos(w0*(t+T)+phi) for t>0 |
| 241 | * |
| 242 | * g2(t) = (t+T)^(n-1)*(exp(-b*(t+T)+I(w0*(t+T)+phi) + exp(-b*(t+T)-I(w0*(t+T)+phi)))/2 |
| 243 | * = (exp(-b*T+I*w0*T+I*phi)(t+T)^(n-1)exp(-b*t+I*w0*t) + exp(-b*T-I*w0*T-I*phi)(t+T)^(n-1)exp(-b*t-I*w0*t))/2 |
| 244 | * = (gpT(t)+gmT(t))/2, where |
| 245 | * gpT(t)= exp(-b*T+I*w0*T+I*phi) (t+T)^(n-1) exp(-b*t+I*w0*t) |
| 246 | * gmT(t)= exp(-b*T-I*w0*T-I*phi) (t+T)^(n-1) exp(-b*t-I*w0*t) |
| 247 | * |
| 248 | * Fp(w) = Laplace [gp(t), s=Iw] = exp( I*phi) (b+I(w-w0))^-n Gamma [n] |
| 249 | * Fm(w) = Laplace [gm(t), s=Iw] = exp(-I*phi) (b+I(w+w0))^-n Gamma [n] |
| 250 | * FpT(w) = Laplace [gpT(t),s=Iw] = exp( I*phi) exp(I*w*T) (b+I(w-w0))^-n Gamma [n, b*T+I(w-w0)*T] |
| 251 | * FmT(w) = Laplace [gmT(t),s=Iw] = exp(-I*phi) exp(I*w*T) (b+I(w+w0))^-n Gamma [n, b*T+I(w+w0)*T] |
| 252 | * F(g(t)) = (Fp(w) + Fm(w) - FpT(w) - FmT(w)) / 2 |
| 253 | * |
| 254 | * At resonance w=w0: |
| 255 | * F(w0) = (Fp(w0) + Fm(w0) - FpT(w0) - FmT(w0)) / 2 = |
| 256 | * = (exp(I*phi) (b)^-n Gamma [n] + exp(-I*phi) (b+I*2*w0)^-n Gamma [n] - |
| 257 | * exp(I*phi) exp(I*w0*T) (b)^-n Gamma [n, b*T] - |
| 258 | * exp(-I*phi) exp(I*w0*T) (b+I*2*w0)^-n Gamma [n, b*T+I*2*w0)]) / 2 |
| 259 | * = b^-n(exp(I*phi) Gamma [n] + exp(-I*phi) (1+I*2*w0/b)^-n Gamma [n] - |
| 260 | * exp(I*phi) exp(I*w0*T) Gamma [n, b*T0] - exp(-I*phi) exp(I*w0*T) (1+I*2*w0/b)^-n Gamma [n, b*T+I*2*w0*T)]) / 2 |
| 261 | * |
| 262 | * (x+I*y)^-n = (r*exp(I theta))^-n, where r = sqrt(x^2+y^2) and theta = ArcTan (y/x) |
| 263 | * (1+I*a)^-n = (1+a^2)^(-n/2) exp(-I*n*theta) |
| 264 | */ |
| 265 | dcomplexcxx . . $ dcomplex# gammaToneFilterResponseAtCentreFrequencycxx . . $ gammaToneFilterResponseAtCentreFrequency(4dfd279cecbac3cc). (double centre_frequency, double bandwidth, double gamma, double initialPhase, double truncationTime) { |
| 266 | const double b = NUM2picxx . . $ NUM2pi. * bandwidth, w0 = NUM2picxx . . $ NUM2pi. * centre_frequency, theta = atancxx . . $ atan(fee4f1d22bc8e617). (2.0 * centre_frequency / bandwidth); |
| 267 | const double gamma_n = expcxx . . $ exp(fee4f1d22bc8e617). (NUMlnGammacxx . . $ NUMlnGamma(447c67c6676f75c3). (gamma)), bpow = powcxx . . $ pow(b71928d39fde4295). (b, -gamma); |
| 268 | const dcomplexcxx . . $ dcomplex# expiphicxx . . $ std/complex#complex(bc12c9ec15586e6a). (coscxx . . $ cos(fee4f1d22bc8e617). (initialPhase), sincxx . . $ sin(fee4f1d22bc8e617). (initialPhase)), expmiphi = conjcxx . . $ std/conj(a358fd9634d01384). (expiphi); |
| 269 | const dcomplexcxx . . $ dcomplex# expnithetacxx . . $ std/complex#complex(bc12c9ec15586e6a). (coscxx . . $ cos(fee4f1d22bc8e617). (gamma * theta), - sincxx . . $ sin(fee4f1d22bc8e617). (gamma * theta)); |
| 270 | const dcomplexcxx . . $ dcomplex# expiw0Tcxx . . $ std/complex#complex(bc12c9ec15586e6a). (coscxx . . $ cos(fee4f1d22bc8e617). (w0 * truncationTime), sincxx . . $ sin(fee4f1d22bc8e617). (w0 * truncationTime)); |
| 271 | const dcomplexcxx . . $ dcomplex# peak = expnitheta *cxx . . $ std/`operator*`(70c2920da18d026d). powcxx . . $ pow(b71928d39fde4295). (1.0 + 4.0 * (w0 / b) * (w0 / b), - 0.5 * gamma); |
| 272 | const dcomplexcxx . . $ dcomplex# r1 = NUMincompleteGammaFunctioncxx . . $ NUMincompleteGammaFunction(ce4e3faad6c748f3). (dcomplexcxx . . $ std/complex#complex(bc12c9ec15586e6a). {gamma, 0.0},dcomplexcxx . . $ std/complex#complex(bc12c9ec15586e6a). { b * truncationTime, 0.0}); |
| 273 | const dcomplexcxx . . $ dcomplex# r2 = NUMincompleteGammaFunctioncxx . . $ NUMincompleteGammaFunction(ce4e3faad6c748f3). (dcomplexcxx . . $ std/complex#complex(bc12c9ec15586e6a). {gamma, 0.0}, dcomplexcxx . . $ std/complex#complex(bc12c9ec15586e6a). {b * truncationTime, 2.0 * w0 * truncationTime}); |
| 274 | const dcomplexcxx . . $ dcomplex# response = 0.5 * bpow *cxx . . $ std/`operator*`(16b4b0889fd250c1). ((expiphi +cxx . . $ std/operator+(804c2413f71972e2). expmiphi *cxx . . $ std/`operator*`(804c2413f71972e2). peak) *cxx . . $ std/`operator*`(70c2920da18d026d). gamma_n -cxx . . $ std/operator-(804c2413f71972e2). |
| 275 | expiw0T *cxx . . $ std/`operator*`(804c2413f71972e2). (expiphi *cxx . . $ std/`operator*`(804c2413f71972e2). r1 +cxx . . $ std/operator+(804c2413f71972e2). expmiphi *cxx . . $ std/`operator*`(804c2413f71972e2). peak *cxx . . $ std/`operator*`(804c2413f71972e2). r2)); |
| 276 | return responsecxx . . $ std/complex#complex(20ef1611af677bca).; |
| 277 | } |
| 278 | |
| 279 | /* End of file NUMcomplex.cpp */ |