Recent

Author Topic: Simple Sine Table Generator  (Read 877 times)

Gigatron

  • Sr. Member
  • ****
  • Posts: 281
  • Amiga Rulez !!
Simple Sine Table Generator
« on: May 13, 2025, 12:52:48 am »
Hi,

Sometimes if we need to optimise code we need precalculated data table;
One of my tool based on various sintable generator around www  doing that.

It can generate signed integer value sine table like on amiga to move logo or image or text on the screen;
Copy the generated code on your project from memo1Text and move your image x or y
position taken from sin_data: like move(sin_data[index],100) index is the value from sin_data table.

Let me share this code with you, be carefull to play with spinedit values you can encounter data
access error ... hope no !



« Last Edit: May 13, 2025, 01:21:46 am by Gigatron »
Sub Quantum Technology ! Pascal - C - C# - Java - Javascript - Glsl - Lua - Html5 - CSS - Amiga Rules !

Gigatron

  • Sr. Member
  • ****
  • Posts: 281
  • Amiga Rulez !!
Re: Simple Sine Table Generator
« Reply #1 on: May 13, 2025, 09:11:57 pm »
A simple demo using sine data table.

Beginner version code :

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses
  7.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Spin,
  8.   ExtCtrls, BGRAVirtualScreen, BGRABitmap, BGRABitmapTypes, Math;
  9.  
  10. const
  11. sin_data :Array[0..179] of Integer =(0
  12. ,2,5,7,10,12,15,17,19,22,24,27,29,31,34,36,39
  13. ,41,43,46,48,50,52,55,57,59,61,64,66,68,70,72,74
  14. ,76,78,80,82,84,86,88,90,92,94,95,97,99,101,102,104
  15. ,106,107,109,110,112,113,115,116,117,119,120,121,122,124,125,126
  16. ,127,128,129,130,131,132,132,133,134,135,135,136,136,137,137,138
  17. ,138,139,139,139,139,140,140,140,140,140,140,140,140,140,139,139
  18. ,139,139,138,138,137,137,136,136,135,135,134,133,132,132,131,130
  19. ,129,128,127,126,125,124,122,121,120,119,117,116,115,113,112,110
  20. ,109,107,106,104,102,101,99,97,95,94,92,90,88,86,84,82
  21. ,80,78,76,74,72,70,68,66,64,61,59,57,55,52,50,48
  22. ,46,43,41,39,36,34,31,29,27,24,22,19,17,15,12,10
  23. ,7,5,2);
  24.  
  25. type
  26.   { TForm1 }
  27.   TForm1 = class(TForm)
  28.     BGRAVirtualScreen1: TBGRAVirtualScreen;
  29.     Timer1: TTimer;
  30.     procedure BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
  31.     procedure FormCreate(Sender: TObject);
  32.     procedure Timer1Timer(Sender: TObject);
  33.  
  34.   private
  35.  
  36.   public
  37.   end;
  38.  
  39. var
  40.   Form1: TForm1;
  41.   txt : Array of Pchar;
  42.   s1,s2,s3,s4,s5,s6,s7 : Integer;
  43.  
  44. implementation
  45.  
  46. {$R *.lfm}
  47.  
  48. { TForm1 }
  49.  
  50. procedure TForm1.FormCreate(Sender: TObject);
  51. begin
  52.  
  53.   SetLength(txt, 1);
  54.   txt[0] := 'LAZARUS';
  55.   SetLength(txt ,Length(txt));
  56.   s7 := 0;
  57.   s6 := 4;
  58.   s5 := 8;
  59.   s4 := 12;
  60.   s3 := 16;
  61.   s2 := 20;
  62.   s1 := 24;
  63.  
  64. end;
  65.  
  66. procedure TForm1.BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
  67. begin
  68.  
  69.          Bitmap.FontName := 'Arial';
  70.          Bitmap.FontHeight := 72;
  71.          Bitmap.FontStyle:=[fsBold];
  72.          Bitmap.FillRect(0,0,BGRAVirtualScreen1.Width,BGRAVirtualScreen1.Height,BGRA(0,0,0));
  73.  
  74.           Bitmap.TextOut(140+60,460 -   sin_data[s1]*3,    txt[0][0], BGRA(255,255,255));
  75.           Bitmap.TextOut(140+120,460 -  sin_data[s2]*3,    txt[0][1], BGRA(255,255,255));
  76.           Bitmap.TextOut(140+180,460 -  sin_data[s3]*3,    txt[0][2], BGRA(255,255,255));
  77.           Bitmap.TextOut(140+240,460 -  sin_data[s4]*3,    txt[0][3], BGRA(255,255,255));
  78.           Bitmap.TextOut(140+300,460 -  sin_data[s5]*3,    txt[0][4], BGRA(255,255,255));
  79.           Bitmap.TextOut(140+360,460 -  sin_data[s6]*3,    txt[0][5], BGRA(255,255,255));
  80.           Bitmap.TextOut(140+420,460 -  sin_data[s7]*3,    txt[0][6], BGRA(255,255,255));
  81.  
  82.           inc(s1,2);inc(s2,2);inc(s3,2);inc(s4,2);inc(s5,2);inc(s6,2);inc(s7,2);
  83.  
  84.            s1 := s1 mod Length(sin_data);
  85.            s2 := s2 mod Length(sin_data);
  86.            s3 := s3 mod Length(sin_data);
  87.            s4 := s4 mod Length(sin_data);
  88.            s5 := s5 mod Length(sin_data);
  89.            s6 := s6 mod Length(sin_data);
  90.            s7 := s7 mod Length(sin_data);
  91.  
  92. end;
  93.  
  94. procedure TForm1.Timer1Timer(Sender: TObject);
  95. begin
  96.     BGRAVirtualScreen1.RedrawBitmap;
  97. end;
  98.  
  99. end.

