Skip to content

Commit

Permalink
incoroprate previous hexagonal ifftshift into filter_shift()
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-langfield committed May 21, 2024
1 parent 10ec961 commit 0c4e97a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/hexfft/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .hexfft import fftshift, ifftshift, fft, ifft, FFT
from .hexfft import fft, ifft, FFT
from .array import HexArray
73 changes: 0 additions & 73 deletions src/hexfft/hexfft.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,79 +503,6 @@ def mersereau_ifft(PX):
return px.T / 4


def fftshift(X):
N = X.shape[0]
n1, n2 = X.indices
m = hsupport(N, X.pattern).astype(bool)
shifted = HexArray(np.zeros_like(X), X.pattern)
if X.pattern == "oblique":
regI = (n1 < N // 2) & (n2 < N // 2)
regII = m & (n1 < n2) & (n2 >= N // 2)
regIII = m & (n2 <= n1) & (n1 >= N // 2)

_regI = (n1 >= N // 2) & (n2 >= N // 2)
_regII = m & (n1 >= n2) & (n2 < N // 2)
_regIII = m & (n2 > n1) & (n1 < N // 2)

shifted[_regI] = X[regI]
shifted[_regII] = X[regII]
shifted[_regIII] = X[regIII]

elif X.pattern == "offset":
m = m.T
n2 = n2 - N // 4
regI = m & (n1 < N // 2) & (n2 < N // 2)
regII = m & (n1 <= n2) & (n2 >= N // 2)
regIII = m & (n2 < n1) & (n1 >= N // 2)

_regI = m & (n1 >= N // 2) & (n2 >= N // 2)
_regII = m & (n1 > n2) & (n2 < N // 2)
_regIII = m & (n2 >= n1) & (n1 < N // 2)

shifted[_regI.T] = X[regI.T]
shifted[_regII.T] = X[regII.T]
shifted[_regIII.T] = X[regIII.T]

return shifted


def ifftshift(X):
N = X.shape[0]
n1, n2 = X.indices
m = hsupport(N, X.pattern).astype(bool)
shifted = HexArray(np.zeros_like(X), X.pattern)

if X.pattern == "oblique":
_regI = (n1 < N // 2) & (n2 < N // 2)
_regII = m & (n1 < n2) & (n2 >= N // 2)
_regIII = m & (n2 <= n1) & (n1 >= N // 2)

regI = (n1 >= N // 2) & (n2 >= N // 2)
regII = m & (n1 >= n2) & (n2 < N // 2)
regIII = m & (n2 > n1) & (n1 < N // 2)

shifted[_regI] = X[regI]
shifted[_regII] = X[regII]
shifted[_regIII] = X[regIII]

elif X.pattern == "offset":
m = m.T
n2 = n2 - N // 4
regI = m & (n1 < N // 2) & (n2 < N // 2)
regII = m & (n1 <= n2) & (n2 >= N // 2)
regIII = m & (n2 < n1) & (n1 >= N // 2)

_regI = m & (n1 >= N // 2) & (n2 >= N // 2)
_regII = m & (n1 > n2) & (n2 < N // 2)
_regIII = m & (n2 >= n1) & (n1 < N // 2)

shifted[regI.T] = X[_regI.T]
shifted[regII.T] = X[_regII.T]
shifted[regIII.T] = X[_regIII.T]

return shifted


def _hexdft_pgram(px):
""""""
dtype = px.dtype
Expand Down
82 changes: 78 additions & 4 deletions src/hexfft/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,88 @@ def pgram_to_hex(p, N, pattern="oblique"):
return HexArray(h, pattern)


def filter_shift(x):
def fftshift(X):
N = X.shape[0]
n1, n2 = X.indices
m = hsupport(N, X.pattern).astype(bool)
shifted = HexArray(np.zeros_like(X), X.pattern)
if X.pattern == "oblique":
regI = (n1 < N // 2) & (n2 < N // 2)
regII = m & (n1 < n2) & (n2 >= N // 2)
regIII = m & (n2 <= n1) & (n1 >= N // 2)

_regI = (n1 >= N // 2) & (n2 >= N // 2)
_regII = m & (n1 >= n2) & (n2 < N // 2)
_regIII = m & (n2 > n1) & (n1 < N // 2)

shifted[_regI] = X[regI]
shifted[_regII] = X[regII]
shifted[_regIII] = X[regIII]

elif X.pattern == "offset":
m = m.T
n2 = n2 - N // 4
regI = m & (n1 < N // 2) & (n2 < N // 2)
regII = m & (n1 <= n2) & (n2 >= N // 2)
regIII = m & (n2 < n1) & (n1 >= N // 2)

_regI = m & (n1 >= N // 2) & (n2 >= N // 2)
_regII = m & (n1 > n2) & (n2 < N // 2)
_regIII = m & (n2 >= n1) & (n1 < N // 2)

shifted[_regI.T] = X[regI.T]
shifted[_regII.T] = X[regII.T]
shifted[_regIII.T] = X[regIII.T]

return shifted


def ifftshift(X):
N = X.shape[0]
n1, n2 = X.indices
m = hsupport(N, X.pattern).astype(bool)
shifted = HexArray(np.zeros_like(X), X.pattern)

if X.pattern == "oblique":
_regI = (n1 < N // 2) & (n2 < N // 2)
_regII = m & (n1 < n2) & (n2 >= N // 2)
_regIII = m & (n2 <= n1) & (n1 >= N // 2)

regI = (n1 >= N // 2) & (n2 >= N // 2)
regII = m & (n1 >= n2) & (n2 < N // 2)
regIII = m & (n2 > n1) & (n1 < N // 2)

shifted[_regI] = X[regI]
shifted[_regII] = X[regII]
shifted[_regIII] = X[regIII]

elif X.pattern == "offset":
m = m.T
n2 = n2 - N // 4
regI = m & (n1 < N // 2) & (n2 < N // 2)
regII = m & (n1 <= n2) & (n2 >= N // 2)
regIII = m & (n2 < n1) & (n1 >= N // 2)

_regI = m & (n1 >= N // 2) & (n2 >= N // 2)
_regII = m & (n1 > n2) & (n2 < N // 2)
_regIII = m & (n2 >= n1) & (n1 < N // 2)

shifted[regI.T] = X[_regI.T]
shifted[regII.T] = X[_regII.T]
shifted[regIII.T] = X[_regIII.T]

return shifted


def filter_shift(x, periodicity="rect"):
"""
Shift the quadrants of a HexArray to move the origin to/from
Shift the quadrants/thirds of a HexArray to move the origin to/from
the center of the grid. Useful if a filter kernel is easier to define
the origin at the center of the grid.
"""
if not isinstance(x, HexArray):
x = HexArray(x)

if periodicity == "hex":
return ifftshift(x)

N1, N2 = x.shape
out = HexArray(np.zeros_like(x), x.pattern)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hexfft import HexArray, fft, ifft, FFT, fftshift, ifftshift
from hexfft import HexArray, fft, ifft, FFT
from hexfft.array import rect_shift, rect_unshift
import numpy as np
import pytest
Expand Down
7 changes: 3 additions & 4 deletions tests/test_hexfft.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hexfft import fftshift, ifftshift, HexArray
from hexfft import fft, ifft, FFT, HexArray
from hexfft.hexfft import (
_hexdft_pgram,
_hexidft_pgram,
Expand All @@ -8,15 +8,14 @@
mersereau_ifft,
rect_fft,
rect_ifft,
FFT,
fft,
ifft,
)
from hexfft.utils import (
hsupport,
pgram_to_hex,
nice_test_function,
hex_to_pgram,
fftshift,
ifftshift,
)
from hexfft.array import rect_shift, rect_unshift
from hexfft.reference import (
Expand Down

0 comments on commit 0c4e97a

Please sign in to comment.