<p>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 <code>np.linspace()</code>.
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.</p>
<p>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.</p>
<p>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.</p>

CC BY-SA 4.0
2021-02-25T11:22:05.210Z
Welcome to astronomy SE! I slightly edited your answer, hope you are ok with the changes.
B--rian
CC BY-SA 4.0
2021-02-24T21:42:45.283Z
2021-02-25T11:21:13.930Z
2021-02-25T11:21:13.930Z
Zac
<p>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?</p>
<p>This is a standard task implemented in the <a href="https://specutils.readthedocs.io/en/stable/api/specutils.analysis.template_logwl_resample.html#specutils.analysis.template_logwl_resample" rel="nofollow noreferrer">specutils</a> python library.</p>
<p>You can also do the cross-correlation with specutils.</p>
<p>Note that these days one doesn't generally use the Tonry &amp; 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).</p>

CC BY-SA 4.0
2023-02-08T07:52:41.173Z
2023-02-08T07:52:41.173Z
ProfRob
<p>In <a href="http://adsabs.harvard.edu/abs/1979AJ.....84.1511T" rel="nofollow noreferrer">Tonry and Davis (1979), p.1513</a>, they formulated a cross-correlation method for extracting velocity redshifts:</p>
<blockquote>
<h2>Theory of Correlation Analysis</h2>
<h3>a) Introduction</h3>
<p>Let <span class="math-container">$g(n)$</span> be the spectrum of a galaxy whose redshift and velocity dispersion are to be found and let <span class="math-container">$t(n)$</span> be a template spectrum of zero redshift and instrumentally broadened stellar-line profiles. These spectra are discretely sampled into <span class="math-container">$N$</span> bins, labeeled by bin number <span class="math-container">$n$</span>; the relationship between wavelength and bin number is</p>
<p><span class="math-container">$$n = A \ln \lambda + B$$</span>
Because the spectra are binned linearly with <span class="math-container">$\ln \lambda$</span> a velocity redshift is a uniform linear shift. The spectra are assumed periodic with period <span class="math-container">$N$</span> [...]</p>
</blockquote>
<p>They bin the linear spectra onto a logarithmic wavelength scale. Similar work was done by <a href="https://arxiv.org/abs/1404.2626" rel="nofollow noreferrer">Baldry et. al (2014)</a> and I have inserted a relevant excerpt from TD79. I understand that log wavelength binning is a pre-requisite of cross-correlation procedure.</p>
<p>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?</p>
<p>Here is the text as picture:
<a href="https://i.sstatic.net/nz0zN.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nz0zN.png" alt="Tonry and Davis 1979" /></a></p>

CC BY-SA 4.0
2019-07-31T17:11:07.333Z
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
CC BY-SA 4.0
2019-07-31T22:26:15.940Z
@CarlWitthoft there are [228 posts](https://astronomy.stackexchange.com/search?q=python) that mention python here and [46 questions](https://astronomy.stackexchange.com/questions/tagged/python) using the python tag, [16 tagged Astropy](https://astronomy.stackexchange.com/questions/tagged/astropy) and [26 mention Skyfield](https://astronomy.stackexchange.com/search?q=skyfield) and [34 mention PyEphem](https://astronomy.stackexchange.com/search?q=pyephem) . The mentioning of python does not make a question off-topic. *Let's leave it open and get this astronomy question answered!*
uhoh
CC BY-SA 4.0
2019-08-01T02:52:39.127Z
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.
Ingolifs
CC BY-SA 4.0
2019-08-01T02:58:48.273Z
This (unanswered) meta question might be an appropriate place to discuss the relevance of this question. 
https://astronomy.meta.stackexchange.com/questions/513/are-questions-asking-for-help-with-calculations-and-results-on-or-off-topic
Ingolifs
CC BY-SA 4.0
2019-08-01T07:20:11.190Z
@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
<observational-astronomy><redshift><spectra><python>
How to do logarithmic wavelength binning (in Python) in order to find redshift?
Anks_Eagle
# How to do logarithmic wavelength binning (in Python) in order to find redshift?

(Asked by: username_0 on 2019-07-31)

In [Tonry and Davis (1979), p.1513](http://adsabs.harvard.edu/abs/1979AJ.....84.1511T), 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)](https://arxiv.org/abs/1404.2626) 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:
[![Tonry and Davis 1979](https://i.sstatic.net/nz0zN.png)](https://i.sstatic.net/nz0zN.png)

**Tags:** observational-astronomy, redshift, spectra, python
> **Comments:**
> **username_1**: 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.  
> **username_1**: @CarlWitthoft there are [228 posts](https://astronomy.stackexchange.com/search?q=python) that mention python here and [46 questions](https://astronomy.stackexchange.com/questions/tagged/python) using the python tag, [16 tagged Astropy](https://astronomy.stackexchange.com/questions/tagged/astropy) and [26 mention Skyfield](https://astronomy.stackexchange.com/search?q=skyfield) and [34 mention PyEphem](https://astronomy.stackexchange.com/search?q=pyephem) . The mentioning of python does not make a question off-topic. *Let's leave it open and get this astronomy question answered!*  
> **username_2**: 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.  
> **username_2**: This (unanswered) meta question might be an appropriate place to discuss the relevance of this question.  https://astronomy.meta.stackexchange.com/questions/513/are-questions-asking-for-help-with-calculations-and-results-on-or-off-topic  
> **username_1**: @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.  

---

## 2 answers

### Answer by username_3 on 2021-02-24. Score: 2

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.

> **Comments:**
> **username_4**: Welcome to astronomy SE! I slightly edited your answer, hope you are ok with the changes.  

---

### Answer by username_5 on 2023-02-08. Score: 1

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](https://specutils.readthedocs.io/en/stable/api/specutils.analysis.template_logwl_resample.html#specutils.analysis.template_logwl_resample) 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).


---

