public void DrawFilterCurve( Rect r, float[] coeffs, bool lowGain, bool midGain, bool highGain, Color color, bool useLogScale, bool filled, double masterGain, double samplerate, double magScale) { double wm = -2.0f * 3.1415926 / samplerate; ComplexD one = new ComplexD(1.0f, 0.0f); AudioCurveRendering.AudioCurveEvaluator d = delegate(float x) { ComplexD w = ComplexD.Exp(wm * GUIHelpers.MapNormalizedFrequency((double)x, samplerate, useLogScale, true)); ComplexD hl = (!lowGain) ? one : (w * (w * coeffs[0] + coeffs[1]) + coeffs[2]) / (w * (w * coeffs[3] + coeffs[4]) + 1.0f); ComplexD hp = (!midGain) ? one : (w * (w * coeffs[5] + coeffs[6]) + coeffs[7]) / (w * (w * coeffs[8] + coeffs[9]) + 1.0f); ComplexD hh = (!highGain) ? one : (w * (w * coeffs[10] + coeffs[11]) + coeffs[12]) / (w * (w * coeffs[13] + coeffs[14]) + 1.0f); ComplexD h = hh * hp * hl; double mag = masterGain + 10.0 * Math.Log10(h.Mag2()); return((float)(mag * magScale)); }; if (filled) { AudioCurveRendering.DrawFilledCurve(r, d, color); } else { AudioCurveRendering.DrawCurve(r, d, color); } }
public static ComplexD[] ToCudafy(double[] input) { var result = new ComplexD[input.Length]; ToCudafy(input, result); return(result); }
public static ComplexD[] ToCudafy(IEnumerable <double> input) { var result = new ComplexD[input.Count()]; ToCudafy(input.ToArray(), result); return(result); }
public static void ToCudafy(double[] input, ComplexD[] output) { for (int idx = 0; idx < input.Length; idx++) { output[idx] = new ComplexD(input[idx], 0); } }
public void SetUp() { _gpu = CudafyHost.CreateDevice(CudafyModes.Target); _hostInput = new double[N * BATCH]; _hostInputCplx = new ComplexD[N * BATCH]; _hostOutput = new double[N * BATCH]; _hostOutputCplx = new ComplexD[N * BATCH]; _devInput = _gpu.Allocate(_hostInput); _devInputCplx = _gpu.Allocate(_hostInputCplx); _devInter = _gpu.Allocate <double>(N * 2 * BATCH); _devInterCplx = _gpu.Allocate <ComplexD>(N * BATCH); _devOutput = _gpu.Allocate(_hostOutput); _devOutputCplx = _gpu.Allocate(_hostOutputCplx); _fft = GPGPUFFT.Create(_gpu); for (int b = 0; b < BATCH; b++) { for (int i = 0; i < N; i++) { ComplexD cf = new ComplexD(); cf.x = (double)((10.0F * Math.Sin(100 * 2 * Math.PI * i / N * Math.PI / 180))); cf.y = (double)((10.0F * Math.Sin(200 * 2 * Math.PI * i / N * Math.PI / 180))); _hostInput[i + b * N] = cf.x; _hostInputCplx[i + b * N] = cf; } } }
private static void AreEqualAngle(Num.Complex a, ComplexD b) { AreEqual( new Num.Complex(a.Real % Constant.PiHalf, a.Imaginary), new ComplexD(b.Real % Constant.PiHalf, b.Imag) ); }
private static bool Verify(ComplexD x, ComplexD y, float delta) { if (Math.Abs(x.x - y.x) > delta || Math.Abs(x.y - y.y) > delta) { return(false); } return(true); }
private static ComplexD arcsin(ComplexD c) { ComplexD x1 = new ComplexD(-c.y, c.x); ComplexD one = new ComplexD(1, 0); ComplexD x2 = sqrt(ComplexD.Subtract(one, ComplexD.Multiply(c, c))); return(ComplexD.Multiply(new ComplexD(0, -1), log(ComplexD.Add(x1, x2)))); }
public void TestAddD() { var x = new ComplexD(1.1, 2.5); var y = new ComplexD(3.4, 7.8); var z = x + y; Assert.AreEqual<ComplexD>(new ComplexD(4.5, 10.3), z); }
public void Test_getValue_complexD() { _gpucplxDBufferIn = _gpu.Allocate(_cplxDBufferIn); _gpu.CopyToDevice(_cplxDBufferIn, _gpucplxDBufferIn); ComplexD cd = _gpu.GetValue(_gpucplxDBufferIn, N / 32); Assert.AreEqual(_cplxDBufferIn[N / 32], cd); ClearOutputsAndGPU(); }
private static ComplexD th(ComplexD c) { ComplexD sn = sh(c); ComplexD cs = ch(c); if (cs.x == 0 && cs.y == 0) { return(new ComplexD(double.MaxValue, 0)); } return(ComplexD.Divide(sn, cs)); }
private static ComplexD tg(ComplexD c) { ComplexD sn = sin(c); ComplexD cs = cos(c); if (cs.x == 0 && cs.y == 0) { return(new ComplexD(1e37F, 0)); } return(ComplexD.Divide(sn, cs)); }
private static ComplexD cth(ComplexD c) { ComplexD sn = sh(c); ComplexD cs = ch(c); if (sn.x == 0 && sn.y == 0) { return(new ComplexD(double.MaxValue, 0)); } return(ComplexD.Divide(cs, sn)); }
public static void complexDiv(GThread thread, ComplexD[,] a, ComplexD[,] b, ComplexD[,] c) { int x = thread.blockIdx.x; int y = 0; while (y < YSIZE) { c[x, y] = ComplexD.Divide(a[x, y], b[x, y]); y++; } }
private static int smethod_8(int n, IList <double> a, ComplexD root) { double num = -2.0 * root.Real; double modulusSquared = root.GetModulusSquared(); IList <double> doubleList; (doubleList = a)[1] = doubleList[1] - num * a[0]; for (int index = 2; index < n - 1; ++index) { a[index] = a[index] - num * a[index - 1] - modulusSquared * a[index - 2]; } return(n - 2); }
private static void AreEqual(Num.Complex a, ComplexD b) { bool cond = (a.Real.ApproximateEquals(b.Real, Epsilon) && a.Imaginary.ApproximateEquals(b.Imag, Epsilon)) || (a.Real.Equals(b.Real) && a.Imaginary.Equals(b.Imag)); if (!cond) { Debugger.Break(); } Assert.IsTrue(cond, "{0} != {1}", a, b); }
private static ComplexD pow(ComplexD c1, ComplexD c2) { double a = abs(c1); double b = arg(c1); double c = c2.x; double d = c2.y; if (a == 0) { a = 1e-38F; } return(makeComplexD(Cudafy.GMath.Pow((float)a, (float)c) * Cudafy.GMath.Exp((float)(-b * d)), Cudafy.GMath.Log((float)a) * d + b * c)); }
private static ComplexD smethod_5(int n, IList <double> a, ComplexD z) { double num1 = -2.0 * z.Real; double modulusSquared = z.GetModulusSquared(); double num2 = 0.0; double num3 = a[0]; for (int index = 1; index < n; ++index) { double num4 = a[index] - num1 * num3 - modulusSquared * num2; num2 = num3; num3 = num4; } return(new ComplexD(a[n] + z.Real * num3 - modulusSquared * num2, z.Imaginary * num3)); }
public static void complexAbs(GThread thread, ComplexD[,] a, ComplexD[,] c) { int x = thread.blockIdx.x; int y = 0; double j = 1.0; double k = 2.0; ComplexD cf = new ComplexD(3 + j, 7 + k); while (y < YSIZE) { c[x, y].x = ComplexD.Abs(a[x, y]); c[x, y].y = cf.y;// 0.0F; y++; } }
private static int getSequenceDivergence(ComplexD c, int rpnLength, byte[] rpnFormula, ComplexD[] rpnKoef, ComplexD init, double infty, int steps, ComplexD[] stackMem, int stackOffset) { // DEBUG //return Mandelbrot(c); ComplexD z = init; for (int i = steps - 1; i >= 1; i--) { z = eval(rpnLength, rpnFormula, rpnKoef, c, z, stackMem, stackOffset); if (abs(z) > infty) { return(i); } } return(0); }
public ComplexD[] ComputeFft() { this.RefreshTime(); if (this.signals.Count <= 0) { this.fft = new ComplexD[0]; return(this.fft); } fft = new ComplexD[signals.Count]; if (signals.Count <= 0) { return(fft); } this.fft = CtkNumContext.GetOrCreate().FftForwardD(this.signals); return(fft); }
public ComplexD[] FftForward(ComplexD[] input) { var dev_cm = Gpu.CopyToDevice(input); var ifftData = new ComplexD[input.Length]; var dev_ifftData = Gpu.CopyToDevice(ifftData); Cudafy.Maths.FFT.GPGPUFFT gpuFFT = Cudafy.Maths.FFT.GPGPUFFT.Create(Gpu); Cudafy.Maths.FFT.FFTPlan1D fft_1d = gpuFFT.Plan1D( Cudafy.Maths.FFT.eFFTType.Complex2Complex, Cudafy.Maths.FFT.eDataType.Double, input.Length, 1); fft_1d.Execute <ComplexD, ComplexD>(dev_cm, dev_ifftData, true); Gpu.CopyFromDevice(dev_ifftData, ifftData); return(ifftData); }
private static void GetRandomComplex(RandomSystem rnd, out Num.Complex c1, out ComplexD c2, bool withInf = true) { var type = rnd.UniformDouble(); if (type < 0.1) { var v = rnd.UniformV2i(2); c1 = new Num.Complex(v.X, v.Y); c2 = new ComplexD(v.X, v.Y); } else if (type < 0.2 && withInf) { var i = rnd.UniformV2i(3); var v = new V2d( (i.X == 0) ? 0 : ((i.X == 1) ? double.NegativeInfinity : double.PositiveInfinity), (i.Y == 0) ? 0 : ((i.Y == 1) ? double.NegativeInfinity : double.PositiveInfinity) ); c1 = new Num.Complex(v.X, v.Y); c2 = new ComplexD(v.X, v.Y); } else { var v = (rnd.UniformV2d() - 0.5) * 100; if (type < 0.4) { c1 = new Num.Complex(v.X, 0); c2 = new ComplexD(v.X, 0); } else if (type < 0.5) { c1 = new Num.Complex(0, v.Y); c2 = new ComplexD(0, v.Y); } else { c1 = new Num.Complex(v.X, v.Y); c2 = new ComplexD(v.X, v.Y); } } }
private static double smethod_6(int n, IList <double> a, ComplexD z) { double num1 = -2.0 * z.Real; double modulusSquared = z.GetModulusSquared(); double num2 = System.Math.Sqrt(modulusSquared); double num3 = 0.0; double num4 = a[0]; double num5 = System.Math.Abs(num4) * (7.0 / 9.0); for (int index = 1; index < n; ++index) { double num6 = a[index] - num1 * num4 - modulusSquared * num3; num3 = num4; num4 = num6; num5 = num2 * num5 + System.Math.Abs(num6); } double num7 = a[n] + z.Real * num4 - modulusSquared * num3; double num8 = (9.0 * (num2 * num5 + System.Math.Abs(num7)) - 7.0 * (System.Math.Abs(num7) + System.Math.Abs(num4) * num2) + System.Math.Abs(z.Real) * System.Math.Abs(num4) * 2.0) * Class64.double_0; return(num8 * num8); }
public void DrawFilterCurve( Rect r, float[] coeffs, Color color, int numModes, bool useLogScale, bool filled, double samplerate, double magScale) { double wm = -2.0f * 3.1415926 / samplerate; ComplexD zero = new ComplexD(0.0f, 0.0f); ComplexD one = new ComplexD(1.0f, 0.0f); AudioCurveRendering.AudioCurveEvaluator d = delegate(float x) { ComplexD w = ComplexD.Exp(wm * GUIHelpers.MapNormalizedFrequency((double)x, samplerate, useLogScale, true)); ComplexD h = zero; int num = numModes * 3; for (int n = 0; n < num; n += 3) { h += coeffs[n] * (one - w * w) / (w * (w * coeffs[n + 2] + coeffs[n + 1]) + 1.0); } double mag = 10.0 * Math.Log10(h.Mag2()); return((float)(mag * magScale)); }; if (filled) { AudioCurveRendering.DrawFilledCurve(r, d, color); } else { AudioCurveRendering.DrawCurve(r, d, color); } }
private static int Mandelbrot(ComplexD c) { //DEBUG = MANDELBROT double r = Cudafy.GMath.Sqrt((float)((c.x - 0.25F) * (c.x - 0.25F) + c.y * c.y)); double t = Cudafy.GMath.Atan2((float)c.y, (float)c.x); if (r <= 0.5 * (1 - Cudafy.GMath.Cos((float)t))) { return(0); } ComplexD z = new ComplexD(0, 0); for (int i = 0; i < 200; i++) { z = ComplexD.Add(ComplexD.Multiply(z, z), c); if (z.x * z.x + z.y * z.y > 4) { return(1); } } return(0); }
private static void setPixel(GThread thread, int W, int H, int rpnLength, byte[] bmp, byte[] colorMap, ComplexD[] stackMem, byte[] rpnFormula, ComplexD[] rpnKoef, double rotation, double scale, ComplexD ctr, ComplexD init, double infty, int steps) { int pixelId = thread.blockIdx.x * thread.blockDim.x + thread.threadIdx.x; int x = pixelId % (2 * W); int y = pixelId / (2 * W); if (y >= 2 * H) { return; } ComplexD c1 = new ComplexD(Cudafy.GMath.Cos((float)rotation), Cudafy.GMath.Sin((float)rotation)); ComplexD c2 = new ComplexD((x - W) * (scale / W), (y - H) * (scale / W)); ComplexD c = ComplexD.Add(ComplexD.Multiply(c1, c2), ctr); int clr = getSequenceDivergence(c, rpnLength, rpnFormula, rpnKoef, init, infty, steps, stackMem, Function.MAX_STACK_SIZE * pixelId); bmp[3 * pixelId] = colorMap[3 * clr]; bmp[3 * pixelId + 1] = colorMap[3 * clr + 1]; bmp[3 * pixelId + 2] = colorMap[3 * clr + 2]; }
public static Complex ToSysComplex(ComplexD d) { return(new Complex(d.x, d.y)); }
public static void smethod_0(IList <double> coefficients, IList <double> roots, double epsilon) { if (roots == null) { throw new ArgumentNullException(nameof(roots)); } if (coefficients.Count < 2) { throw new ArgumentException("Number of coefficients must be 2 or greater."); } if (coefficients[coefficients.Count - 1] == 0.0) { throw new ArgumentException("The highest coefficient is expected to be non-zero."); } if (epsilon == 0.0) { epsilon = -1E-08; } if (coefficients.Count == 2) { roots.Add(-coefficients[0] / coefficients[1]); } else if (coefficients.Count == 3) { MathUtil.GetQuadraticPolynomialRoots(coefficients, roots); } else if (coefficients.Count == 4) { IList <double> cubicPolynomialRoots = MathUtil.GetCubicPolynomialRoots(coefficients); for (int index = 0; index < cubicPolynomialRoots.Count; ++index) { roots.Add(cubicPolynomialRoots[index]); } } else { int num1 = 0; int n = coefficients.Count - 1; double[] numArray1 = new double[coefficients.Count]; for (int index = n; index >= 0; --index) { numArray1[index] = coefficients[n - index]; } double[] numArray2 = new double[coefficients.Count - 1]; while (n > 3) { for (int index = 0; index < n; ++index) { numArray2[index] = numArray1[index] * (double)(n - index); } double num2 = Class64.smethod_3(n, (IList <double>)numArray1); ComplexD complexD1 = ComplexD.Zero; double num3 = numArray1[n] * numArray1[n]; double num4 = num3; ComplexD complexD2 = (ComplexD)numArray1[n - 1]; ComplexD complexD3 = (ComplexD)(numArray1[n - 1] == 0.0 ? 1.0 : -numArray1[n] / numArray1[n - 1]); complexD3 = (ComplexD)((double)System.Math.Sign(complexD3.Real) * num2); ComplexD dz = complexD3; ComplexD complexD4 = Class64.smethod_5(n, (IList <double>)numArray1, complexD3); double num5 = complexD4.GetModulusSquared(); double num6 = 2.5 * num2; double modulus = dz.GetModulus(); double num7 = 4.0 * (double)n * (double)n * num3 * Class64.double_0; bool flag1 = true; int num8; for (num8 = 0; (complexD3.Real + dz.Real != complexD3.Real || complexD3.Imaginary + dz.Imaginary != complexD3.Imaginary) && (num5 > num7 && num8 < 50); ++num8) { ComplexD complexD5 = Class64.smethod_5(n - 1, (IList <double>)numArray2, complexD3); double modulusSquared1 = complexD5.GetModulusSquared(); if (modulusSquared1 == 0.0) { dz = Class64.smethod_4(dz, 5.0); } else { dz = new ComplexD((complexD4.Real * complexD5.Real + complexD4.Imaginary * complexD5.Imaginary) / modulusSquared1, (complexD4.Imaginary * complexD5.Real - complexD4.Real * complexD5.Imaginary) / modulusSquared1); double num9 = (complexD2 - complexD5).GetModulusSquared() / (complexD1 - complexD3).GetModulusSquared(); flag1 = num5 != num4 || num9 * num5 > 0.25 * modulusSquared1 * modulusSquared1; modulus = dz.GetModulus(); if (modulus > num6) { dz = Class64.smethod_4(dz, num6 / modulus); } num6 = modulus * 5.0; } complexD1 = complexD3; double num10 = num5; complexD2 = complexD4; bool flag2 = true; while (flag2) { flag2 = false; complexD3 = complexD1 - dz; complexD4 = Class64.smethod_5(n, (IList <double>)numArray1, complexD3); num4 = num5 = complexD4.GetModulusSquared(); if (flag1) { ComplexD z = complexD3; int num9 = 1; bool flag3 = num5 > num10; for (; num9 <= n; ++num9) { if (flag3) { dz *= 0.5; z = complexD1 - dz; } else { z -= dz; } ComplexD complexD6 = Class64.smethod_5(n, (IList <double>)numArray1, z); double modulusSquared2 = complexD6.GetModulusSquared(); if (modulusSquared2 < num5) { num5 = modulusSquared2; complexD4 = complexD6; complexD3 = z; if (flag3 && num9 == 2) { dz = Class64.smethod_4(dz, 0.5); complexD3 = complexD1 - dz; complexD4 = Class64.smethod_5(n, (IList <double>)numArray1, complexD3); num5 = complexD4.GetModulusSquared(); break; } } else { break; } } } else { num7 = Class64.smethod_6(n, (IList <double>)numArray1, complexD3); } if (modulus < complexD3.GetModulus() * Class64.double_0 && num5 >= num10) { complexD3 = complexD1; dz = Class64.smethod_4(dz, 0.5); if (complexD3 + dz != complexD3) { flag2 = true; } } } } if (num8 >= 50) { --num1; } ComplexD real = (ComplexD)complexD3.Real; if ((complexD4 = Class64.smethod_5(n, (IList <double>)numArray1, real)).GetModulusSquared() <= num5) { roots.Add(complexD3.Real); n = Class64.smethod_7(n, (IList <double>)numArray1, complexD3.Real); } else { n = Class64.smethod_8(n, (IList <double>)numArray1, complexD3); } } if (n == 1) { roots.Add(-numArray1[1] / numArray1[0]); } else if (n == 2) { Class64.smethod_1((IList <double>)numArray1, roots); } else { if (n != 3) { return; } IList <double> doubleList = Class64.smethod_2((IList <double>)numArray1); for (int index = 0; index < doubleList.Count; ++index) { roots.Add(doubleList[index]); } } } }
private static IList <double> smethod_2(IList <double> coefficients) { double coefficient1 = coefficients[0]; if (coefficient1 == 0.0) { throw new ArgumentException("Coefficient a is zero."); } double coefficient2 = coefficients[1]; double coefficient3 = coefficients[2]; double coefficient4 = coefficients[3]; double num1 = coefficient2 * coefficient2; double num2 = coefficient1 * coefficient3; double num3 = coefficient1 * coefficient4; double a = num1 - 3.0 * num2; double num4 = coefficient2 * (2.0 * num1 - 9.0 * num2) + 27.0 * coefficient1 * num3; double d = num4 * num4 - 4.0 * a * a * a; double[] numArray; if (d < -8.88178419700125E-16) { double num5 = System.Math.Sqrt(-d); ComplexD complexD = new ComplexD(0.5 * num4, 0.5 * num5); ComplexD v = ComplexD.FromModulusAndArgument(System.Math.Pow(complexD.GetModulusSquared(), 1.0 / 6.0), 1.0 / 3.0 * complexD.GetArgument()); ComplexD conjugate = v.GetConjugate(); double num6 = -1.0 / (3.0 * coefficient1); numArray = new double[3] { num6 *(coefficient2 + v.Real + conjugate.Real), num6 *(coefficient2 + ComplexD.MultiplyAndGetReal(Class64.complexD_2, v) + ComplexD.MultiplyAndGetReal(Class64.complexD_3, conjugate)), num6 *(coefficient2 + ComplexD.MultiplyAndGetReal(Class64.complexD_4, v) + ComplexD.MultiplyAndGetReal(Class64.complexD_5, conjugate)) }; } else if (d > 8.88178419700125E-16) { double num5 = System.Math.Sqrt(d); double x1 = 0.5 * (num4 + num5); double x2 = 0.5 * (num4 - num5); double num6 = x1 >= 0.0 ? System.Math.Pow(x1, 1.0 / 3.0) : -System.Math.Pow(-x1, 1.0 / 3.0); double num7 = x2 >= 0.0 ? System.Math.Pow(x2, 1.0 / 3.0) : -System.Math.Pow(-x2, 1.0 / 3.0); numArray = new double[1] { -(coefficient2 + num6 + num7) / (3.0 * coefficient1) }; } else if (MathUtil.AreApproxEqual(a, 0.0, 8.88178419700125E-16)) { numArray = new double[1] { -coefficient2 / (3.0 * coefficient1) } } ; else { numArray = new double[2] { (9.0 * num3 - coefficient2 * coefficient3) / (2.0 * a), (4.0 * coefficient2 * num2 - 9.0 * coefficient1 * num3 - coefficient2 * num1) / (coefficient1 * a) } }; return((IList <double>)numArray); }
private static ComplexD smethod_4(ComplexD dz, double scaleFactor) { return(new ComplexD((dz.Real * 0.6 - dz.Imaginary * 0.8) * scaleFactor, (dz.Real * 0.8 + dz.Imaginary * 0.6) * scaleFactor)); }