Here’s a short animation showing the effects of stacking up spectra.
Measuring the intensities and equivalent widths of emission lines can be tricky in galaxy spectra where the signal-to-noise (S/N) is low, mostly due to difficulty in determining the baseline continuum. The optical spectrum below is an example of one of the worst cases (i.e. not representative of our dataset!) where some emission lines are measurable but the continuum is ill-defined due to the high noise.
In order to double check that no systematic trends or bias are introduced in measurements for low S/N observations of a sample of galaxies, it is helpful to stack and combine all the spectra together to create an average spectrum. We can then see how measured spectral properties from the average spectra compare to the mean properties from the individual spectra.
One of the key steps for properly stacking spectra is to resample each individual spectrum to a common wavelength scale, making sure to conserve the total flux. Rather than re-invent the wheel, after some googling I found this article on the AstroBetter site. Using their example code snippet for using pysynphot to resample a 1D spectrum, given as
from pysynphot import observation from pysynphot import spectrum def rebin_spec(wave, specin, wnew): spec=spectrum.ArraySourceSpectrum(wave=wave,flux=specin) f=np.ones(len(wave)) filt=spectrum.ArraySpectralElement(wave,f,waveunits='angstrom') obs=observation.Observation(spec,filt,binset=wnew,force='taper') return obs.binflux
seemed to work straight out of the box with no additional tweaking necessary. I included this code in a python script that inputs each spectra, resamples them, then takes the median flux in each wavelength bin. It didn’t take much extra effort to export the stacked spectrum as each new observation was added to the calculation, and using ImageMagick to create an animated GIF with the terminal command
~> convert -delay 10 -loop 0 *.png stacking.gif
produced the GIF below. It nicely demonstrates how stacking more and more spectra increases the S/N ratio. This is particularly evident at the blue end of the spectrum (wavelengths < 4000 angstroms), where the H8, H9 and H10 Balmer absorption features are initially hidden under the noise and require many spectra to be stacked (N > 70) before they are easily identifiable. You’ll also notice that the continuum wiggles around a bit before settling down at a stable average continuum, which is mostly due to uncertainties in the shape of the flux calibration solution.
Finally, you can see the full size GIF by clicking on the image.