« Observatories: Wast Hills | Main | Cassiopeia with the BRT »

Python: blackbody curves

I decided to put a little bit of python code together as a demonstration of how to define a function by calculating the blackbody curve of an object (see the bottom of the entry if you want to know what this is), anyway I thought I'd share this little snippet of code.. its not anything particularly outstanding but tis neat. How to define a blackbody:

def blackbody_at_f_t(freq,kB,T,c,h): #T in K, f in Hz return 2*h*(freq**3)/(c**2)*(1/(exp((h*freq)/(kB*T))-1))

You just need to pass freq - the observing frequency (in Hz), kB - the Boltzmann constant (in J/K), T - the temperature (in K), c - the speed of light (in m/s) and h - the Planck constant (in JS). This show just how simple python can be to define a function. Below is an example of actually using this, and of course we can nicely vectorise things too... and plot!

def blackbody_at_f_t(freq,kB,T,c,h): #T in K, f in Hz return 2*h*(freq**3)/(c**2)*(1/(exp((h*freq)/(kB*T))-1))

#Python Libraries
from pylab import *
from scipy import *
from numpy import *

#constants
kB=1.38066E-23; # Boltzmann's constant in J/K
c=3.0E8
h = 6.63E-34 #Planck's constant (JS)

logspace = 10.**linspace(1.0, 20, 5000) #logspacing of frequency
T =2.715 #K cosmic background radiation
out = blackbody_at_f_t(logspace,kB,T,c,h)
fig = plt.figure(figsize=(6, 6))
ax1 = fig.add_subplot(111)
plot(logspace/1E9,out,'r-',markersize=0.5)
ax1.set_xlabel('Frequency (GHz)', fontsize=10); ax1.set_ylabel('Energy Density (J/Hz)', fontsize=10)
xlim(0.0,800)
show()

The whole above code can be found in [blackbody.py]

and you end up with blackbody curve of the cosmic microwave background:

Blackbody curve for CMB


A black body is a theoretical object that absorbs 100% of the radiation that hits it, hence it appears to be black. Due to this nature its also a perfect emitter of thermal radiation. which it radiates in a characteristic spectrum that depends on the body's temperature - as can be seen above for the cosmic microwave background. If you want to know more about the cosmic microwave background try [here]

TrackBack

TrackBack URL for this entry:
http://krioma.net/cgi-bin/MT-3.38/mt-tb.cgi/543

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on September 7, 2011 9:31 PM.

The previous post in this blog was Observatories: Wast Hills.

The next post in this blog is Cassiopeia with the BRT.

Many more can be found on the main index page or by looking through the archives.

Get Firefox! Valid XHTML 1.0! Valid CSS! RSS Feed BlogUniverse - listed Powered by Apache Creative Commons License ringsofsaturnrock's Most Interesting Photos on Flickriver

Powered by
Movable Type 3.38