Skip to content

Astrophysics & AI with Python: Unlocking the Secrets of Spectral Lines and Redshift

The universe is a symphony of light, but to the naked eye, it often looks like a static painting. Stars are points; galaxies are fuzzy patches. But hidden within that light is a rich, dynamic story—a cosmic fingerprint that tells us exactly what an object is made of, how hot it is, and—most importantly—how fast it is moving away from us.

This is the power of spectroscopy, the astronomer’s most indispensable tool. By breaking light into its constituent wavelengths, we transform a single beam of photons into a detailed dataset. In this guide, we’ll explore the theoretical foundations of spectral analysis, the physics of redshift, and how to build a Python "Stellar Speedometer" to calculate the expansion of the universe from your own computer.

The Cosmic Fingerprint: Emission and Absorption

At its core, spectroscopy relies on quantum mechanics. Electrons orbiting an atom can only exist at specific energy levels. When an electron jumps between these levels, it must absorb or emit a photon with energy exactly equal to the difference between those levels.

Because energy is related to wavelength (\(E = hc/\lambda\)), every element has a unique, immutable set of "rest wavelengths" (\(\lambda_{rest}\)) where it absorbs or emits light.

  • Emission Lines: When hot, diffuse gas (like a nebula) is excited, electrons drop back down and release photons at specific wavelengths, creating bright spikes on a spectrum.
  • Absorption Lines: When light from a hot source (like a star's core) passes through cooler gas, the gas absorbs specific wavelengths, leaving dark dips.

By matching the observed pattern to known laboratory data, we can identify the chemical composition of a star billions of light-years away.

The Doppler Effect: From Sirens to Stars

While the pattern tells us what the object is, the position of the pattern tells us where it is going. This is governed by the Doppler effect—the same phenomenon that changes the pitch of an ambulance siren as it passes you.

  1. Blueshift: If an object moves toward us, light waves are compressed. The observed wavelength (\(\lambda_{obs}\)) is shorter than the rest wavelength. The spectrum shifts blue.
  2. Redshift: If an object moves away from us, light waves are stretched. The observed wavelength is longer. The spectrum shifts red.

We quantify this shift using the dimensionless parameter \(z\):

\[ z = \frac{\lambda_{obs} - \lambda_{rest}}{\lambda_{rest}} \]

For nearby objects, we can calculate velocity simply by multiplying the redshift by the speed of light (\(v \approx c \cdot z\)). However, for distant galaxies moving at a significant fraction of light speed, we must use the full relativistic formula to ensure accuracy.

Line Profile Analysis: Finding the Center

In a perfect world, a spectral line would be an infinitely thin spike. In reality, thermal motion and collisions broaden the line into a shape called a profile. To find the precise center wavelength (\(\lambda_{obs}\)) amidst noisy data, we fit mathematical functions to the data:

  • Gaussian: Models pure thermal broadening (a standard bell curve).
  • Lorentzian: Models pressure broadening (heavy wings, falling off slowly).
  • Voigt Profile: The gold standard. It is the convolution of both Gaussian and Lorentzian profiles, representing the complex physics of stellar atmospheres.

This fitting process is essentially an optimization problem. Just as we use gradient descent to train neural networks, we use non-linear least-squares fitting (like scipy.optimize.curve_fit) to minimize the difference between our mathematical model and the observed data, extracting the true \(\lambda_{obs}\).

Python Implementation: The Stellar Speedometer

Let’s put theory into practice. The following Python script acts as a "Stellar Speedometer." We will simulate observing the famous H-alpha emission line (Rest wavelength: \(6563.0\) Å) from a distant galaxy and calculate its recession velocity.

import numpy as np

# 1. Define Universal Constants
# Speed of light (c) in kilometers per second (km/s).
C = 299792.458 

# 2. Define Rest Wavelength (The Theoretical Baseline)
# The H-alpha emission line wavelength (in Angstroms).
lambda_rest = 6563.0 

# 3. Define Observed Wavelength (The Simulated Data Input)
# Simulated observation of the H-alpha line from a distant source.
# Note: This observed value (7219.3 Å) is longer than the rest value, indicating redshift.
lambda_observed = 7219.3 

# 4. Calculate Redshift (z)
# The fundamental definition of redshift (z): 
# z = (lambda_observed - lambda_rest) / lambda_rest
redshift_z = (lambda_observed - lambda_rest) / lambda_rest

# 5. Calculate Velocity (v) using the Non-Relativistic Approximation
# For small redshifts (z < 0.1), the velocity (v) is approximated by v = c * z.
velocity_km_s = C * redshift_z

# 6. Output and Interpretation
print(f"--- Astrophysical Redshift Calculation ---")
print(f"Rest Wavelength (Hydrogen H-alpha): {lambda_rest:.2f} Å")
print(f"Observed Wavelength:                {lambda_observed:.2f} Å")
print("-" * 40)
print(f"Calculated Redshift (z):            {redshift_z:.4f}")
print(f"Inferred Recession Velocity (v):    {velocity_km_s:.2f} km/s")

# 7. Relativistic Check (Advanced Context)
# If velocity is > 10% of c, the non-relativistic approximation introduces error.
if velocity_km_s > 0.1 * C:
    print("\n[WARNING] Velocity exceeds 10% of speed of light. Using relativistic correction.")
    relativistic_factor = ((redshift_z + 1)**2 - 1) / ((redshift_z + 1)**2 + 1)
    velocity_relativistic = C * relativistic_factor
    print(f"Relativistic Velocity (v_rel):      {velocity_relativistic:.2f} km/s")

Code Breakdown

  1. Constants & Inputs: We define the speed of light and the H-alpha rest wavelength (\(6563.0\) Å). The lambda_observed represents our raw data point.
  2. The Redshift Calculation: We apply the formula \(z = (\lambda_{obs} - \lambda_{rest}) / \lambda_{rest}\). A positive \(z\) confirms the galaxy is receding.
  3. Velocity Conversion: We multiply \(z\) by \(c\) to get a physical speed. In our example, the galaxy is moving away at roughly \(30,000\) km/s.
  4. Relativistic Warning: The code checks if the velocity is significant. If we were observing a quasar with a redshift of \(z=1\), the simple \(v=c \cdot z\) formula would break down, and we would need the relativistic correction included in the script.

Conclusion

Spectroscopy is the bridge between raw starlight and physical understanding. By analyzing the shift in emission lines, we can measure the velocity of distant galaxies. By combining these velocities with Hubble's Law (\(v = H_0 \cdot D\)), we can map the expansion of the universe itself.

Whether you are fitting a Gaussian curve to noisy data or calculating the relativistic velocity of a quasar, the combination of astrophysics and Python provides the tools to decode the universe's hidden messages.

Let's Discuss

  1. The Limits of Light: If we discovered a galaxy with a redshift of \(z = 10\), why would our simple non-relativistic velocity calculation (\(v = c \cdot z\)) be completely invalid? What does a redshift that high physically imply about the space between us and that galaxy?
  2. AI in Spectroscopy: Modern telescopes generate terabytes of spectral data every night. How do you think machine learning algorithms (like the ones used to fit the Voigt profiles mentioned in the text) could improve the speed or accuracy of detecting exoplanet atmospheres compared to manual analysis?

The concepts and code demonstrated here are drawn directly from the comprehensive roadmap laid out in the ebook Astrophysics & AI: Building Research Agents for Astronomy, Cosmology, and SETI. You can find it here: Leanpub.com or here: Amazon.com. Check all the other programming ebooks on python, typescript, c#: Leanpub.com or Amazon.com.



Code License: All code examples are released under the MIT License. Github repo.

Content Copyright: Copyright © 2026 Edgar Milvus | Privacy & Cookie Policy. All rights reserved.

All textual explanations, original diagrams, and illustrations are the intellectual property of the author. To support the maintenance of this site via AdSense, please read this content exclusively online. Copying, redistribution, or reproduction is strictly prohibited.