Nugroho's blog.

Friday, November 14, 2014

Discrete Fourier Transform in Delphi (in progess)

Here we go...

I plan to coding it in a way that it has flexibility in term of function. So I create two variable ft and ff, represent time domain and frequency domain function as two dimensional array, with the first index as 'function name' so it can be (in future) ft[0,i] as rect(x), ft[1,i] as cos(x) and so on.



unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, Grids, StdCtrls, Math;

type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
Image1: TImage;
Edit1: TEdit;
function fourier(a,k:integer):real;
procedure proses;
procedure fungsi(a:integer);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Image1Click(Sender: TObject);
private{ Private declarations }
public{ Public declarations }
end;
const n=37;m=5;
var
Form1: TForm1;
ft,ff: array[0..m,0..n]of real;
x0,y0:integer;
sx:real=3;
sy:real=17;

implementation

{$R *.dfm}
procedure tform1.fungsi(a:integer);
var i:integer;
begin
for i:=0 to n-1 do begin
ft[a,i]:=cos(i)+cos(2*i);
stringgrid1.Cells[1,i+1]:=inttostr(i);
stringgrid1.Cells[2,i+1]:=floattostr(ft[a,i]);
image1.Canvas.Pen.Color:=clblack;
image1.Canvas.MoveTo(x0+round(sx*i),y0);
image1.Canvas.lineTo(x0+round(sx*i),y0-round(sy*ft[a,i]));
end;
end;
function tform1.fourier(a,k:integer):real;
var i:integer;jml,j:real;
begin
jml:=0;
for i:=0 to n-1 do begin
j:=i;
jml:=jml+ft[a,i]*cos(2*PI*k*j/n);
edit1.Text:=floattostr(jml);
//application.ProcessMessages;sleep(500);
//fm:=fm+1*cos(2*PI*j*i/10/11);
end;
fourier:=jml;
end;

procedure tform1.proses;
var a,i:integer;z:real;
begin
a:=0;
stringgrid1.Cells[3,0]:='f(rect(t))';
for i:=0 to n-1 do begin
ff[a,i]:=fourier(a,i);
stringgrid1.Cells[3,i+1]:=floattostr(ff[a,i]);
stringgrid1.Cells[4,i+1]:=inttostr(round(ff[a,i]));
z:=ff[a,i];
//z:=strtofloat(stringgrid1.Cells[3,i+1]);
image1.Canvas.Pen.Color:=clblue;
image1.Canvas.moveto(x0+round(sx*i),y0);
image1.Canvas.lineTo(x0+round(sx*i),y0-round(sy*z));
stringgrid1.Cells[0,0]:=floattostr(z);
stringgrid1.Cells[0,1]:=inttostr(floor(z));
//application.ProcessMessages;sleep(500);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
proses;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
x0:=round(image1.Width/2);
y0:=round(image1.Height/2);
image1.Canvas.pen.Color:=cllime;
image1.Canvas.MoveTo(0,y0);
image1.Canvas.lineto(image1.Width,y0);
image1.Canvas.MoveTo(x0,0);
image1.Canvas.lineto(x0,image1.Height);
stringgrid1.RowCount:=n+1;
//rect(x)
fungsi(0);
//tulis stringgrid
stringgrid1.Cells[0,0]:='no';
stringgrid1.Cells[0,0]:=inttostr(round(1.5));
stringgrid1.Cells[1,0]:='t';
stringgrid1.Cells[2,0]:='rect(t)';

end;

procedure TForm1.Image1Click(Sender: TObject);
begin

end;

end.


Flexible Digital Counter using Delphi (with Recursive Procedure)

Updated version from before




unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Math;

type
TForm1 = class(TForm)
Button1: TButton;
StringGrid1: TStringGrid;
function toString(var a:boolean):string;
function denary:string;
procedure proses;
procedure tlsStrgrd;
procedure counter(l:integer);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const n=5;
var Form1: TForm1;
Q:array[0..n-1]of boolean;
l:integer=0;
jalan:boolean=false;
clock:boolean=true;

implementation{$R *.dfm}

function tform1.denary;
var i,j:integer;
begin
j:=0;
for i:=0 to n-1 do begin
j:=j+round(strtoint(toString(Q[i]))*Power(2,i));
end;
denary:=inttostr(j);
end;

function tform1.toString(var a:boolean):string;begin
toString:=inttostr(-1*strtoint(booltostr(a)));
end;

procedure tform1.counter(l:integer);
begin
if l<=n-1 then begin
Q[l]:=not Q[l];
if Q[l]=false then begin
l:=l+1;
counter(l);
end;
end;
end;

procedure tform1.tlsStrgrd;
var i:integer;
begin
for i:=0 to n-1 do begin
stringgrid1.Cells[i+2,1]:=toString(Q[i]);
end;
end;

procedure tform1.proses;
begin
clock:=not clock;
if clock=false then
begin
l:=0;
counter(l);
tlsStrgrd;
end;
stringgrid1.Cells[1,1]:=toString(clock);
stringgrid1.Cells[n+2,1]:=denary;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
jalan:= not jalan;
while jalan=true do begin
proses;
application.ProcessMessages;sleep(300);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
stringgrid1.ColCount:=n+3;
stringgrid1.Cells[1,0]:='clock';
stringgrid1.Cells[n+2,0]:='denary';
stringgrid1.Cells[n+2,1]:=denary;
stringgrid1.Cells[1,1]:=toString(clock);
for i:=0 to n-1 do begin
Q[i]:=false;
stringgrid1.Cells[i+2,0]:='Q'+inttostr(i);
stringgrid1.Cells[i+2,1]:=toString(Q[i]);
end;
end;
end.

