0
public static void EnergyandEntropy(List<float> data, string _filename, OpenFileDialog open)
        {
    NAudio.Wave.WaveChannel32 wave = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));
            byte[] mainBuffer = new byte[wave.Length];
            int sampleRate = wave.WaveFormat.SampleRate;

            int window_ms = 200;
            int window;
            int fs = sampleRate;
            window = window_ms * fs / 1000;

            float[] fbuffer = new float[mainBuffer.Length / 8];
            wave.Read(mainBuffer, 0, mainBuffer.Length);

            for (int i = 0; i < fbuffer.Length; i++)
            {
                fbuffer[i] = (BitConverter.ToSingle(mainBuffer, i * 8));
            }

            data.AddRange(fbuffer);
            int dataRange = data.Count - (data.Count % window);
            data.RemoveRange(dataRange, data.Count - dataRange);
            var rows = window;
            var cols = data.Count / window;
            float[,] array2D = new float[window, data.Count / window];
            var current = 0;   
            List<double> abc = new List<double>();
            List<double> cba = new List<double>();
            List<double> vuv = new List<double>();
            List<MWArray> ent = new List<MWArray>();
            MWNumericArray arr1;
            EntropyCalculation obj = new EntropyCalculation();
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    array2D[i, j] = data[current++];
                    double a = array2D[i, j] * array2D[i, j];
                    abc.Add(a);
                    cba.Add(array2D[i, j]);
                    arr1 = cba.ToArray();
                    if (abc.Count % rows == 0 && abc.Count > 0)
                    {
                          double energies = Math.Sqrt(abc.Sum());
                          MWArray result = obj.entropy(arr1);
                          abc.Clear();
                          cba.Clear();
                          arr1 = null;
                          vuv.Add(energies);
                          ent.Add(result);
                    }
                }
            }
    }

我有这段代码并使用了一个名为 entropy.m 的 MatLab 函数(我将其转换为 dll)。我正在计算数据段的能量和熵。为此,我在里面填充数据List。当我的数据达到行数时,我清除列表和 MWArray。然后它继续处理下一个段。

这是我的问题。在计算小文件的熵时,它工作正常。但是,当我尝试计算大于 10 兆字节的文件的熵时,arr1 = cba.ToArray();此行会引发异常。SEHException was unhandled-External component has thrown an exception.我试图放置一些断点然后观察代码。它在循环开始时起作用,但随后给了我那个例外。我怎么解决这个问题?

4

1 回答 1

0
        NAudio.Wave.WaveChannel32 wave = new NAudio.Wave.WaveChannel32(new NAudio.Wave.WaveFileReader(open.FileName));
        WaveFileReader wavFile = new WaveFileReader(open.FileName);

        int sampleRate = wave.WaveFormat.SampleRate;
        byte[] mainBuffer = new byte[wave.WaveFormat.AverageBytesPerSecond / 5];

        #region Constraints
        int window_ms = 200;
        int window;
        int fs = sampleRate;
        window = window_ms * fs / 1000;
        #endregion

        int read;
        int length = (int)wave.Length;
        List<double> listOfEnergies = new List<double>();
        List<MWArray> ent = new List<MWArray>();
        MWNumericArray arr1 = null;
        EntropyCalculation obj = new EntropyCalculation();
        if (wave.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>();
            List<double> listOfSquaredSegment = 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)));
                double segmentSquare = segment[i] * segment[i];
                listOfSquaredSegment.Add(segmentSquare);
                //arr1 = segment.ToArray();
            }
            arr1 = segment.ToArray();
            double energy = Math.Sqrt(listOfSquaredSegment.Sum());
            listOfEnergies.Add(energy);
            MWArray result = obj.entropy(arr1);
            ent.Add(result);
            arr1 = null;
        }

我得到了 SEHException。它源于“功能评估被禁用,因为先前的功能评估超时”。我找到了它的资源。如果您尝试填写循环MWNumericArrayfor则无法完成该过程,因此评估超时。我还更改了代码以处理更大的文件。它仍然有点慢,但现在可以正常工作。

于 2013-10-08T12:37:11.253 回答