public void MultiCosineDistance() { var distribution = new Normal(0, 5); var a = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable(); var b = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable(); var c = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable(); var distance = a.FindDistances(new[] { b, c }, DistanceMetric.Cosine).AsIndexable(); IIndexableVector distance2; using (var gpuA = _cuda.CreateVector(a)) using (var gpuB = _cuda.CreateVector(b)) using (var gpuC = _cuda.CreateVector(c)) using (var temp = gpuA.FindDistances(new[] { gpuB, gpuC }, DistanceMetric.Cosine)) distance2 = temp.AsIndexable(); FloatingPointHelper.AssertEqual(distance, distance2, 10); }
public void MatrixSplitRows() { const int POSITION = 2000; var rand = new Random(); var a = _cpu.CreateMatrix(6000, 3000, (x, y) => Convert.ToSingle(rand.NextDouble())).AsIndexable(); var r = a.SplitAtColumn(POSITION); IIndexableMatrix gpuResults1, gpuResults2; using (var gpuA = _cuda.CreateMatrix(a)) { var r2 = gpuA.SplitAtColumn(POSITION); using (var m1 = r2.Left) using (var m2 = r2.Right) { gpuResults1 = m1.AsIndexable(); gpuResults2 = m2.AsIndexable(); } } FloatingPointHelper.AssertEqual(gpuResults1, r.Left.AsIndexable()); FloatingPointHelper.AssertEqual(gpuResults2, r.Right.AsIndexable()); }
public void VectorCopyFrom() { Load(); var a = _cpu.CreateVector(10, i => i * 2).AsIndexable(); var b = _cpu.CreateVector(10, 0).AsIndexable(); b.CopyFrom(a); FloatingPointHelper.AssertEqual(a, b); IIndexableVector gpuResults; using (var gpuA = _cuda.CreateVector(a)) { using var gpuB = _cuda.CreateVector(10, 0); gpuB.CopyFrom(gpuA); gpuResults = gpuB.AsIndexable(); } FloatingPointHelper.AssertEqual(a, gpuResults); Cleanup(); }
public void MatrixConcatRows() { Load(); var rand = new Random(); var a = _cpu.CreateMatrix(300, 4000, (x, y) => Convert.ToSingle(rand.NextDouble())). AsIndexable(); var b = _cpu.CreateMatrix(300, 200, (x, y) => Convert.ToSingle(rand.NextDouble())). AsIndexable(); var c = a.ConcatRows(b).AsIndexable(); IIndexableMatrix gpuResults; using (var gpuA = _cuda.CreateMatrix(a)) { using var gpuB = _cuda.CreateMatrix(b); using var concat = gpuA.ConcatRows(gpuB); gpuResults = concat.AsIndexable(); } FloatingPointHelper.AssertEqual(c, gpuResults); Cleanup(); }
public void TestMatrixCreationFromColumns() { Load(); var values = new[] { Enumerable.Range(0, 10).Select(v => (float)v).ToArray(), Enumerable.Range(0, 10).Select(v => (float)v * 2).ToArray(), Enumerable.Range(0, 10).Select(v => (float)v * 3).ToArray() }; var cpuRowList = values.Select(v => _cpu.CreateVector(v)).ToList(); var cpuMatrix = _cpu.CreateMatrixFromColumns(cpuRowList); var gpuRowList = values.Select(v => _cuda.CreateVector(v)).ToList(); using (var gpuMatrix = _cuda.CreateMatrixFromColumns(gpuRowList)) { FloatingPointHelper.AssertEqual(cpuMatrix.AsIndexable(), gpuMatrix.AsIndexable()); } gpuRowList.ForEach(v => v.Dispose()); Cleanup(); }
public void MultiManhattanDistance() { Load(); var distribution = new Normal(0, 5); var a = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable(); var b = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable(); var c = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable(); var distance = a.FindDistances(new[] { b, c }, DistanceMetric.Manhattan).AsIndexable(); IIndexableVector distance2; using (var gpuA = _cuda.CreateVector(a)) { using var gpuB = _cuda.CreateVector(b); using var gpuC = _cuda.CreateVector(c); using var temp = gpuA.FindDistances(new[] { gpuB, gpuC }, DistanceMetric.Manhattan); distance2 = temp.AsIndexable(); } FloatingPointHelper.AssertEqual(distance, distance2, 18); Cleanup(); }
public void VectorSplit() { const int BLOCK_COUNT = 3; var a = _cpu.CreateVector(12, i => i).AsIndexable(); var cpuResult = a.Split(BLOCK_COUNT).Select(v => v.AsIndexable()).ToList(); var gpuResult = new List <IIndexableVector>(); using (var gpuA = _cuda.CreateVector(a)) { var split = gpuA.Split(BLOCK_COUNT); foreach (var item in split) { gpuResult.Add(item.AsIndexable()); item.Dispose(); } } for (var i = 0; i < cpuResult.Count; i++) { Assert.IsTrue(cpuResult[i].Count == 4); FloatingPointHelper.AssertEqual(cpuResult[i], gpuResult[i]); } }
public void _MatrixMultiplication(int rowsA, int columnsArowsB, int columnsB) { var rand = new Random(1); var a = _cpu. CreateMatrix(rowsA, columnsArowsB, (j, k) => Convert.ToSingle(rand.NextDouble())). AsIndexable(); var b = _cpu. CreateMatrix(columnsArowsB, columnsB, (j, k) => Convert.ToSingle(rand.NextDouble())). AsIndexable(); var cpuResults = a.Multiply(b); IIndexableMatrix gpuResults; using (var gpuA = _cuda.CreateMatrix(a)) { using var gpuB = _cuda.CreateMatrix(b); using var gpuC = gpuA.Multiply(gpuB); gpuResults = gpuC.AsIndexable(); } FloatingPointHelper.AssertEqual(gpuResults, cpuResults.AsIndexable()); _cuda.Dispose(); }
public void MatrixSplitColumns() { Load(); const int POSITION = 2000; var rand = new Random(); var a = _cpu.CreateMatrix(6000, 3000, (x, y) => Convert.ToSingle(rand.NextDouble())). AsIndexable(); var r = a.SplitAtRow(POSITION); IIndexableMatrix gpuResults1, gpuResults2; using (var gpuA = _cuda.CreateMatrix(a)) { var r2 = gpuA.SplitAtRow(POSITION); using var m1 = r2.Top; using var m2 = r2.Bottom; gpuResults1 = m1.AsIndexable(); gpuResults2 = m2.AsIndexable(); } FloatingPointHelper.AssertEqual(gpuResults1, r.Top.AsIndexable()); FloatingPointHelper.AssertEqual(gpuResults2, r.Bottom.AsIndexable()); Cleanup(); }
public void MatrixReadWrite() { var a = _cpu.CreateMatrix(7, 20, (x, y) => x * 10 + y).AsIndexable(); // test Numerics -> Numerics serialisation var serialised = a.Data; var b = _cpu.CreateMatrix(serialised); FloatingPointHelper.AssertEqual(a.AsIndexable(), b.AsIndexable()); // test Numerics -> Cuda serialisation using (var c = _cuda.CreateMatrix(serialised)) { FloatingPointHelper.AssertEqual(a.AsIndexable(), c.AsIndexable()); // test Cuda -> Cuda serialisation var serialised2 = c.Data; using (var d = _cuda.CreateMatrix(serialised2)) FloatingPointHelper.AssertEqual(a.AsIndexable(), d.AsIndexable()); // test Cuda -> Numerics serialisation var e = _cpu.CreateMatrix(c.Data); FloatingPointHelper.AssertEqual(a.AsIndexable(), e.AsIndexable()); } }
public void MultiCosineDistance2() { Load(); var distribution = new Normal(0, 5); float[] dataNorm1 = null; float[] dataNorm2 = null; var a = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable(); var b = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable(); var c = _cpu.CreateVector(5000, i => Convert.ToSingle(distribution.Sample())).AsIndexable(); var distance = a.CosineDistance(new[] { b, c }, ref dataNorm1).AsIndexable(); IIndexableVector distance2; using (var gpuA = _cuda.CreateVector(a)) { using var gpuB = _cuda.CreateVector(b); using var gpuC = _cuda.CreateVector(c); using var temp = gpuA.CosineDistance(new[] { gpuB, gpuC }, ref dataNorm2); distance2 = temp.AsIndexable(); } FloatingPointHelper.AssertEqual(distance, distance2, 10); Cleanup(); }
public void VectorReadWrite() { var a = _cpu.CreateVector(5, i => i).AsIndexable(); // test Numerics -> Numerics serialisation var serialised = a.Data; var b = _cpu.CreateVector(serialised); FloatingPointHelper.AssertEqual(a.AsIndexable(), b.AsIndexable()); // test Numerics -> Cuda serialisation using (var c = _cuda.CreateVector(serialised)) { FloatingPointHelper.AssertEqual(a.AsIndexable(), c.AsIndexable()); // test Cuda -> Cuda serialisation var serialised2 = c.Data; using (var d = _cuda.CreateVector(serialised2)) FloatingPointHelper.AssertEqual(a.AsIndexable(), d.AsIndexable()); // test Cuda -> Numerics serialisation var e = _cpu.CreateVector(c.Data); FloatingPointHelper.AssertEqual(a.AsIndexable(), e.AsIndexable()); } }
public void UpdateParameter(ref ReverbParameter parameter) { uint sampleRate = (uint)FixedPointHelper.ToFloat((uint)parameter.SampleRate, FixedPointPrecision); float preDelayTimeInMilliseconds = FixedPointHelper.ToFloat(parameter.PreDelayTime, FixedPointPrecision); float earlyGain = FixedPointHelper.ToFloat(parameter.EarlyGain, FixedPointPrecision); float coloration = FixedPointHelper.ToFloat(parameter.Coloration, FixedPointPrecision); float decayTime = FixedPointHelper.ToFloat(parameter.DecayTime, FixedPointPrecision); for (int i = 0; i < 10; i++) { EarlyDelayTime[i] = Math.Min(IDelayLine.GetSampleCount(sampleRate, EarlyDelayTimes[i] + preDelayTimeInMilliseconds), PreDelayLine.SampleCountMax) + 1; EarlyGain[i] = EarlyGainBase[i] * earlyGain; } if (parameter.ChannelCount == 2) { EarlyGain[4] = EarlyGain[4] * 0.5f; EarlyGain[5] = EarlyGain[5] * 0.5f; } PreDelayLineDelayTime = Math.Min(IDelayLine.GetSampleCount(sampleRate, PreDelayTimes[(int)parameter.EarlyMode] + preDelayTimeInMilliseconds), PreDelayLine.SampleCountMax); ReadOnlySpan <float> fdnDelayTimes = GetFdnDelayTimesByLateMode(parameter.LateMode); ReadOnlySpan <float> decayDelayTimes = GetDecayDelayTimesByLateMode(parameter.LateMode); float highFrequencyDecayRatio = FixedPointHelper.ToFloat(parameter.HighFrequencyDecayRatio, FixedPointPrecision); float highFrequencyUnknownValue = FloatingPointHelper.Cos(1280.0f / sampleRate); for (int i = 0; i < 4; i++) { FdnDelayLines[i].SetDelay(fdnDelayTimes[i]); DecayDelays[i].SetDelay(decayDelayTimes[i]); float tempA = -3 * (DecayDelays[i].CurrentSampleCount + FdnDelayLines[i].CurrentSampleCount); float tempB = tempA / (decayTime * sampleRate); float tempC; float tempD; if (highFrequencyDecayRatio < 0.995f) { float tempE = FloatingPointHelper.Pow10((((1.0f / highFrequencyDecayRatio) - 1.0f) * 2) / 100 * (tempB / 10)); float tempF = 1.0f - tempE; float tempG = 2.0f - (tempE * 2 * highFrequencyUnknownValue); float tempH = MathF.Sqrt((tempG * tempG) - (tempF * tempF * 4)); tempC = (tempG - tempH) / (tempF * 2); tempD = 1.0f - tempC; } else { // no high frequency decay ratio tempC = 0.0f; tempD = 1.0f; } HighFrequencyDecayDirectGain[i] = FloatingPointHelper.Pow10(tempB / 1000) * tempD * 0.7071f; HighFrequencyDecayPreviousGain[i] = tempC; PreviousFeedbackOutput[i] = 0.0f; DecayDelays[i].SetDecayRate(0.6f * (1.0f - coloration)); } }
public void UpdateParameter(ref Reverb3dParameter parameter) { uint sampleRate = parameter.SampleRate / 1000; EarlyDelayTime = new uint[20]; DryGain = parameter.DryGain; PreviousFeedbackOutputDecayed.AsSpan().Fill(0); PreviousPreDelayValue = 0; EarlyReflectionsGain = FloatingPointHelper.Pow10(Math.Min(parameter.RoomGain + parameter.ReflectionsGain, 5000.0f) / 2000.0f); LateReverbGain = FloatingPointHelper.Pow10(Math.Min(parameter.RoomGain + parameter.ReverbGain, 5000.0f) / 2000.0f); float highFrequencyRoomGain = FloatingPointHelper.Pow10(parameter.RoomHf / 2000.0f); if (highFrequencyRoomGain < 1.0f) { float tempA = 1.0f - highFrequencyRoomGain; float tempB = 2.0f - ((2.0f * highFrequencyRoomGain) * FloatingPointHelper.Cos(256.0f * parameter.HfReference / parameter.SampleRate)); float tempC = MathF.Sqrt(MathF.Pow(tempB, 2) - (4.0f * (1.0f - highFrequencyRoomGain) * (1.0f - highFrequencyRoomGain))); PreviousPreDelayGain = (tempB - tempC) / (2.0f * tempA); TargetPreDelayGain = 1.0f - PreviousPreDelayGain; } else { PreviousPreDelayGain = 0.0f; TargetPreDelayGain = 1.0f; } ReflectionDelayTime = IDelayLine.GetSampleCount(sampleRate, 1000.0f * (parameter.ReflectionDelay + parameter.ReverbDelayTime)); EchoLateReverbDecay = 0.6f * parameter.Diffusion * 0.01f; for (int i = 0; i < FdnDelayLines.Length; i++) { FdnDelayLines[i].SetDelay(FdnDelayMinTimes[i] + (parameter.Density / 100 * (FdnDelayMaxTimes[i] - FdnDelayMinTimes[i]))); uint tempSampleCount = FdnDelayLines[i].CurrentSampleCount + DecayDelays1[i].CurrentSampleCount + DecayDelays2[i].CurrentSampleCount; float tempA = (-60.0f * tempSampleCount) / (parameter.DecayTime * parameter.SampleRate); float tempB = tempA / parameter.HfDecayRatio; float tempC = FloatingPointHelper.Cos(128.0f * 0.5f * parameter.HfReference / parameter.SampleRate) / FloatingPointHelper.Sin(128.0f * 0.5f * parameter.HfReference / parameter.SampleRate); float tempD = FloatingPointHelper.Pow10((tempB - tempA) / 40.0f); float tempE = FloatingPointHelper.Pow10((tempB + tempA) / 40.0f) * 0.7071f; DecayDirectFdnGain[i] = tempE * ((tempD * tempC) + 1.0f) / (tempC + tempD); DecayCurrentFdnGain[i] = tempE * (1.0f - (tempD * tempC)) / (tempC + tempD); DecayCurrentOutputGain[i] = (tempC - tempD) / (tempC + tempD); DecayDelays1[i].SetDecayRate(EchoLateReverbDecay); DecayDelays2[i].SetDecayRate(EchoLateReverbDecay * -0.9f); } for (int i = 0; i < EarlyDelayTime.Length; i++) { uint sampleCount = Math.Min(IDelayLine.GetSampleCount(sampleRate, (parameter.ReflectionDelay * 1000.0f) + (EarlyDelayTimes[i] * 1000.0f * ((parameter.ReverbDelayTime * 0.9998f) + 0.02f))), PreDelayLine.SampleCountMax); EarlyDelayTime[i] = sampleCount; } }
private static float DownMixSurroundToStereo(ReadOnlySpan <float> coefficients, float back, float lfe, float center, float front) { return(FloatingPointHelper.RoundUp(coefficients[3] * back + coefficients[2] * lfe + coefficients[1] * center + coefficients[0] * front)); }