gpt4 book ai didi

c# - 获取最大值的字节数组

转载 作者:行者123 更新时间:2023-12-03 00:28:46 28 4
gpt4 key购买 nike

我有一个包含数百万字节的数组。这些字节是 int 值(Int16、Int24 或 Int32)。现在我想从一定数量的字节中获取具有最大 int 值的 x 字节。

所以为了更好地解释这一点,让我们想象一个有 10 个条目的数组:

byte[] arr = {255, 10, 55, 60, 128, 90, 88, 66, 199, 56};

我会知道我们使用的是 In16、Int24 还是 Int32,所以对于这个例子,让我们假设我们使用的是 Int16。这意味着,我们使用 2 个字节来表示一个 Int16。所以整数包括:
{255, 10},
{55, 60},
{128, 90},
{88, 66},
{199, 56}

问题1:因为这是音频处理所需要的,所以1046低于-2096。所以有必要独立于消极性进行比较

问题 2:因为这需要非常高效,将字节转换为 Ints 进行比较似乎效率低下,应该有其他方法。

这是起点:
    /// <summary>
/// Gets the maximum value of a number of bytes representing Int-Values
/// </summary>
/// <returns>The channels.</returns>
/// <param name="leftChannel">Left channel.</param>
/// <param name="rightChannel">Right channel.</param>
/// <param name="bytesPerInt">Bytes per int. 2 bytes = Int16, 3 bytes = Int24, 4 bytes = Int32</param>
/// <param name="countBytesToCombine">The number of bytes to look for the highest value</param>
private (byte[] combinedLeft, byte[] combinedRight) CombineChannels(byte[] leftChannel, byte[] rightChannel, int bytesPerInt, int countBytesToCombine)
{

}

/// <summary>
/// Gets the highest byte[] value
/// </summary>
/// <returns>The highest value. The size of the byte array is equal the bytesPerInt</returns>
/// <param name="bytes">A subarray of the given byte array of the upper method. The size of this array is equals countBytesToCombine</param>
/// <param name="bytesPerInt">The count of bytes representing an Int</param>
private byte[] GetHighestValue(byte[] bytes, int bytesPerInt)
{

}

编辑2

这是一个可行的解决方案,但每个 channel 执行 1400 万字节需要大约 2 秒,这太远了。
    /// <summary>
/// Gets the maximum value of a number of bytes representing Int-Values
/// </summary>
/// <returns>The channels.</returns>
/// <param name="leftChannel">Left channel.</param>
/// <param name="rightChannel">Right channel.</param>
/// <param name="bytesPerInt">Bytes per int. 2 bytes = Int16, 3 bytes = Int24, 4 bytes = Int32</param>
/// <param name="countValuesToCombine">The number of bytes to look for the highest value</param>
private (byte[] combinedLeft, byte[] combinedRight) CombineChannels(byte[] leftChannel, byte[] rightChannel, int bytesPerInt, int countValuesToCombine)
{
var cLeft = new List<byte>();
var cRight = new List<byte>();

for (int i = 0; i < leftChannel.Length; i += countValuesToCombine * bytesPerInt)
{
var arrLeft = SubArray(leftChannel, i, countValuesToCombine * bytesPerInt);
var arrRight = SubArray(rightChannel, i, countValuesToCombine * bytesPerInt);

cLeft.AddRange(GetHighestValue(arrLeft, bytesPerInt));
cRight.AddRange(GetHighestValue(arrRight, bytesPerInt));
}

return (cLeft.ToArray(), cRight.ToArray());
}

/// <summary>
/// Gets the highest byte[] value
/// </summary>
/// <returns>The highest value.</returns>
/// <param name="bytes">Bytes.</param>
/// <param name="bytesPerInt">The count of bytes representing an Int</param>
private byte[] GetHighestValue(byte[] bytes, int bytesPerInt)
{
byte[] bytesOfHighestValue = new byte[bytesPerInt];

for (int i = 0; i < bytes.Length; i += bytesPerInt)
{
var arr = SubArray(bytes, i, bytesPerInt);

if (IsValueHigher(arr, bytesOfHighestValue, bytesPerInt))
{
bytesOfHighestValue = arr;
}
}

return bytesOfHighestValue;
}

private bool IsValueHigher(byte[] one, byte[] two, int bytesPerInt)
{
var o = ConvertToInt(one, bytesPerInt);
var t = ConvertToInt(two, bytesPerInt);

return Math.Abs(o) > Math.Abs(t);
}

private int ConvertToInt(byte[] bytes, int bytesPerInt)
{
switch (bytesPerInt)
{
case 2:
return BitConverter.ToInt16(bytes, 0);
case 3:
return Int24.ToInt32(bytes, 0);
case 4:
return BitConverter.ToInt32(bytes, 0);
}

return 0;
}

这很难解释,所以请在投票前询问是否有问题。

最佳答案

好的,这是 4 字节整数的简单实现:

private static int GetHighestValue(byte[] data)
{
if (data.Length % 4 != 0)
throw new ArgumentException();

var maximum = 0, maximumAbs = 0;
for (var i = 0; i < data.Length; i+=4)
{
var current = BitConverter.ToInt32 (data, i);
var currentAbs = Math.Abs(current);

if (currentAbs > maximumAbs)
{
maximum = current;
maximumAbs = currentAbs;
}
}

return maximum;
}

byte[] 上运行此程序使用 Debug 编译时,使用 100 万字节大约需要 3ms。

我不知道您的目标是哪种速度,但对于 99% 的情况,这应该没问题。

编辑:由于您更新了问题并包含示例代码,因此这里是更新:

这些是我让你的代码比它需要的慢的一些地方:
  • 我们不需要在 CombineChannels 的每次迭代中创建子数组.我们可以重写GetHighestValue所以它需要array , offsetamount作为参数。
  • 而不是拥有一个 CombineChannels方法我们应该把它分成不同的字节大小。例如 CombineChannelsInt32 , CombineChannelsInt16 ... 这样,方法本身可以将最大值存储为 int32/int16/... 而不必在每次迭代时转换它们。

  • 所以这里是我们最终会得到这样的方法:
    (byte[] combinedLeft, byte[] combinedRight) CombineChannels(byte[] leftChannel, byte[] rightChannel, int bytesPerInt, int countValuesToCombine)
    {
    switch(bytesPerInt)
    {
    case 2:
    return CombineChannelsInt16(leftChannel, rightChannel, countValuesToCombine);
    case 3:
    return CombineChannelsInt24(leftChannel, rightChannel, countValuesToCombine);
    case 4:
    return CombineChannelsInt32(leftChannel, rightChannel, countValuesToCombine);
    }
    }

    (byte[] combinedLeft, byte[] combinedRight) CombineChannelsInt16(byte[] leftChannel, byte[] rightChannel, int countValuesToCombine);
    (byte[] combinedLeft, byte[] combinedRight) CombineChannelsInt24(byte[] leftChannel, byte[] rightChannel, int countValuesToCombine);
    (byte[] combinedLeft, byte[] combinedRight) CombineChannelsInt32(byte[] leftChannel, byte[] rightChannel, int countValuesToCombine);

    short GetHighestValueInt16(byte[] bytes, int offset, int amount);
    Int24 GetHighestValueInt24(byte[] bytes, int offset, int amount);
    int GetHighestValueInt32(byte[] bytes, int offset, int amount);

    关于c# - 获取最大值的字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52689895/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com