Link to home
Start Free TrialLog in
Avatar of chmichael
chmichael

asked on

Nomalize and convert Float to Integer

Hello,
  I have the following arrays:

const
  cMaxSampleDuration = 420;

var
    MyDoubleArray: array[0..5, 0..cMaxSampleDuration -1] of Double;
    MyIntegerArray: array[0..5, 0..cMaxSampleDuration -1] of Integer;

Open in new window


with the following function i convert the float number to integers:

  for Channel := 0 to 6 -1 do
  for Samples := 0 to cMaxSampleDuration -1 do
    MyIntegerArray[Channel, Samples] := Round(Frac(MyDoubleArray[Channel, Samples]) * 200000);

Open in new window


Comment: Since it's a wave i care only for the  fractional part of the float not the integer.

Now, i want to normalize the array so the scale up every channel to be in the same range.
(Like normilizing a waveform, in this case 6 channels/waves)

Thank you
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Must pre-scan all data and get maximum value for each channel. Then go upscale data.
const
  cMaxSampleDuration = 420;

var
    MyDoubleArray: array[0..5, 0..cMaxSampleDuration -1] of Double;
    MyIntegerArray: array[0..5, 0..cMaxSampleDuration -1] of Integer;
    Channel, Samples: Integer;

    ScaleValue: array[0..5] of Double;
    AllMaxValue, GlobalScale: Double;
....
  //get scale values
  AllMaxValue := 0;
  for Channel := 0 to 6 -1 do
  begin
    ScaleValue[Channel] := 0;
    for Samples := 0 to cMaxSampleDuration -1 do
    begin
      if MyDoubleArray[Channel, Samples]>ScaleValue[Channel] then
        ScaleValue[Channel] := MyDoubleArray[Channel, Samples];
    end;
    if ScaleValue[Channel]>AllMaxValue then
      AllMaxValue := ScaleValue[Channel];
  end;

  //scaling
  GlobalScale := 200000;
  for Channel := 0 to 6 -1 do
  begin
    for Samples := 0 to cMaxSampleDuration -1 do
      MyIntegerArray[Channel, Samples] :=
        Round(Frac(MyDoubleArray[Channel, Samples] * AllMaxValue / ScaleValue[Channel]) * GlobalScale);
  end;

Open in new window

Avatar of chmichael
chmichael

ASKER

It doesn't work with negative numbers! Can you take a look ?

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial