1

I'm trying to do filtering .wav sample data value using the HAAR formula but got error "floating point overflow"

Edited : add more code

numsamples := round(wavehdr.SampleRate);
SetLength(wavedata[0].Data, numsamples);
Stream.Read(wavedata[0].Data[0], numsamples);
SetLength(cA1, numsamples);
SetLength(cD1, numsamples);
for i:=1 to numsamples-1 do begin 
cA1[i]:=(wavedata[0].Data[(i*2)-1]*0.7071) + (wavedata[0].Data[(i*2)]*0.7071);
cD1[i]:=(wavedata[0].Data[(i*2)-1]*0.7071) + (wavedata[0].Data[(i*2)]*-0.7071);
end;

where wavedata[0].Data[i], i get it from function Stream.Read to load sample data value of .wav file. I don't know why i got the error or what the error means and i've been searching the error mostly caused of divizion by zero, but there is no divizion by zero in my code. So maybe i could some help here what is the error mean in my code?

EDIT 1: (i'm really new to delphi, this code is not mine i found it internet. In my understanding the following code is the one to read .wav file sample data value)

type
  TWaveHeader = packed record

    Marker_RIFF: array [0..3] of char;
    ChunkSize: cardinal;


    Marker_WAVE: array [0..3] of char;


    Marker_fmt: array [0..3] of char;
    SubChunkSize: cardinal;


    FormatTag: word;

    { nChannels : 1  mono, 2  stereo }
    NumChannels: word;
    SampleRate: longint;
    BytesPerSecond: longint;
    BytesPerSample: word;
    BitsPerSample: word;


    Marker_data: array [0..3] of char;


    DataBytes: longint;
  end;

  TChannel = record

    Data : array of double;
  end;

And a private declaration :

private
    wavehdr:TWaveHeader;

the function :

FillChar(wavehdr, sizeof(wavehdr),0);
Stream.Read(wavehdr,sizeof(wavehdr));

i modified a bit of the code to handle null value while reading the sample data :

 if(IsNan(wavedata[0].Data[(i*2)-1])) then begin
      wavedata[0].Data[(i*2)-1]:=0;
    end
    else if(IsNan(wavedata[0].Data[(i*2)]))  then begin
      wavedata[0].Data[(i*2)]:=0;
    end;
4

3 回答 3

6

i:=0...

(wavedata[0].Data[(i*2)-1]

你真的有数组元素Data[-1]吗?

PS 在调试时设置范围检查编译器选项。

编辑:我看到了一些新代码,所以让我们开始第 2 步:

SetLength(wavedata[0].Data, **numsamples**);

for i:=1 to **numsamples**-1

wavedata[0].Data[(**i*2)**]

我们必须彻底检查每一行代码吗?

于 2013-04-24T09:45:13.333 回答
4

当表达式产生的值不适合计算表达式的数据类型范围时,就会发生溢出。正数和负数都可能发生溢出。

只有当输入值已经接近溢出浮点值时,您的特定表达式才会导致溢出。因此,例如,如果您使用双精度值,那么只有当输入数据的大小约为 1e308 时,您的代码才会溢出。

您的输入数据似乎不太可能真的是那种形式。所以我的猜测是你的问题与你如何读取输入数据有关。我怀疑你读错了,所以最终对无意义的值执行算术。

于 2013-04-24T06:34:20.397 回答
0

在我尝试了这个并且我发现了自己的错误之后,感谢@MBo 你的回答缩小了循环中的 mw 焦点,这对我来说真的很愚蠢。循环应该像

for i:=0 to round(numsamples/2) do begin

问题不是元素数据[-1],但是,如果数组wavedata的长度= X,那么我尝试到达不可用的[X * 2]元素,它肯定会在一半之后导致错误。例如 array[4] 但我尝试到达不可用的 array[4*2] (抱歉我的英语不好,我不知道我的解释是否好)但感谢大家的帮助:D

于 2013-04-24T20:56:46.833 回答