Re: Faster Frac() imlementation?



Hello Anybody,

<buramu@xxxxxxxxx> schrieb im Newsbeitrag
news:d6b749d5-6577-4fb4-900c-5d500b2ea123@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Does something faster for Frac also exist?

Change all to integer-mathematics, than you don't need ceil or frac.
(my crystal ball is broken, show me the code ;-)

Here is my most simplified linear interpolation mechanism.
I don't see how floating point maths can be avoided.

You can use fixpoint arithmetic e.g. 24:8 (in a 32bit Integer)
when 0..1 can represented with 8 Bits (0..255).

//round the current pointer into my sample data up
p2 := ceil( fIndex );
// determine the previous sample index (and avoid going lower than
sample 0)
p1 := pred( p2 );
if p1 < 0 then p1 := 0;
// pointer is probably somewhere between samples.. determine where...
fract := frac( fIndex );
// get the 2 nearest samples from the sample data
h1 := sample[p1];
h2 := sample[p2];
// now calculate the interpolated value using the difference between
these two samples
delta := h2 - h1;
result := round( h1 + ( fract * delta ) );

You can play with 4 Scollbars and this:

procedure TForm1.ScrollBarSample1_0_65535Change(Sender: TObject);
begin
Calculate;
end;

procedure TForm1.ScrollBarSample2_0_65535Change(Sender: TObject);
begin
Calculate;
end;

procedure TForm1.ScrollBarGradient_0_255Change(Sender: TObject);
begin
Calculate;
end;

procedure TForm1.Calculate;
var
s1,s2,g,t:integer;
begin
s1:= ScrollBarSample1_0_65535.Position; // First sample 16bit
s2:= ScrollBarSample2_0_65535.Position; // Second sample 16bit
g := ScrollBarGradient_0_255.Position; // gradient 8bit

t := s1+ (((s2-s1)*g) div 256);

ScrollBarTarget_0_65535.Position := t;

Caption := '('+IntToStr(s1)+'..'+IntToStr(s2)+') ['+FormatFloat('0.000',
g/256)+']='+IntToStr(t)
end;

###### [not tested !!! only an example/examination to the understanding, it
can be optimize]

// determine the previous sample index
p1 := fIndex div 256;
p2 := p1 + 1;

// pointer is probably somewhere between samples.. determine where...
fract := fIndex and 255;

// get the 2 nearest samples from the sample data
h1 := sample[p1];
h2 := sample[p2];

// now calculate the interpolated value using the difference between these
two samples
delta := h2 - h1;
result := h1+ ((delta*frac) div 256);

######

mfg.
Herby

--
http://www.hubert-seidel.de


.



Relevant Pages

  • Re: Faster Frac() imlementation?
    ... Here is my most simplified linear interpolation mechanism. ... fract:= frac(fIndex); ... // get the 2 nearest samples from the sample data ...
    (borland.public.delphi.language.basm)
  • Min num of total sample points for tricubic convolution?
    ... I have a volume filled with scattered sample data (sort of like a body ... of water with measured temperatures for different locations). ... planning on using interpolation to get a *rough* estimate of the ...
    (sci.math)