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.
我尝试了一些断点然后观察了代码。它在循环的开始工作,但然后给我这个例外。我该如何解决这个问题?
答案 0 :(得分: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。它起因于“由于先前的功能评估超时而导致功能评估被禁用”。我找到了它的资源。如果您尝试在MWNumericArray
循环中填写for
,则无法完成此过程,因此评估超时。我还改变了我的代码来处理更大的文件。它仍然有点慢,但它现在工作正常。