I have float data coming in as 33m/s. I stored them on Memory Stream so that I could calculate the average of stored data. However, I managed to stack float data to the memory stream by using this.
byte[] FloatToByte = System.BitConverter.GetBytes(SomeData);
memoryStream.Write(FloatToByte, 0, FloatToByte.Length);
Now my problem is that I want to get the float data that are stacked for 1second which would give me 33 float data stacked in the memory stream. I want to caculate the average of 33 float data. So how could I get the 33 float data from the memory stream? Can anyone help? Thank you
UPDATE I managed to solve the problem. Thank you for the answer that has been just wrote. I handled the problem by using queue. I wondered why I was trying to use memory stream. the first one who answered caught me on the mind thank you. For those who might be struggling like me, I am posting some code below.
QueueForMicLeft.Enqueue(MicLeft_data);
if(QueueForMicLeft.Count> 30)
{
AverageOfMicLeftData(QueueForMicLeft);
Console.WriteLine("Average_data = " + AverageOfMicLeftData(QueueForMicLeft));
QueueForMicLeft.Dequeue();
}
public static float AverageOfMicLeftData(IEnumerable<float> Collection)
{
float sum = 0;
foreach (float obj in Collection)
sum += obj;
return (sum /30);
}