SSPC-Tester/Driver/PCIE8586/Pcie8586Probe/Acquisition/CodeConverter.cs

48 lines
1.5 KiB
C#
Raw Permalink Normal View History

using Pcie8586Probe.Models;
namespace Pcie8586Probe.Acquisition;
public static class CodeConverter
{
public static double CodeToVolts(ushort code, InputRange range)
{
return range switch
{
InputRange.PlusMinus5V => ((10_000.0 / 65_536.0) * code - 5_000.0) / 1_000.0,
InputRange.PlusMinus1V => ((2_000.0 / 65_536.0) * code - 1_000.0) / 1_000.0,
_ => throw new ArgumentOutOfRangeException(nameof(range), range, null)
};
}
public static double[][] Deinterleave(ReadOnlySpan<ushort> raw, int channelCount, InputRange range)
{
if (channelCount is not (1 or 2 or 4 or 8))
{
throw new ArgumentOutOfRangeException(nameof(channelCount), "Channel count must be 1, 2, 4, or 8.");
}
if (raw.Length % channelCount != 0)
{
throw new ArgumentException("Raw sample length must be divisible by channel count.", nameof(raw));
}
var samplesPerChannel = raw.Length / channelCount;
var result = new double[channelCount][];
for (var channel = 0; channel < channelCount; channel++)
{
result[channel] = new double[samplesPerChannel];
}
for (var sample = 0; sample < samplesPerChannel; sample++)
{
var baseIndex = sample * channelCount;
for (var channel = 0; channel < channelCount; channel++)
{
result[channel][sample] = CodeToVolts(raw[baseIndex + channel], range);
}
}
return result;
}
}