C#: MWArray SEHException
本文关键字:SEHException MWArray | 更新日期: 2023-09-27 18:33:38
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 MB 的文件的熵时arr1 = cba.ToArray();
此行会引发异常。 SEHException was unhandled
- External component has thrown an exception.
我试图放置一些断点然后观察代码。它在循环开始时工作,但随后给了我这个例外。我该如何解决这个问题?
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。它源于"函数评估被禁用,因为以前的函数评估超时"。我找到了它的资源。如果您尝试在循环中填充MWNumericArray
for
则它无法完成该过程,因此评估超时。我还更改了代码以处理更大的文件。它仍然有点慢,但现在工作正常。