1
votes

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;
3
how are cA1, cD1 and wavedata[0].Data declared? - David A
cA1 cD1 and wavedata were all array of double. which wavedata array may contain value like : 0, 1.98E-04, etc (it's the value of sample .wav data) - Wawan Ma-chun
result is to big to be kept in your float try to understand uses Math; procedure TForm6.Button1Click(Sender: TObject); var f:Double; begin f := MaxDouble; f := F * 2; Showmessage(FloatToStr(F)) end; - bummi
sorry i'm new to delphi, if that value like 1.98E-04 and similar too big then what should i do? doi have to change the datatype? then what datatype should i use? - Wawan Ma-chun
While this is a complete aside, consider defining const RSQRT_2 = 0.70710678118; and instead writing ...Data[(i*2)-1]*RSQRT_2. It's neater, less error prone, easier to maintain, etc. - J...

3 Answers

6
votes

for i:=0 ...

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

Do you really have array element Data[-1] ?

P.S. Set range check compiler option while debugging.

Edit: I see some new code, so let's go with step 2:

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

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

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

Have we to check thoroughly every line of code?

4
votes

Overflow occurs when an expression yields a value that does not fit in the range of the data type in which the expression is evaluated. Overflow can occur for positive and negative numbers.

Your particular expression will only result in overflow if the input values are already close to overflowing a floating point value. So, if you are using double precision values for example, then your code can only overflow if the input data has magnitude of around 1e308.

It seems unlikely that your input data is really of that form. So my guess is that your problem is related to how you read your input data. I suspect you are reading it incorrectly and so end up performing arithmetic on meaningless values.

0
votes

After i try this and that i figured out my own mistakes, thanks to @MBo your answer narrowing mw focus in the loop, it's really stupid of me. The looping should be like

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

it is not element Data[-1] that the problem but, if length of array wavedata = X, then i try to reach the element of [X*2] which are not available it's surely after a half will cause an error. eg array[4] but i try to reach array[4*2] which is not available (Sorry for my bad english, i dont know if my explanation was good or not) but thanks everyone for the help :D