Thursday, November 13, 2014

Recursive Procedure on Delphi.

Yup, recursive procedure (not recursive function, :) ).

I use it to create a simulation about digital asynchronous binary n-bit counter, complete with the denary representation.

n-bit means it's very flexible, you can change n and its output (stringgrid, thats it) automatically adjust itself, :)

Here's the code




unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, Grids, math;

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
BitBtn1: TBitBtn;
function denary:integer;
function tostring(a:boolean):string;
procedure counter(m:integer);
procedure proses;
procedure isiStringgrid;

procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

const n=3;
var
Form1: TForm1;
Q:array[0..(n-1)]of boolean;
clock:boolean=true;
l,denary:integer;
running:boolean=false;
implementation

{$R *.dfm}
function tform1.denary:integer;
var i,j:integer;
begin
j:=0;
for i:=0 to n-1 do begin
j:=j+round(power(2,i))*strtoint(tostring(Q[i]));
end;
denary:=j;
end;

function tform1.tostring(a:boolean):string;
begin
{}
tostring:=inttostr(-1*strtoint(booltostr(a)))
end;
procedure tform1.counter(m:integer);
begin
if l<=n-1 then begin
Q[l]:=not Q[l];
if Q[l]=false then begin
l:=l+1;
counter(l);
end;
end;
end;
procedure tform1.proses;
begin
clock:= not clock;
if clock=false then begin
l:=0;
counter(l);
isiStringgrid;
end;
stringgrid1.Cells[1,1]:=tostring(clock);
end;

procedure tform1.isiStringgrid;
var i:integer;
begin
for i:=0 to n-1 do begin
stringgrid1.Cells[2+i,1]:=tostring(Q[i]);
end;
stringgrid1.Cells[n+2,1]:=inttostr(denary);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
running:=not running;
if running=true then bitbtn1.Caption:='stop'else bitbtn1.Caption:='run';
while running=true do begin
proses;
application.ProcessMessages;sleep(500);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
stringgrid1.ColCount:=n+3;
bitbtn1.Caption:='run';
stringgrid1.Cells[1,0]:='clock';
stringgrid1.Cells[1,1]:=tostring(clock);
for i:=0 to n-1 do begin
Q[i]:=false;
stringgrid1.Cells[2+i,0]:='Q'+inttostr(i);
stringgrid1.Cells[2+i,1]:=tostring(Q[i]);
end;
stringgrid1.Cells[n+2,0]:='Denary';
stringgrid1.Cells[n+2,1]:=inttostr(denary);
end;

end.

The screenshot.

And the result, :)

Friday, November 7, 2014

iOS and OS X's Note

Want to edit Pages document 'on the fly' but don't have a Pages on iPhone? It's OK.

Copy the content of your Page document by select all -> copy, then paste to Note app on OS X. Wait a moment, it will sync with the Note app on iPhone, complete with the formatting and the images, :)





Friday, October 31, 2014

My New Toys, :)

Action Script3

(just for self documentation)

Notice how it differs from ActionScript 2, onRelease, onEnterFrame stuff.

Anyway, I'll tinker with this for next couple days, because it could be published as androidApps.apk via Adobe Air, for iOS too.





var vx:Number = 17;
var vy:Number = 3;
var jalan:Boolean = false;
var batasDrag:Rectangle = new Rectangle(0,0,stage.stageWidth,stage.stageHeight);
addEventListener(Event.ENTER_FRAME, utama);
/* Touch and Drag Event*/
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
kotak.addEventListener(TouchEvent.TOUCH_BEGIN, tekanKotak);
kotak.addEventListener(TouchEvent.TOUCH_END, lepasKotak);
tbStart.addEventListener(TouchEvent.TOUCH_TAP, toggleStart);
function utama(evtObj :Event):void
{
info.text = 'hallo';
if (jalan)
{
//kotak.y = 156.9;
kotak.x +=  vx;
kotak.y +=  vy;
info.text = "vx = " + String(vx);
info.appendText("\nvy = " + String(vy));
if (((kotak.x+kotak.width) >= stage.stageWidth) ||(kotak.x<0))
{
vx *=  -1;
}
if (((kotak.y+kotak.height) >= stage.stageHeight) ||(kotak.y<0))
{
vy *=  -1;
}

}
}
function toggleStart(event:TouchEvent):void
{
jalan = ! jalan;
}
function tekanKotak(event:TouchEvent):void
{
event.target.startTouchDrag(event.touchPointID, false, batasDrag);
jalan = false;
//kecepatan.text = "0";
}
function lepasKotak(event:TouchEvent):void
{
event.target.stopTouchDrag(event.touchPointID);
jalan = true;
}

/*tbStart.addEventListener(MouseEvent.CLICK,tbStartRelease);
function tbStartRelease(event:MouseEvent):void
{
jalan = ! jalan;
}
*/



Tuesday, October 21, 2014

Enable SMS Forwarding between iOS 8.1 and Yosemite

No code's shown on Mac?

Me too. 

I have this solution after googling  DuckDuckGoing. 

here the excerpt 

I've found a solution on another forum and it worked for me:

Try this: Go to your iPhone Messages settings and turn off iMessages and turn it on again. Then go to the iMessages Settings on your Mac and do the same. After that it worked for me. Also make sure to activate your telephone number in the iMessages settings on your Mac.
and yeah, it works, :)











Keynote on Linux

No, there's no native iWork on my Debian Sid.

It has done via iCloud Drive and iCloud Keynote.

My keynote, wrote it using my old OS X Maverick (or maybe even when my MacBook is still using Lion or Snow Leopard, I forgot), can be accessed via Chrome browser.


323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)