-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft.py
130 lines (116 loc) · 4.99 KB
/
fft.py
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
#
# Free FFT and convolution (Python)
#
# Copyright (c) 2014 Project Nayuki
# https://www.nayuki.io/page/free-small-fft-in-multiple-languages
#
# (MIT License)
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# - The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# - The Software is provided "as is", without warranty of any kind, express or
# implied, including but not limited to the warranties of merchantability,
# fitness for a particular purpose and noninfringement. In no event shall the
# authors or copyright holders be liable for any claim, damages or other
# liability, whether in an action of contract, tort or otherwise, arising from,
# out of or in connection with the Software or the use or other dealings in the
# Software.
#
import cmath
import sys
if sys.version_info.major == 2:
range = xrange
#
# Computes the discrete Fourier transform (DFT) of the given complex vector, returning the result as a new vector.
# Set 'inverse' to True if computing the inverse transform. This DFT does not perform scaling, so the inverse is not a true inverse.
# The vector can have any length. This is a wrapper function.
#
def transform(vector, inverse=False):
n = len(vector)
if n > 0 and n & (n - 1) == 0: # Is power of 2
return transform_radix2(vector, inverse)
else: # More complicated algorithm for arbitrary sizes
return transform_bluestein(vector, inverse)
#
# Computes the discrete Fourier transform (DFT) of the given complex vector, returning the result as a new vector.
# The vector's length must be a power of 2. Uses the Cooley-Tukey decimation-in-time radix-2 algorithm.
#
def transform_radix2(vector, inverse):
# Returns the integer whose value is the reverse of the lowest 'bits' bits of the integer 'x'.
def reverse(x, bits):
y = 0
for i in range(bits):
y = (y << 1) | (x & 1)
x >>= 1
return y
# Initialization
n = len(vector)
levels = 0
while True:
if 1 << levels == n:
break
elif 1 << levels > n:
raise ValueError("Length is not a power of 2")
else:
levels += 1
# Now, levels = log2(n)
exptable = [cmath.exp((2j if inverse else -2j) * cmath.pi * i / n) for i in range(n // 2)]
vector = [vector[reverse(i, levels)] for i in range(n)] # Copy with bit-reversed permutation
# Radix-2 decimation-in-time FFT
size = 2
while size <= n:
halfsize = size // 2
tablestep = n // size
for i in range(0, n, size):
k = 0
for j in range(i, i + halfsize):
temp = vector[j + halfsize] * exptable[k]
vector[j + halfsize] = vector[j] - temp
vector[j] += temp
k += tablestep
size *= 2
return vector
#
# Computes the discrete Fourier transform (DFT) of the given complex vector, returning the result as a new vector.
# The vector can have any length. This requires the convolution function, which in turn requires the radix-2 FFT function.
# Uses Bluestein's chirp z-transform algorithm.
#
def transform_bluestein(vector, inverse):
# Find a power-of-2 convolution length m such that m >= n * 2 + 1
n = len(vector)
m = 1
while m < n * 2 + 1:
m *= 2
exptable = [cmath.exp((1j if inverse else -1j) * cmath.pi * (i * i % (n * 2)) / n) for i in range(n)] # Trigonometric table
a = [x * y for (x, y) in zip(vector, exptable)] + [0] * (m - n) # Temporary vectors and preprocessing
b = [(exptable[min(i, m - i)].conjugate() if (i < n or m - i < n) else 0) for i in range(m)]
c = convolve(a, b, False)[:n] # Convolution
for i in range(n): # Postprocessing
c[i] *= exptable[i]
return c
#
# Computes the circular convolution of the given real or complex vectors, returning the result as a new vector. Each vector's length must be the same.
# realoutput=True: Extract the real part of the convolution, so that the output is a list of floats. This is useful if both inputs are real.
# realoutput=False: The output is always a list of complex numbers (even if both inputs are real).
#
def convolve(x, y, realoutput=True):
assert len(x) == len(y)
n = len(x)
x = transform(x)
y = transform(y)
for i in range(n):
x[i] *= y[i]
x = transform(x, inverse=True)
# Scaling (because this FFT implementation omits it) and postprocessing
if realoutput:
for i in range(n):
x[i] = x[i].real / n
else:
for i in range(n):
x[i] /= n
return x