Saturday, November 7, 2009

Jacobi Method example

I wrote this code a while back. Apparently, this post is getting a few hits. I refined it a bit. Also wanted to make sure it was looked right.

What is the Jacobi method? It's an iterative way of solving a system of equations. Ax = b. This is learned in Numerical Analysis classes.

Generally, you decompose a matrix into its diagonal and lower and upper triangles. Such that,

A = D + L + U

x0 is the initial guess.

Now you want to solve for x. x_approx = inverse of D * (b - (L+U)x0)

Get x_approx from this formula, then plug it into x0 and do it all over again until you reach the desired error.



% Code is for matlab / octave
format long e;
A = [ 2 -1; -1 2];
b = [ 2; 1];
k = 20;
xi = [0;0]; % initial guess

%we want
%x' = Dinv(b - (L+U)x)

% just take the diagonal of A
D=[A(1,1) 0;0 A(2,2)];

Di=inv(D);

%lower and upper triangles of A
L=[0 0; -A(2,1) 0];
U=[0 -A(1, 2); 0 0];

% this part doesn't change, so calc now
C=Di*b;
% this is the main term of the algo
M=Di*(L+U);

x = [5/3; 4/3]; % solution

abs_err = [ 100 ; 100];
abs_err = abs_err - x;
i=0;

%stop until error is <= 1e-6
while abs_err(1) >= 1e-6 && abs_err(2) >= 1e-6
xi=M*xi+C
abs_err = abs(xi - x);
fprintf('i = %f abs-err = %f %f \n',i,abs_err(1),abs_err(2));
i=i+1;
end


Friday, November 6, 2009

matlab : polynomial fitting



octave-3.2.3:4> for i = 1 : 51
> x(i) = .2*(i - 1);
> end


octave-3.2.3:6> for i = 1: 51
> y(i) = 1 - x(i);
> end

octave-3.2.3:8> polyfit(x,y,4)
ans =

1.1249e-17 -2.3654e-16 1.7285e-15 -1.0000e+00 1.0000e+00

Wednesday, November 4, 2009

Java : sample applet






import java.applet.*;
import java.awt.*;

public class A extends Applet
{
private int w, h;
public void init( )
{
w = 45;
h = 50;
}

public void paint(Graphics g)
{
g.drawRect(w, h, 20, 80);
}
}


Sample J2Me Midlet for sound recording



import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

public class VoiceRecordMidlet extends MIDlet {
private Display display;

public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(new VoiceRecordForm());
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
}

class VoiceRecordForm extends Form implements CommandListener {
private StringItem message;
private StringItem errormessage;
private final Command record, play;
private Player player;
private byte[] recordedAudioArray = null;
public VoiceRecordForm() {
super("Recording Audio");
message = new StringItem("", "Select Record to start recording.");
this.append(message);
errormessage = new StringItem("", "");
this.append(errormessage);
record = new Command("Record", Command.OK, 0);
this.addCommand(record);
play = new Command("Play", Command.BACK, 0);
this.addCommand(play);
this.setCommandListener(this);
}
public void commandAction(Command comm, Displayable disp) {
if (comm == record) {
Thread t = new Thread() {
public void run() {
try {
player = Manager.createPlayer("capture://audio?encoding=pcm");
player.realize();
RecordControl rc = (RecordControl) player.getControl("RecordControl");
ByteArrayOutputStream output = new ByteArrayOutputStream();
rc.setRecordStream(output);
rc.startRecord();
player.start();
message.setText("Recording...");
Thread.sleep(5000);
message.setText("Recording Done!");
rc.commit();
recordedAudioArray = output.toByteArray();
player.close();
} catch (Exception e) {
errormessage.setLabel("Error");
errormessage.setText(e.toString());
}
}
};
t.start();

}
else if (comm == play) {
try {
ByteArrayInputStream recordedInputStream = new ByteArrayInputStream(recordedAudioArray);
Player p2 = Manager.createPlayer(recordedInputStream, "audio/basic");
p2.prefetch();
p2.start();
} catch (Exception e) {
errormessage.setLabel("Error");
errormessage.setText(e.toString());
}
}
}
}