Optimized Version code in project ;

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  6.   ExtCtrls, BGRAVirtualScreen, BGRABitmap, BGRABitmapTypes;
  7.  
  8. const
  9.   // Sinus table
  10.   sin_data: array[0..179] of Integer = (0,2,5,7,10,12,15,17,19,22,24,27,29,31,34,36,39
  11.                                        ,41,43,46,48,50,52,55,57,59,61,64,66,68,70,72,74
  12.                                        ,76,78,80,82,84,86,88,90,92,94,95,97,99,101,102,104
  13.                                        ,106,107,109,110,112,113,115,116,117,119,120,121,122,124,125,126
  14.                                        ,127,128,129,130,131,132,132,133,134,135,135,136,136,137,137,138
  15.                                        ,138,139,139,139,139,140,140,140,140,140,140,140,140,140,139,139
  16.                                        ,139,139,138,138,137,137,136,136,135,135,134,133,132,132,131,130
  17.                                        ,129,128,127,126,125,124,122,121,120,119,117,116,115,113,112,110
  18.                                        ,109,107,106,104,102,101,99,97,95,94,92,90,88,86,84,82
  19.                                        ,80,78,76,74,72,70,68,66,64,61,59,57,55,52,50,48
  20.                                        ,46,43,41,39,36,34,31,29,27,24,22,19,17,15,12,10,7,5,2);
  21.  
  22.   TEXT_TO_DISPLAY = 'LAZARUS 4.0';
  23.   pos_X = 0;
  24.   pos_Y = 430;
  25.   amp   = 3;
  26.   phase = 4;
  27.   anim_speed = 2;
  28.  
  29. type
  30.   { TForm1 }
  31.   TForm1 = class(TForm)
  32.     BGRAVirtualScreen1: TBGRAVirtualScreen;
  33.     Timer1: TTimer;
  34.     procedure BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
  35.     procedure FormCreate(Sender: TObject);
  36.     procedure Timer1Timer(Sender: TObject);
  37.   private
  38.     FPhases: array of Integer;
  39.   public
  40.   end;
  41.  
  42. var
  43.   Form1: TForm1;
  44.   X_Space : Integer;
  45.  
  46. implementation
  47. {$R *.lfm}
  48.  
  49. { TForm1 }
  50.  
  51. procedure TForm1.FormCreate(Sender: TObject);
  52. var
  53.   i: Integer;
  54. begin
  55.  
  56.   X_Space := 60;
  57.   SetLength(FPhases, Length(TEXT_TO_DISPLAY)); // num letters
  58.  
  59.   for i := 0 to High(FPhases) do     // downto ?
  60.     FPhases[i] := i * phase;  // each letter y pos pointer in sin table
  61. end;
  62.  
  63. procedure TForm1.BGRAVirtualScreen1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
  64. var
  65.   i: Integer;
  66.   x, y: Integer;
  67.   s: string;
  68. begin
  69.  
  70.   Bitmap.FontName := 'Courier New';
  71.   Bitmap.FontHeight := 90;
  72.   Bitmap.FontStyle := [fsBold];
  73.  
  74.   for i := 0 to Length(TEXT_TO_DISPLAY) - 1 do
  75.   begin
  76.     x := pos_X + (i + 1) * X_Space;
  77.     y := pos_Y - sin_data[FPhases[i]] * amp;
  78.     s := TEXT_TO_DISPLAY[i+1]; // +1
  79.     Bitmap.TextOut(x, y, s, BGRA(255, 255, 255));
  80.     Bitmap.TextOut(x-4, y-4, s, BGRA(255, 0,0));
  81.     FPhases[i] := (FPhases[i] + anim_speed) mod Length(sin_data);
  82.   end;
  83.  
  84. end;
  85.  
  86. procedure TForm1.Timer1Timer(Sender: TObject);
  87. begin
  88.   BGRAVirtualScreen1.RedrawBitmap;
  89. end;
  90.  
  91. end.
