HOWTO - Encode any string into a trigonometric function

Following Poromenos' nifty "Hello World!" function, Jan Krueger posted a great explanation for why it works along with a general method for producing a trigonometric function for _any_ string you like:

The magic behind Poromenos's function is the Fourier theorem: any "mostly" continuous and periodic function can be expressed as a sum of sines and cosines. I'm not going to bore you with the details; suffice to say that this also works for sampled functions, i.e. discrete series of values.

There's an algorithm called DFT (Discrete Fourier Transform) that takes a series of N complex sample values and generates a corresponding Fourier series which encodes the various sine/cosine coefficients in N complex output values. In the special case of real input values (which is an extremely common case), you can effectively throw away half of these output values and take the remaining N real/imaginary components, do a bit of magic, and end up with coefficients for a function of the form:

f(t) = x0 + x1 cos(t) + x2 sin(t) + x3 cos(2t) + x4 sin(2t) + ...

Now, f(2 pi n/N) returns exactly the (n+1)th character of the original string.

Follow the link and you'll find a nifty C program that will produce a trigonometric function for any string you like.

HOWTO: encode a string into a complicated-looking trigonometric function

Posted by Jason Striegel | Apr 5, 2008 06:49 PM
Math | Permalink | Comments (2) Bookmark and Share

Recent Entries

Comments

Newest comments listed first.

Posted by: I completely dumbfounded one of my coworkers with this today. on April 8, 2008 at 3:35 PM

I completely dumbfounded one of my coworkers with this today.

I completely dumbfounded one of my coworkers with this today. He tried to work it out in his head for about fifteen minutes before finally giving up and running it.


Posted by: Poromenos on April 16, 2008 at 6:12 PM

I never realized it could be used to dumbfound your colleagues, now I will have to start encoding random strings this way :P

Also, T-shirts of the function are finally available at http://www.cafepress.com/poromenos in various shapes!


Leave a comment



Bloggers

Welcome to the Hacks Blog!

Brian Jepson.Brian Jepson


Jason Striegel.Jason Striegel


Philip Torrone.Phillip Torrone



See all of the books in the Hacks Series!
Advertise here.

Recent Posts

www.flickr.com
photos in Hacks More photos in Hacks