Repository navigation
Expand file tree
/
Copy pathfrft2d.py
More file actions
137 lines (118 loc) · 3.35 KB
/
Copy pathfrft2d.py
File metadata and controls
137 lines (118 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
Module to calculate the fast 2D fractional fourier transform.
"""
from __future__ import division
import numpy
import scipy
import scipy.signal
def frft2d(matrix, a = [0.7, 0.7]):
"""
Calculate the 2D fast fractional fourier transform.
Parameters
----------
matrix : 2D numpy array
The signal to be transformed.
a : list / array
fractional powers
Returns
-------
data : numpy array
The transformed 2D signal.
"""
temp = numpy.zeros((matrix.shape[0], matrix.shape[1]), dtype=numpy.complex)
for k in range(0, matrix.shape[0]):
temp[k, :] = frft(matrix[k, :], a[0])
out = numpy.zeros((temp.shape[0], temp.shape[1]), dtype=numpy.complex)
for m in range(0, temp.shape[1]):
out[:, m] = frft(temp[:, m], a[1])
return out
def frft(f, a):
"""
Calculate the fast fractional fourier transform.
Parameters
----------
f : numpy array
The signal to be transformed.
a : float
fractional power
Returns
-------
data : numpy array
The transformed signal.
References
---------
.. [1] This algorithm implements `frft.m` from
https://nalag.cs.kuleuven.be/research/software/FRFT/
"""
ret = numpy.zeros_like(f, dtype=numpy.complex)
f = f.copy().astype(numpy.complex)
N = len(f)
shft = numpy.fmod(numpy.arange(N) + numpy.fix(N / 2), N).astype(int)
sN = numpy.sqrt(N)
a = numpy.remainder(a, 4.0)
# Special cases
if a == 0.0:
return f
if a == 2.0:
return numpy.flipud(f)
if a == 1.0:
ret[shft] = numpy.fft.fft(f[shft]) / sN
return ret
if a == 3.0:
ret[shft] = numpy.fft.ifft(f[shft]) * sN
return ret
# reduce to interval 0.5 < a < 1.5
if a > 2.0:
a = a - 2.0
f = numpy.flipud(f)
if a > 1.5:
a = a - 1
f[shft] = numpy.fft.fft(f[shft]) / sN
if a < 0.5:
a = a + 1
f[shft] = numpy.fft.ifft(f[shft]) * sN
# the general case for 0.5 < a < 1.5
alpha = a * numpy.pi / 2
tana2 = numpy.tan(alpha / 2)
sina = numpy.sin(alpha)
f = numpy.hstack((numpy.zeros(N - 1), sincinterp(f), numpy.zeros(N - 1))).T
# chirp premultiplication
chrp = numpy.exp(-1j * numpy.pi / N * tana2 / 4 *
numpy.arange(-2 * N + 2, 2 * N - 1).T ** 2)
f = chrp * f
# chirp convolution
c = numpy.pi / N / sina / 4
ret = scipy.signal.fftconvolve(
numpy.exp(1j * c * numpy.arange(-(4 * N - 4), 4 * N - 3).T ** 2),
f
)
ret = ret[4 * N - 4:8 * N - 7] * numpy.sqrt(c / numpy.pi)
# chirp post multiplication
ret = chrp * ret
# normalizing constant
ret = numpy.exp(-1j * (1 - a) * numpy.pi / 4) * ret[N - 1:-N + 1:2]
return ret
def ifrft(f, a):
"""
Calculate the inverse fast fractional fourier transform.
Parameters
----------
f : numpy array
The signal to be transformed.
a : float
fractional power
Returns
-------
data : numpy array
The transformed signal.
"""
return frft(f, -a)
def sincinterp(x):
N = len(x)
y = numpy.zeros(2 * N - 1, dtype=x.dtype)
y[:2 * N:2] = x
xint = scipy.signal.fftconvolve(
y[:2 * N],
numpy.sinc(numpy.arange(-(2 * N - 3), (2 * N - 2)).T / 2),
)
return xint[2 * N - 3: -2 * N + 3]