# How to do logarithmic wavelength binning (in Python) in order to find redshift?

In Tonry and Davis (1979), p.1513, they formulated a cross-correlation method for extracting velocity redshifts:

## Theory of Correlation Analysis

### a) Introduction

Let $$g(n)$$ be the spectrum of a galaxy whose redshift and velocity dispersion are to be found and let $$t(n)$$ be a template spectrum of zero redshift and instrumentally broadened stellar-line profiles. These spectra are discretely sampled into $$N$$ bins, labeeled by bin number $$n$$; the relationship between wavelength and bin number is

$$n = A \ln \lambda + B$$ Because the spectra are binned linearly with $$\ln \lambda$$ a velocity redshift is a uniform linear shift. The spectra are assumed periodic with period $$N$$ [...]

They bin the linear spectra onto a logarithmic wavelength scale. Similar work was done by Baldry et. al (2014) and I have inserted a relevant excerpt from TD79. I understand that log wavelength binning is a pre-requisite of cross-correlation procedure.

I have been trying to follow this approach and to rebin linear spectra (3000 to 9000 Angstroms) into equal intervals of log wavelength. Could someone please give me a guidance about how to do this logarithmic re-binning in Python?

Here is the text as picture:

• I got as far as $$\frac{dS}{d\log(\lambda)} = \frac{dS}{\frac{1}{\lambda}d\lambda} = \lambda \frac{dS}{d\lambda}$$ but it's 1 AM, that may be the wrong way to go, and I've got to "close up shop" for the night. These days we may not need to re-bin the spectrum so much as we just need to adjust what's plotted. I'll look at this again tomorrow, but I have a hunch someone will be able to write an authoritative answer a lot faster than I, that is unless it gets closed and everyone has to wait for it to be reopened again.
– uhoh
Jul 31, 2019 at 17:11
• @CarlWitthoft there are 228 posts that mention python here and 46 questions using the python tag, 16 tagged Astropy and 26 mention Skyfield and 34 mention PyEphem . The mentioning of python does not make a question off-topic. Let's leave it open and get this astronomy question answered!
– uhoh
Jul 31, 2019 at 22:26
• While I normally agree with uhoh on most borderline close issues, I disagree here. Astronomical knowledge is irrelevant to answering the question. Python (and general stats) knowledge is relevant. If you exchange the topic for something else (logarithmic binning for income distribution,say), the fundamental question remains the same and it can be answered the same. If you replace the logarithmic binning with a different technique, the question becomes different and the answer different. Aug 1, 2019 at 2:52
• This (unanswered) meta question might be an appropriate place to discuss the relevance of this question. astronomy.meta.stackexchange.com/questions/513/… Aug 1, 2019 at 2:58
• @Ingolifs while the absolute minimal answer imaginable might possibly squeeze by without touching on Astronomy, a good quality answer (remember those?) to the question will address the astronomical aspects of it head-on and add value to the site for future readers. (just for example padding after binning for the Fourier transform that comes next) I don't think "I could answer this while staying off-topic, so nobody could possibly write an on-topic answer (sic)" is the right way to think here.
– uhoh
Aug 1, 2019 at 7:20

I think you could just take the natural log of your wavelength scale, then re-bin this onto a new log of wavelength scale generate using np.linspace(). Your new scale would be generated using the maximum and minimum values of your raw log of wavelength scale then it is up to you to define the amount of bin in the new homogenous log scale.

To re-bin the raw log of wavelength scale onto the homogenous log of wavelength scale you would assign fractions of the old flux values into the new bins where the fractions are determined by the fraction of the old bin (that the flux value was in) that overlaps the new bin.

In the end you will need this to be on kilometres per second or a unit of velocity. I expect you would simply multiply the scale by the speed of light although I have experienced slight loss of precision when I do this.

• Welcome to astronomy SE! I slightly edited your answer, hope you are ok with the changes. Feb 25, 2021 at 11:22

I know you probably want to do and understand it yourself - Brian's answer is fine in that regard - but sometimes why reinvent the wheel?

This is a standard task implemented in the specutils python library.

You can also do the cross-correlation with specutils.

Note that these days one doesn't generally use the Tonry & Davis FFT method, because the available computing power is so much greater; a pixel-by- pixel cross-correlation calculation is usually feasible. (But the logarithmic binning is still necessary).