<p>There are two ways (at least).</p>
<ol>
<li><p>Do a pixel by pixel cross-correlation of the galaxy and template spectrum. The cross-correlation function will have a broad peak, and the position of the peak is the &quot;lag&quot; that gives you the radial velocity difference between the template and the galaxy. The lag will be in pixels, but since you have (correctly) got your data in equal bins of log wavelength, then each pixel will correspond to a fixed radial velocity increment (see <a href="https://astronomy.stackexchange.com/a/14524/2531">https://astronomy.stackexchange.com/a/14524/2531</a> ). You appear in any case to be heading towards using the Tonry &amp; Davis FFT cross-correlation route (multiplying the Fourier Transforms), which would give a similar cross-correlation function.</p>
</li>
<li><p>Direct fitting of the spectrum. Broaden and shift the template spectrum until it gives you the best match with the galaxy spectrum. The free parameters of your model will be the amount of broadening, possibly a relative normalisation and a (velocity) shift. The latter is the parameter you are looking for, so use its best-fit value. There are lots of ways to achieve the fitting in Python, from classic least squares fitting to more sophisticated Bayesian and Monte Carlo Markov Chain methods.</p>
</li>
</ol>

CC BY-SA 4.0
2023-02-08T07:42:15.637Z
2023-02-19T08:16:24.730Z
2023-02-19T08:16:24.730Z
ProfRob
<p>Hello I extracted a spectrum from the central region of the 2D spectrum of the major axis from NGC 4697 (I just took a central row in the 2D data set, see this question for more information: <a href="https://astronomy.stackexchange.com/questions/44725/how-to-extract-galaxy-spectra-for-different-radii-in-python-for-spectra-taken-by">How to extract galaxy spectra for different radii in Python for spectra taken by long slit spectrograph?</a>). I also have got a template spectrum from the K2-III star hd132345. Here both spectras are plotted:
<a href="https://i.sstatic.net/Afedw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Afedw.png" alt="enter image description here" /></a>
One can clearly see that the absorption lines in the galaxy spectrum have been broadened, due to the superposition of many stellar spectra, each one having a individual velocity along the line of sight. I now have done some preprocessing steps to keep on working with the spectra in fourier space later. These steps included: cutting off pixels at both ends of the spectrum, removing the continuum, tapering both spectra with a window function using np.blackman(). The result can be seen here:
<a href="https://i.sstatic.net/sMzTQ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/sMzTQ.png" alt="enter image description here" /></a>
To further work with the spectra I want to remove/measure the redshift from the galaxy, to use them later in the FCQ algorithm to determine the velocity distribution along the line of sight and therefore the kinematics of NGC 4697. One can clearly see the 3 dominant lines in the template spectrum arround the x-value of 8.55, these lines should be the Magnesium triplet, which has its wavelengths at round about 5200 Angstroems ( note that the spectra are logarithmically rebinned). I wanted to use these lines to determine the redshift of the galaxy. The triplet in the galaxy has involved into a doublet, due to the broadening. I did set up vertical lines at the peaks of the absorption lines and calculated the shift graphically. One can clearly see that the galaxy absorption lines have been shifted towards the red end of the spectrum. I wanted to know if there is a more elegant/common way to tackle this problem, since it is probably one of the most basic things to do in Astronomy. I just could not find a sophisticated answer for a Python solution in the internet and did not want to use a prewritten funciton, for educational purposes. My goal is it to not have to look for the x values of the peaks manually, to make the problem easier when you are working with more spectra. The spectra in the MG-Triplet region with redshift look as follows.<a href="https://i.sstatic.net/o0Oa3.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/o0Oa3.png" alt="enter image description here" /></a> And after I subtracted the redshift. <a href="https://i.sstatic.net/1cDhc.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1cDhc.png" alt="enter image description here" /></a>. Here I just calculated: loglam_gal = loglam_gal - (8.55702-8.55323). Is there a better way to do this and did I even do it correct ? Both spectra after removing the redshift from the galaxy look as follows:
<a href="https://i.sstatic.net/nDlAT.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nDlAT.png" alt="enter image description here" /></a>
I have calculated the redshift from my graphically measured wavelengths to be: z = 0.003797. In Wikipedia it says z= 0.004140 for NGC 4697.</p>

CC BY-SA 4.0
2021-09-07T05:00:11.557Z
Not an astronomer, but would minimizing the root mean squares of the number series work?
nick012000
<observational-astronomy><galaxy><redshift><spectra><python>
How to remove redshift from galaxy spectra in Python?
trynerror
# How to remove redshift from galaxy spectra in Python?

(Asked by: username_0 on 2021-09-05)

Hello I extracted a spectrum from the central region of the 2D spectrum of the major axis from NGC 4697 (I just took a central row in the 2D data set, see this question for more information: [How to extract galaxy spectra for different radii in Python for spectra taken by long slit spectrograph?](https://astronomy.stackexchange.com/questions/44725/how-to-extract-galaxy-spectra-for-different-radii-in-python-for-spectra-taken-by)). I also have got a template spectrum from the K2-III star hd132345. Here both spectras are plotted:
[![enter image description here](https://i.sstatic.net/Afedw.png)](https://i.sstatic.net/Afedw.png)
One can clearly see that the absorption lines in the galaxy spectrum have been broadened, due to the superposition of many stellar spectra, each one having a individual velocity along the line of sight. I now have done some preprocessing steps to keep on working with the spectra in fourier space later. These steps included: cutting off pixels at both ends of the spectrum, removing the continuum, tapering both spectra with a window function using np.blackman(). The result can be seen here:
[![enter image description here](https://i.sstatic.net/sMzTQ.png)](https://i.sstatic.net/sMzTQ.png)
To further work with the spectra I want to remove/measure the redshift from the galaxy, to use them later in the FCQ algorithm to determine the velocity distribution along the line of sight and therefore the kinematics of NGC 4697. One can clearly see the 3 dominant lines in the template spectrum arround the x-value of 8.55, these lines should be the Magnesium triplet, which has its wavelengths at round about 5200 Angstroems ( note that the spectra are logarithmically rebinned). I wanted to use these lines to determine the redshift of the galaxy. The triplet in the galaxy has involved into a doublet, due to the broadening. I did set up vertical lines at the peaks of the absorption lines and calculated the shift graphically. One can clearly see that the galaxy absorption lines have been shifted towards the red end of the spectrum. I wanted to know if there is a more elegant/common way to tackle this problem, since it is probably one of the most basic things to do in Astronomy. I just could not find a sophisticated answer for a Python solution in the internet and did not want to use a prewritten funciton, for educational purposes. My goal is it to not have to look for the x values of the peaks manually, to make the problem easier when you are working with more spectra. The spectra in the MG-Triplet region with redshift look as follows.[![enter image description here](https://i.sstatic.net/o0Oa3.png)](https://i.sstatic.net/o0Oa3.png) And after I subtracted the redshift. [![enter image description here](https://i.sstatic.net/1cDhc.png)](https://i.sstatic.net/1cDhc.png). Here I just calculated: loglam\_gal = loglam\_gal - (8.55702-8.55323). Is there a better way to do this and did I even do it correct ? Both spectra after removing the redshift from the galaxy look as follows:
[![enter image description here](https://i.sstatic.net/nDlAT.png)](https://i.sstatic.net/nDlAT.png)
I have calculated the redshift from my graphically measured wavelengths to be: z = 0.003797. In Wikipedia it says z= 0.004140 for NGC 4697.

**Tags:** observational-astronomy, galaxy, redshift, spectra, python
> **Comments:**
> **username_1**: Not an astronomer, but would minimizing the root mean squares of the number series work?  

---

## 1 answers

### Answer by username_2 on 2023-02-08. Score: 1 | Accepted answer

There are two ways (at least).

1. Do a pixel by pixel cross-correlation of the galaxy and template spectrum. The cross-correlation function will have a broad peak, and the position of the peak is the "lag" that gives you the radial velocity difference between the template and the galaxy. The lag will be in pixels, but since you have (correctly) got your data in equal bins of log wavelength, then each pixel will correspond to a fixed radial velocity increment (see <https://astronomy.stackexchange.com/a/14524/2531> ). You appear in any case to be heading towards using the Tonry & Davis FFT cross-correlation route (multiplying the Fourier Transforms), which would give a similar cross-correlation function.
2. Direct fitting of the spectrum. Broaden and shift the template spectrum until it gives you the best match with the galaxy spectrum. The free parameters of your model will be the amount of broadening, possibly a relative normalisation and a (velocity) shift. The latter is the parameter you are looking for, so use its best-fit value. There are lots of ways to achieve the fitting in Python, from classic least squares fitting to more sophisticated Bayesian and Monte Carlo Markov Chain methods.


---

