I've got an array containing millions of bytes. These bytes are int values (Int16, Int24 or Int32). Now I want to get the x-bytes with the max int value out of an amount of bytes.
So to explain this better, lets imagine an array with 10 entries:
byte[] arr = {255, 10, 55, 60, 128, 90, 88, 66, 199, 56};
I will know if we use In16, Int24 or Int32, so for this example, lets imagine we are using Int16. This means, we use 2 bytes to represent an Int16. So the Ints consist of:
{255, 10},
{55, 60},
{128, 90},
{88, 66},
{199, 56}
Problem1: Because this is needed for audio processing, 1046 is lower than -2096. So there is a need to compare independent of negativity
Problem2: Because this needs to be very performant, converting the bytes into Ints for comparing seems inefficient and there should be an other way.
This is the starting point:
/// <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)
{
}
Edit2
This is a working solution but it takes about 2 seconds to execute with 14 million bytes for each channel which is way too far.
/// <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;
}
This is extremely difficult to explain so please ask if there are questions before downvoting.