0

我正在处理大文件并使用一些算法从 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;
            }
4

1 回答 1

0

也许你可以试试下面的代码

while (wave.Position != length)
            {                
                UInt16 read = wave.Read(mainBuffer, 0, mainBuffer.Length);
                double[] segment = new double[read / 8];
                for (UInt16 i = 0; i < segment.Length; i++)
                {
                    segment[i] =BitConverter.ToSingle(mainBuffer, i * 8);
                }               
                ent.Add(obj.entropy(segment));                
            }
于 2013-10-09T09:42:33.087 回答