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;
8
  • how are cA1, cD1 and wavedata[0].Data declared? Commented Apr 24, 2013 at 5:46
  • 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) Commented Apr 24, 2013 at 5:50
  • 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; Commented Apr 24, 2013 at 5:51
  • 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? Commented Apr 24, 2013 at 6:00
  • 1
    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. Commented Apr 24, 2013 at 10:35

3 Answers 3

6

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?

Sign up to request clarification or add additional context in comments.

4 Comments

That's a very likely explanation. +1
@DavidHeffernan Strange as it seems, I've seen (and had the personal challenge to debug) large codebases where some arrays started at 0, some at 1, some at -1, some at -2, etc. Agreed just the same that it is a likely explanation.
yeah sorry that was a mistake too, but after i change for i:=0 to i:=1 i still get the same error though.
oh yeah i dont mention it earlier but my code work for like 1-3 second until something/some value (the cause of the error maybe) then the error pop up. because my wavedata[0].Data have maybe a lot of value in it, i tested it in matlab it has 26k(depend on .wav file size) value in the array. actually after each loop i add it into memo.lines.add and it work for just a moment then pop up the error
4

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.

3 Comments

it's good explanation for beginner like me :D. yeah i got some value that i think it exceeding the range of the datatype range like : 1.39073482014559E-309, 4.24399158143648E-314, and so on. i read my input data using some code i found, well maybe i will edit and post it to so you can see them.
i've post bit more code to add the detail how i read the .wav value, maybe you can correct me if i'm doing it wrong?
I think I answered the original question that you asked. Which was quite general in nature. I'm not in a position to debug the specifics of your code in detail. I'm sure that your input data is read incorrectly.
0

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

1 Comment

This adds nothing over MBo's answer. You should accept that answer and delete this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.