我正在处理大文件并使用一些算法从 MatLab 转换为 dll。其中之一是 entropy.m。我将它转换为 dll 并在 c# 中使用它。如您所知,它需要 MWArray 来执行计算。我将数据存储为段。将一段填充到MWNumericArray
. 计算段的熵然后清除MWNumericArray
。然而这个过程太慢了。我可以做些什么来提高程序的性能?这是我的代码:
NAudio.Wave.WaveChannel32 wave = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));
byte[] mainBuffer = new byte[wave.WaveFormat.AverageBytesPerSecond / 5];
int read;
int length = (int)wave.Length;
List<MWArray> ent = new List<MWArray>();
MWNumericArray arr1 = null;
EntropyCalculation obj = new EntropyCalculation();
if (file.Length % mainBuffer.Length != 0)
{
int value = (int)(wave.Length / mainBuffer.Length);
length = mainBuffer.Length * value;
}
while (wave.Position != length)
{
List<double> segment = new List<double>();
read = wave.Read(mainBuffer, 0, mainBuffer.Length);
for (int i = 0; i < read / 8; i++)
{
segment.Add((BitConverter.ToSingle(mainBuffer, i * 8)));
}
arr1 = segment.ToArray();
MWArray result = obj.entropy(arr1);
ent.Add(result);
arr1 = null;
}