I don’t usually write about blog technology, but I thought this was probably worth mentioning considering it is such an annoying problem that I (and I know many others) have had. The problem is getting code into wordpress posts, don’t even mention syntax highlighting. Well, I found this wordpress plugin that not only properly formats code, but also does syntax highlighting. It supposedly uses Google’s code prettify. There is no need to write which specific language it is, just do this <code class=”prettyprint”> //Code goes here </code>, or use the pre tag. I am so glad I finally got this solved. Thanks Dean Lee.
Entries from November 2007 ↓
Google Code Prettify
November 25th, 2007 — Software
Java implementation of the FFT algorithm
November 25th, 2007 — Code, DSP, Java, Processing, Software
If you are looking use the FFT algorithm in a java application, you should probably take a look around for a well optimized and tested library. But if you want to get into it yourself, here is a start. I ported this code from C and I got that from “Numerical Recipes in C“. It was originally written by N. M. Brenner in C. This book is essential for anyone interested in DSP programming. I got it for a Numerical Methods class I took last year and I have still been finding uses for it.
For those of you who don’t know, the FFT algorithm allows you to view a discrete function (such as a sound wave) in the frequency domain. This means that you can take a buffer of sampled sound (or function) and retrieve the level of each frequency spectrum. This particular function returns a float array of the levels at each frequency bin b/w 0Hz and the Nyquist frequency. The first half of the array is the real part and the second is the imaginary. Here is a visual representation, the x axis is frequency bins, and the y axis is pressure (or energy):
This is not a very good picture, but, this is a 1 kHz sine wave. Here is the function:
public float[] four1(float data[], int nn, int isign) {
int i, j, n, mmax, m, istep;
float wtemp, wr, wpr, wpi, wi, theta, tempr, tempi;
n = nn << 1;
j = 1;
for(i=1; i
if(j > i) {
float temp;
temp = data[j];
data[j] = data[i];
data[i] = temp;
temp = data[j+1];
data[j+1] = data[i+1];
data[i+1] = temp;
}
m = n >> 1;
while(m >= 2 && j > m) {
j -= m;
m >>= 1;
}
j += m;
}
mmax = 2;
while(n > mmax) {
istep = (mmax << 1);
theta = isign*(6.28318530717959/mmax);
wtemp = sin(0.5*theta);
wpr = -2.0*wtemp*wtemp;
wpi = sin(theta);
wr = 1.0;
wi = 0.0;
for(m=1; m
for(i=m;i<=n;i+=istep) {
j = i+mmax;
tempr = wr*data[j]-wi*data[j+1];
tempi = wr*data[j+1]+wi*data[j];
data[j] = data[i] - tempr;
data[j+1] = data[i+1] - tempi;
data[i] += tempr;
data[i+1] += tempi;
}
wr = (wtemp=wr)*wpr-wi*wpi+wr;
wi = wi*wpr+wtemp*wpi+wi;
}
mmax = istep;
}
return data;
}
data[] is the discrete function to be analyzed, nn is the number of complex points. This is generally data[].length/2, and isign is for inversion. Here is some code you can us to do this in Processing:
import krister.Ess.*;
AudioInput myInput;
float gain = 10.;
int bufferSize;
void setup() {
size(512, 200);
Ess.start(this);
bufferSize=512;
myInput = new AudioInput(bufferSize);
myInput.gain(gain);
frameRate(25);
myInput.start();
noFill();
stroke(255, 255, 255);
smooth();
}
void draw() {
background(0, 0, 255);
float[] buffer=four1(myInput.buffer2,
myInput.buffer2.length/4,1);
stroke(255, 255, 255);
for(int i=0; i
line(i, height, i, height-(buffer[i]*1.4));
}
stroke(255, 0, 0);
line(width/2, 0, width/2, height);
}
Of course, you need to drop the four1 function somewhere in there. This is a little crude for sound, but it will allow you to distinguish the difference in pitch of sounds with some accuracy.
Control Arduino remotely with HTML forms
November 12th, 2007 — Arduino, Code, Hardware, Python, Software
[for everyone who is continuing to read this, it probably won't work anymore and it isn't too reliable in general. I would suggest a better method like this : http://blog.datasingularity.com/?p=148. It works for botnets right? ]
Well, this is the culmination of a lot of posts I have been writing. I devised a simple way to get HTML form data, or URL encoded CGI data, to the Arduino using a remote server. Here is the package to download. Here is a flow chart of how the system works:

form = cgi.FieldStorage()
message = form.getvalue('message')
pipe = open("./cgififo", 'w')
pipe.write(str(message))
pipe.close()
I got this fifo idea from this post. All you do is use the *nix command ‘mkfifo’ and it creates a fifo in the file-system. I used this to communicate between the CGI script and the socket server processes. To get an idea of how to get this section to run, read my post on python CGI linked above. Now to the socket server. This is basically a work in progress but I think it is good enough for right now. I encountered a lot of weird problems that caused me to write some irresponsible code but it isn’t exactly meant to be used without modification so no problem. It simply waits for a client then continually checks the fifo and passes the information off to the client. The client comes in 2 flavors. First is a bare command line interface and the second has a tkinter GUI and is multi-threaded. The problem with the bare single threaded version is that it doesn’t inform the server when the socket is disconnected. The GUI version may or may not work for you. I built a mac app in the dist folder on my intel based MacBook Pro. You can rebuild it on your machine. To get this socket stuff working, look at my post on python sockets. Now for the serial side.
Inside the socket client thread is a pySerial instance. It works exactly the same as my post on sending strings to the Arduino. It waits for the socket to get a string, sends the header to the Arduino, sends the string, then sends another header. The Arduino program is exactly the same as it is on that post as well. You should configure the program to do whatever it is you want to do with that string.
To use all this software, you have to run them in a certain order. First you give the proper permissions to the cgi script and make the fifo. Call it ‘cgififo’ so you don’t have to change any code. Then you run the socket server on your server. After that, you can run the client on your machine and connect. Here are the definitions for the fields:
For host, choose your domain name [e.g. www.datasingularity.com]. The port number is the port which you have bound your socket server. The serial location is the tty file used to identify your Arduino. Baud rate is the serial baud rate and the header character is your serial header byte. Have your ssh or telnet window open to the socket server process on the remote server. When you hit connect you should see the connection in your server window. When you hit start, it will start the child thread and start listening for data. Quit closes the connection and ‘hopefully’ tells the server that you have disconnected. If not, you will need to restart the server to use again.
In conclusion, this code may be pretty ragged, but the concept is what is important and it should be enough to get you started. You obviously can’t do anything too intense with this. It could be used to do some simple but cool things though such as controlling your house from your cell phone, or feeding your dog or cat over the internet, etc. Once again, please e-mail me if you need help getting it working for your project.
**Update** Here is a demonstration video:
Getting information to the Arduino
November 11th, 2007 — Arduino, Code, Hardware, Python, Software
There are many times when you need to design a protocol that retrieves multiple pieces of data in a single instance. You can do this by serially sending a string from your computer and letting the Arduino decide which part of the string means what. In this example, I am going to show a protocol I designed to get strings of varying lengths to the Arduino at irregular intervals decided by the computer. This particular example is a python script that prompts the user for a string and sends it to a serial-enabled LCD screen connected to the Arduino. Here is the package for download.
Because the computer decides when to send the information, hand-shaking is useless. I have decided to use a header byte to tell the Arduino that a string is coming in and the next bytes will be the data. Then I send the same header byte after the string is sent to tell the Arduino that the string transmission is over. Here is what the python side looks like:
ser.write(HEADER) #header byte
ser.write(data)
ser.write(HEADER)
For the HEADER constant, I chose ‘~’ considering I wouldn’t use that in any string literal I am sending. You can choose whatever is right for your protocol from an ASCII chart. Now for the Arduino side. First you need to establish a special “blocking” function for your serial port. A blocking function blocks everything until the serial event is completed. So, if you are trying to read a byte, it will wait until you receive the byte to move on:
byte nextByte() {
while(1) {
if(Serial.available() > 0) {
byte b = Serial.read();
return b;
}
}
}
This function waits until info is available in the buffer, then reads and returns the byte treating the buffer like a fast FIFO queue. Now, as for the main loop, you have 2 options. If your Arduino has stuff to do other than read serial data, you want to start off the main loop with:
if(Serial.available() > 0){
// do serial communications
}
// do what the Arduino needs to do all the time
Since this example is dedicated only to reading serial information. I started with my blocking function nextByte(). So, we check to see if this is the header byte ‘~’, or 126 on the ASCII chart. If it is, we go into the read loop:
char charIn = 0;
byte i = 0;
while (charIn != 126) { // wait for header again
charIn = nextByte();
stringIn[i] = charIn;
i += 1;
}
This is a pretty simple loop, it keeps reading bytes into the string until the header byte comes again, then we break the loop. From there, we can null out the string:
for (int j=i-1; j<16; j++) {
stringIn[j] = ' '; // null out string
}
The rest of the code is self-explanatory. You should be able to easily hack it to your liking.

