Solved

Nomalize and convert Float to Integer

Posted on 2013-05-13
3
431 Views
Last Modified: 2013-05-21
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
0
Comment
Question by:chmichael
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 39163176
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

0
 

Author Comment

by:chmichael
ID: 39167465
It doesn't work with negative numbers! Can you take a look ?

Thank you
0
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 39177624
Try this modification:
//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 Abs(MyDoubleArray[Channel, Samples])>Abs(ScaleValue[Channel]) then
        ScaleValue[Channel] := MyDoubleArray[Channel, Samples];
    end;
    if Abs(ScaleValue[Channel])>Abs(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] * Abs(AllMaxValue) / Abs(ScaleValue[Channel])) * GlobalScale);
  end;

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question