def box_smooth(self, nbox, preserve=False, **kwargs): """ Box car smooth the spectrum Parameters ---------- nbox: int Number of pixels to smooth over preserve: bool (False) If True, perform a convolution to ensure the new spectrum has the same number of pixels as the original. **kwargs: dict If preserve=True, these keywords are passed on to astropy.convoution.convolve Returns ------- A new XSpectrum1D instance of the smoothed spectrum """ if preserve: from astropy.convolution import convolve, Box1DKernel new_fx = convolve(self.flux, Box1DKernel(nbox), **kwargs) new_sig = convolve(self.sig, Box1DKernel(nbox), **kwargs) new_wv = self.wavelength else: # Truncate arrays as need be npix = len(self.flux) try: new_npix = npix // nbox # New division except ZeroDivisionError: raise ZeroDivisionError('Dividing by zero..') orig_pix = np.arange(new_npix * nbox) # Rebin (mean) new_wv = liu.scipy_rebin(self.wavelength[orig_pix], new_npix) new_fx = liu.scipy_rebin(self.flux[orig_pix], new_npix) if self.sig_is_set: new_sig = liu.scipy_rebin( self.sig[orig_pix], new_npix) / np.sqrt(nbox) else: new_sig = None # Return return XSpectrum1D.from_tuple( (new_wv, new_fx, new_sig), meta=self.meta.copy())
def box_smooth(self, nbox, preserve=False): """ Box car smooth spectrum and return a new one Is a simple wrapper to the rebin routine Parameters ---------- nbox: integer Number of pixels to smooth over preserve: bool (False) Keep the new spectrum at the same number of pixels as original Returns: -------- XSpectrum1D of the smoothed spectrum """ if preserve: from astropy.convolution import convolve, Box1DKernel new_fx = convolve(self.flux, Box1DKernel(nbox)) new_sig = convolve(self.sig, Box1DKernel(nbox)) new_wv = self.dispersion else: # Truncate arrays as need be npix = len(self.flux) try: new_npix = npix // nbox # New division except ZeroDivisionError: raise ZeroDivisionError("Dividing by zero..") orig_pix = np.arange(new_npix * nbox) # Rebin (mean) new_wv = liu.scipy_rebin(self.dispersion[orig_pix], new_npix) new_fx = liu.scipy_rebin(self.flux[orig_pix], new_npix) new_sig = liu.scipy_rebin(self.sig[orig_pix], new_npix) / np.sqrt(nbox) # Return return XSpectrum1D.from_array( new_wv, new_fx, meta=self.meta.copy(), uncertainty=apy.nddata.StdDevUncertainty(new_sig) )
def box_smooth(self, nbox, preserve=False): """ Box car smooth the spectrum Parameters ---------- nbox: integer Number of pixels to smooth over preserve: bool (False) If True, perform a convolution to ensure the new spectrum has the same number of pixels as the original. Returns ------- A new XSpectrum1D instance of the smoothed spectrum """ if preserve: from astropy.convolution import convolve, Box1DKernel new_fx = convolve(self.flux, Box1DKernel(nbox)) new_sig = convolve(self.sig, Box1DKernel(nbox)) new_wv = self.wavelength else: # Truncate arrays as need be npix = len(self.flux) try: new_npix = npix // nbox # New division except ZeroDivisionError: raise ZeroDivisionError('Dividing by zero..') orig_pix = np.arange(new_npix * nbox) # Rebin (mean) new_wv = liu.scipy_rebin(self.wavelength[orig_pix], new_npix) new_fx = liu.scipy_rebin(self.flux[orig_pix], new_npix) new_sig = liu.scipy_rebin( self.sig[orig_pix], new_npix) / np.sqrt(nbox) # Return return XSpectrum1D.from_array( new_wv, new_fx, meta=self.meta.copy(), uncertainty=apy.nddata.StdDevUncertainty(new_sig))