Skip to content
Snippets Groups Projects
Commit 32b85405 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

use Hartley transform

parent 5c0c1525
Branches
No related tags found
No related merge requests found
Pipeline #
...@@ -38,21 +38,41 @@ class RGRGTransformation(Transformation): ...@@ -38,21 +38,41 @@ class RGRGTransformation(Transformation):
import pyfftw import pyfftw
super(RGRGTransformation, self).__init__(domain, codomain) super(RGRGTransformation, self).__init__(domain, codomain)
pyfftw.interfaces.cache.enable() pyfftw.interfaces.cache.enable()
self._fwd = self.codomain.harmonic
@property @property
def unitary(self): def unitary(self):
return True return True
def _transform_helper(self, val, axes): @staticmethod
from pyfftw.interfaces.numpy_fft import fftn, ifftn def _hartley(a, axes=None):
# Check if the axes provided are valid given the shape # Check if the axes provided are valid given the shape
if axes is not None and \ if axes is not None and \
not all(axis in range(len(val.shape)) for axis in axes): not all(axis in range(len(val.shape)) for axis in axes):
raise ValueError("Provided axes does not match array shape") raise ValueError("Provided axes does not match array shape")
return fftn(val, axes=axes) if self._fwd else ifftn(val, axes=axes) from pyfftw.interfaces.numpy_fft import rfftn
if issubclass(a.dtype.type,np.complexfloating):
raise TypeError ("Hartley tansform works for real-valued arrays only.")
tmp = rfftn(a,axes=axes)
res = np.empty_like(a)
if axes is None:
axes = list(range(a.ndim))
lastaxis = axes[-1]
nlast = a.shape[lastaxis]
ntmplast = tmp.shape[lastaxis]
nrem = nlast - ntmplast
slice1 = [slice(None)]*a.ndim
slice1[lastaxis]=slice(0,ntmplast)
res[slice1] = tmp.real+tmp.imag
tmp = np.roll(tmp,-1,axes)
slice1 = [slice(None)]*a.ndim
slice1[lastaxis]=slice(ntmplast,None)
slice2 = [slice(None)]*a.ndim
for i in axes:
slice2[i] = slice(None,None,-1)
slice2[lastaxis]=slice(nrem-1,None,-1)
res[slice1] = tmp[slice2].real-tmp[slice2].imag
return res
def transform(self, val, axes=None): def transform(self, val, axes=None):
""" """
...@@ -75,26 +95,14 @@ class RGRGTransformation(Transformation): ...@@ -75,26 +95,14 @@ class RGRGTransformation(Transformation):
if self.codomain.harmonic: if self.codomain.harmonic:
fct = self.domain.weight() fct = self.domain.weight()
else: else:
fct = 1./self.codomain.weight() fct = 1./(self.codomain.weight()*self.domain.dim)
# Perform the transformation # Perform the transformation
if issubclass(val.dtype.type, np.complexfloating): if issubclass(val.dtype.type, np.complexfloating):
Tval1 = self._transform_helper(val.real, axes) Tval = self._hartley(val.real, axes) \
Tval2 = self._transform_helper(val.imag, axes) +1j*self._hartley(val.imag, axes)
if self.codomain.harmonic:
Tval1.real += Tval1.imag
Tval2.real += Tval2.imag
else:
Tval1.real -= Tval1.imag
Tval2.real -= Tval2.imag
Tval1.imag = Tval2.real
Tval = Tval1
else: else:
Tval = self._transform_helper(val, axes) Tval = self._hartley(val, axes)
if self.codomain.harmonic:
Tval = Tval.real + Tval.imag
else:
Tval = Tval.real - Tval.imag
Tval *= fct Tval *= fct
return Tval return Tval
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment