Image to Spectrogram
Overview
This project will take a digital picture and convert it into a wave file. Creating a spectrogram of that file then reproduces that picture.
Algorithm
Treat the image as a matrix of pixels with r,g,b values. Go through 1 column of the matrix for a given time interval and use the following formula to get a suitable frequency:
We can get some coloration by changing the decibels for each frequency based on the pixel RGB values. Decibel level is division of an exponent of 10 following the form:
All frequencies for a column are merged using:
+ sin(2*pi + sampleN)/decibelN) / N
Results(Click spectrograms for bigger pictures)
Note that color is just treated as a grayscale, since there is only 1 dimension to decibel level. So it really is treating it like this:
Edit: Following the suggestion below the picture was generated with the following command: sox fract.wav -n spectrogram -x 4 -Z -60 -z 60
Downloads and Execution
imageSpectrogam.pl -- The main Perl script. Will handle png, jpg, or gif files, and requires the extension is present in the name. Simplest way to run it is with ./imageSpectrogram.pl image_filename.
There are 2 Perl CPAN dependencies needed:- cpan Audio::Wav
- cpan GD
Conclusion
I originally tried just doing a bash script with SoX calls, but figured out that it doesn't handle merging 100+ frequencies. But after I found this page showing how add a frequency in Perl, it was pretty simple. Overall it works as expected, but is horribly slow on larger images with lots of color.
Comments(5)
2009-05-10 20:36:03
(2010-06-24 04:17:47) Brent Fisher said:
For those writing a program to generate audio whose spectrogram is an arbitraryimage, I should tell you that in order for the resulting sound wave to sound
pleasant to the ear, you need to set the starting phase of each frequency to a
random degree/radian value. Then leave the phase alone. I tried a similar thing
in C programming using an FFT and was getting "click-click-click" sounds which
sounded awful. The phase needs to be randomized at the start of the signal. Once
I figured that one out it sounded as good as the commercial image to
spectral-audio programs.
(2009-11-09 16:28:53) Gavin Black said:
DM DOKURO, you need to install the module (Audio::Wav) from cpan(http://search.cpan.org/dist/Audio-Wav/Wav.pm) if you haven't already
If you're Linux/Unix/Cygwin/OSX try (As root):
cpan -i Audio::Wav
You will also need GD (cpan -i GD), these are the only 2 dependencies.
You can e-mail me if you need more help. It's listed in the about section (Not
putting it in the comments to prevent spam to the address).
(2009-11-08 04:27:33) DM DOKURO said:
Err... It says that there's an error near Line 3 (Audio::wav). What does thisusually mean?
(2009-05-10 20:30:32) Gavin Black said:
Excellent suggestion! I've updated the post with a new picture.(2009-05-09 20:05:56) loudness said:
I wonder if the last pic would benefit from a little more contrast: maybe spectrogram options something like -Z -60 -z 60 ? Add your comment:
Hardware
Software
- TAIM (Alpha Version): GHCI integration with vim
- CheaTorrent -- An evil BitTorrent client
- Self Modifying 2D Turing Automata
- Competing Conway Life Automata
- X11 Timelapse Desktop Video
- Colored Wolfram Automata With Sound Input
- Pseudo Video Feedback in Processing
- Haskell Cipher Saber
- Illegal FIlenames -- Windows and *nix
- Simple Perl SDL Music Keyboard (Updated)
- Image to Spectrogram
- Pastebin Hell
- OMGWTFRNG (OWR)
- OTP Enhancement : Failure Report
- Java Network File Transfer Tool
- AES Encrypted Filesystem Speeds
- Dual Message Encryption
- PHP Website
- Mp3 Splitting Script
- Random Obfuscation Tool
- Filesystem Speed Comparisons
- Java Based Web Server GUI