Sub Quantum Technology ! Pascal - C - C# - Java - Javascript - Glsl - Lua - Html5 - CSS - Amiga Rules !

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Simple Sine Table Generator
« Reply #2 on: May 17, 2025, 10:28:35 am »
Inside my source, my SinTable have more precision a,at least 4digits.
I'm afraid that somehow there is a misunderstanding about the purpose of the program made by Gigatron.

It is not about precision but about speed and memory-usage. Perhaps not that interesting anymore these days but as a comparison how exactly would you calculate 4 digit-precision on a 8-bit CPU or a CPU that does not contain an FPU ? And perhaps as equal important, how does one use that in practice on such CPU ? It serves hardly any purpose and complicate matters.

If wanted you could use fixed point integers or scaling but that is not the purpose of the demonstration.

Precalculating an algorithm allows for example to use absolute coordinates (which saves another addition/shift), allows to pre-apply ratios and other useful otherwise costly calculations beforehand which saves cpu-cycles not only because of the calculation already been done but because that way you only have to take care of indices and/or move around memory of the pre-calculated data.

On the other hand using this technique also has a turning-point where pre-calculation has no use anymore as it either will take too much time to render (all) the values, lost too much precision and/or requires too much storage space. Newer techniques were developed and combined in order to improve on these limitations.

As said, the technique as demonstrated does probably not serve much purpose anymore on modern CPU's but it is nice to see these old techniques put to use. Though, all that buzz stuff of today is also just all about some random iteration on some random pre-calculated data  :)

fwiw: SinusMaker(tm) is a well-known and perfect example of such tool that belong(s/ed) to every developers toolkit (back) in the 8-bit era. The used techniques are still valid though applied on/to/for other matters these days.
Today is tomorrow's yesterday.

Gigatron

  • Sr. Member
  • ****
  • Posts: 281
  • Amiga Rulez !!
Re: Simple Sine Table Generator
« Reply #3 on: May 17, 2025, 11:21:35 am »
Inside my source, my SinTable have more precision a,at least 4digits.
Can you use myTableCos and myTableSin?
iF you other CircleRounded equations, then use  AcDbEllipse[pc]
 8-)
Hi,

Sometimes if we need to optimise code we need precalculated data table;
One of my tool based on various sintable generator around www  doing that.

It can generate signed integer value sine table like on amiga to move logo or image or text on the screen;
Copy the generated code on your project from memo1Text and move your image x or y
position taken from sin_data: like move(sin_data[index],100) index is the value from sin_data table.

Let me share this code with you, be carefull to play with spinedit values you can encounter data
access error ... hope no !

Hi,

If I had wanted to get out of floating-point values, i would have done it here, the purpose is explained by @Tron is sufficient. Don't forget my programs aim for simplicity for those who are new to Pascal and the Amiga 68000 spirit which has no FPU :) When you disassemble a demo on the Amiga 68000, the sine tables are often signed integers if there is no 3D.
 @Tron thank you for your clarification on the subject;

Regars Gigatron
Sub Quantum Technology ! Pascal - C - C# - Java - Javascript - Glsl - Lua - Html5 - CSS - Amiga Rules !

d2010

  • Full Member
  • ***
  • Posts: 171
Re: Simple Sine Table Generator
« Reply #4 on: May 17, 2025, 07:16:51 pm »
I am sorry , at previous message on this Topic, I got many error/s at Online-broken as Opera.exe.
I am sorry, Please, you here is ..
You search "function CorrectAngleIfNotReadable(Angle:ads_real):ads_real;"
 ;D
Can you replace "sin_data" with my-sin-table"?
Please, You quick-watch, and you respond, You got , what you got?
Thanks/thank you/
Best Regards.
« Last Edit: May 17, 2025, 07:20:26 pm by d2010 »

cdbc

  • Hero Member
  • *****
  • Posts: 2214
    • http://www.cdbc.dk
Re: Simple Sine Table Generator
« Reply #5 on: May 17, 2025, 07:48:42 pm »
Hi
@d2010: STOP TROLLING and wasting peoples time!
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

 

TinyPortal © 2005-2018