Jonathan Thomson's web journal

Code November 14, 2010

Spectrometer Code
The spectrometer code consists of several functions and scripts written for the GNU Octave/MATLAB environment. The code supports JPEG spectrographs and raw spectrographs decoded to PGM with the help of dcraw. Within the archived code there are two primary functions: wavelength_calibrate and image2spectrum, several support scripts and functions, sample spectrographs, and example scripts that demonstrate how to use the provided functions to turn the spectrographs of a cyan LED and incandescent bulbs into radiometrically calibrated spectrograms. You are free to reuse the code in accordance with the BSD open source license.

spectrometer code README.TXT — be sure to call setup_path.m when you start a new session with the spectrometer code.

Browse pretty print spectrometer code source files

spectroradiometer code only (github) — This repository contains only the spectrometer code. It does not provide the pre-processed spectrograph mat-files nor raw spectrographs. None of the primary scripts nor example scripts will work until you provide them with your own data or sample data to process.

If you’d like to try out the code with some sample data, download one of the sample data archives below. Two archives are offered, but only one is needed. It’s better to get, but if you have trouble downloading a file that large you can get which is a third the size.

sample spectrographs zip file (, 969 MB) — This archive contains pre-processed spectrograph mat-files and raw spectrographs. With this sample data set: All of the example scripts will work. The primary script make_master_dark_flat_bias.m will not work because the frames it processes are not included in this archive to save space.

sample pre-processed spectrograph mat-files zip file (, 318 MB) — This archive contains pre-processed spectrograph mat-files. With this sample data set: All of the example scripts will work. The primary scripts batch_preprocess.m, batch_raw_decode.m, and make_master_dark_flat_bias.m will not work because the frames they process are not included in this archive to save space.

Important Functions
image2spectrum() – converts a spectrograph to a spectrogram by summing the columns within a region of interest.
wavelength_calibrate() – takes a spectrograph of a CFL bulb and plots a spectrogram, requests that the user picks out specific spectral peaks, and then uses the peaks pixel locations to calculate a wavelength scale to replace the column number scale.
bayer_demosaic() – converts a Bayer raw image into an RGB image matrix by interpolating missing pixels in a channel from the nearest neighbors in that channel.
generate_Wbb_spectrum() – outputs the emissivity corrected, black-body spectrum of a tungsten filament incandescent bulb.

Important Scripts
batch_preprocess – ensemble averages and calibrates (noise reduction) every spectrograph. This script needs to be run once for every new set of spectrographs.
batch_raw_decode – decodes all raw images found within a parent directory and all of it’s children directories. This script needs to be run once for every new set of spectrographs.
make_master_dark_flat_bias – converts a collection of calibration frames into master calibrations frames. This script only needs to be run on a new set of calibration frames. It’s necessary to take a new set of dark frames if you change the exposure time. Each camera needs it’s own set of calibration frames.


CHDK scripts and grid contains two lua scripts and a custom grid. shootn.lua sets the camera’s exposure time, aperture, zoom, focus, and ISO, then shoots N spectrographs. get_values.lua outputs the current camera settings, which can then be used to modify shootn.lua to alter the camera settings desired for shooting spectrographs. These scripts have only been tested on my Canon PowerShot A590, so you may have to modify them to work on your camera. Neither script is essential for taking spectrographs; they simply help automate the process. cfl.grd is a grid of vertical lines that occur at the locations of the most pronounced spectral peaks of a mercury compact fluorescent lamp’s spectrograph as recorded by my A590. You may not be able to use the grid directly, but you should be able to modify it easily so it better matches the CFL spectrum your camera captures.


Continue Reading

<<<Previous — Results<<<



2 Responses to “Code”

  1. sunwukong Says:

    When I first started trying to build a Spectrogrometer I was using Win XP pro. I knew about CHDK and found that aparently no one was trying to use it for spectroscopy. I then expanded my computer to Win 8 and found that was a disaster. Then I tried Ubuntu and found gphoto2 could do a few things mainly take remote photos and do nanosecond time stamps. Then I found your page. I have a canon A620 that seems to be respoinding well to Cam4You Remote (in windows 8) and gphoto2 in Ubuntu 12.04, (Im also still using Win Xp pro (sp3) and on a different machine. I don’t do twitter and the like so I hope this comment makes its way to you. Thank you for sharing this effort. Amazing stuff

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s