-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft.py
272 lines (228 loc) · 8.92 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import sys
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
import math
import time
def set_arguments(args):
parameters = {
'mode': 1,
'image' : 'moonlanding.png'
}
i = 1
while i < len(args):
if args[i] == '-m':
if i+1 < len(args) and args[i+1].isdigit():
parameters['mode'] = int(args[i+1])
i += 2
else:
print(f"ERROR\tIncorrect input syntax: expected integer after argument {args[i]}")
return None
elif args[i] == '-i':
if i+1 < len(args):
parameters['image'] = args[i+1]
i += 2
else:
print(f"ERROR\tIncorrect input syntax: expected image after argument {args[i]}")
return None
else:
print(f"ERROR\tIncorrect input syntax: unknown argument {args[i]}")
return None
return parameters
"""
GOAL OUTPUT:
"""
def fft_image(image):
f = np.fft.fft2(image)
fshift = np.fft.fftshift(f)
magnitude_spectrum = np.log(np.abs(fshift)+ 1)
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks(), plt.yticks()
plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray', norm=LogNorm(vmin=1))
plt.title('Magnitude Spectrum'), plt.xticks(), plt.yticks()
plt.colorbar()
plt.show()
def npfft_image(image):
f = np.fft.fft2(image)
fft_magnitude = np.log(np.abs(f) + 1)
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(fft_magnitude, cmap = 'gray', norm=LogNorm(vmin=1))
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])
plt.colorbar()
plt.show()
"""
OUR CODE:
"""
def dft(signal):
N = len(signal)
power = math.ceil(np.log2(N))
N_final = 2 ** power
signal = np.append(signal, np.zeros(N_final - N))
N = N_final
dft_result = np.zeros(N, dtype=complex)
for k in range(N):
result = 0
for n in range(N):
angle = -1j * 2 * np.pi * k * n / N
result += signal[n] * np.exp(angle)
dft_result[k] = result
return dft_result
def inverse_dft(signal):
N = len(signal)
power = math.ceil(np.log2(N))
N_final = 2 ** power
signal = np.append(signal, np.zeros(N_final - N))
N = N_final
inverse_dft_result = np.zeros(N, dtype=complex)
for n in range(N):
result = 0
for k in range(N):
angle = 1j * 2 * np.pi * k * n / N
result += signal[k] * np.exp(angle)
inverse_dft_result[n] = result / N
return inverse_dft_result
def fft(x):
N = len(x)
power = math.ceil(np.log2(N))
N_final = 2 ** power
x = np.append(x, np.zeros(N_final - N))
N = N_final
if N <= 1:
return x
even = fft(x[::2])
odd = fft(x[1::2])
factor = [np.exp(-2j * np.pi * i / N) for i in range(N)]
result_1 = [even[i] + factor[i] * odd[i] for i in range(N//2)]
result_2 = [even[i] + factor[i+N//2] * odd[i] for i in range(N//2)]
return np.concatenate([result_1, result_2])
def inverse_fft(x):
N = len(x)
if N <= 1:
return x
even = inverse_fft(x[::2])
odd = inverse_fft(x[1::2])
factor = [np.exp(2j * np.pi * i / N) for i in range(N)]
result_1 = [(even[i] + factor[i] * odd[i])/2 for i in range(N//2)]
result_2 = [(even[i] + factor[i+N//2] * odd[i])/2 for i in range(N//2)]
return np.concatenate([result_1, result_2])
def twod_fft(image):
fft_row = np.array([fft(row) for row in image])
fft_col = np.transpose(np.array([fft(col) for col in np.transpose(fft_row)]))
return fft_col
def twod_dft(image):
dft_row = np.array([dft(row) for row in image])
dft_col = np.transpose(np.array([dft(col) for col in np.transpose(dft_row)]))
return dft_col
def inverse_2d_fft(image):
fft_row = np.array([inverse_fft(row) for row in image])
fft_col = np.transpose(np.array([inverse_fft(col) for col in np.transpose(fft_row)]))
return fft_col
def denoise_image(image, cutoff_x, cutoff_y):
fft_result = twod_fft(image)
rows, cols = fft_result.shape
for i in range(rows):
for j in range(cols):
if i > cutoff_x or j > cutoff_y:
fft_result[i, j] = 0
denoised = np.abs(inverse_2d_fft(fft_result))
non_zero_coefficients = cutoff_x * cutoff_y
total_coefficients = rows * cols
fraction = non_zero_coefficients / total_coefficients
print(f"Number of non-zero Fourier coefficients: {non_zero_coefficients}")
print(f"Fraction of original Fourier coefficients: {fraction:.4f}")
plt.subplot(121),plt.imshow(image, cmap = 'gray')
plt.title('Input Image'), plt.xticks(), plt.yticks()
plt.subplot(122),plt.imshow(denoised, cmap = 'gray')
plt.title('Denoised Image'), plt.xticks(), plt.yticks()
plt.show()
def compress_image(image, compression_percent):
fft_result = twod_fft(image)
magnitude = np.abs(fft_result)
threshold = np.percentile(magnitude, compression_percent)
number_zeroed = 0
for i in range(magnitude.shape[0]):
for j in range(magnitude.shape[1]):
if magnitude[i, j] < threshold:
fft_result[i, j] = 0
number_zeroed += 1
total_coefficients = magnitude.shape[0] * magnitude.shape[1]
original_coefficients = total_coefficients - number_zeroed
print(f"Compression Level: {compression_percent}")
print(f"Number of zeroed Fourier coefficients: {number_zeroed}")
print(f"Number of original Fourier coefficients: {original_coefficients}")
compressed = np.abs(inverse_2d_fft(fft_result))
return compressed, fft_result
def process_image(img):
f = twod_fft(img)
fft_magnitude = np.log(np.abs(f) + 1)
plt.subplot(121),plt.imshow(img, cmap = 'gray')
plt.title('Input Image'), plt.xticks(), plt.yticks()
plt.subplot(122),plt.imshow(fft_magnitude, cmap = 'gray', norm=LogNorm(vmin=1))
plt.title('Magnitude Spectrum'), plt.xticks(), plt.yticks()
plt.colorbar()
plt.show()
def measure_runtime(method, image):
runtimes = []
for _ in range(10): # 10 trials
start_time = time.time()
method(image)
runtimes.append(time.time() - start_time)
return np.mean(runtimes), np.std(runtimes)
def plot():
sizes = [2**i for i in range(5, 9)] # my laptop could only handle up to 2^8
mean_runtimes_naive = []
std_runtimes_naive = []
mean_runtimes_fft = []
std_runtimes_fft = []
for size in sizes:
image = np.random.rand(size, size).astype(np.float32)
mean_naive, std_naive = measure_runtime(twod_dft, image)
mean_runtimes_naive.append(mean_naive)
std_runtimes_naive.append(std_naive)
mean_fft, std_fft = measure_runtime(twod_fft, image)
mean_runtimes_fft.append(mean_fft)
std_runtimes_fft.append(std_fft)
print(f"Size: {size}, Naive Method: {mean_naive:.4f}s +/- {std_naive:.4f}s, FFT Method: {mean_fft:.4f}s +/- {std_fft:.4f}s")
# Error bars (97% confidence interval, 2 * std deviation)
error_bars_naive = [2 * std for std in std_runtimes_naive]
error_bars_fft = [2 * std for std in std_runtimes_fft]
plt.figure(figsize=(10, 6))
plt.plot(sizes, mean_runtimes_naive, label='Naive Method', marker='o', color='b')
plt.errorbar(sizes, mean_runtimes_naive, error_bars_naive, ecolor='k')
plt.plot(sizes, mean_runtimes_fft, label='FFT Method', marker='o', color='r')
plt.errorbar(sizes, mean_runtimes_fft, error_bars_fft, ecolor='k')
plt.xlabel('Problem Size (Image Dimensions)')
plt.ylabel('Mean Runtime (Seconds)')
plt.title('Runtime Comparison: Naive Method vs. Cooley-Tukey FFT')
plt.legend()
plt.grid(True)
plt.show()
def compression_display(image):
plt.figure(figsize=(12, 8))
sizes = []
compression_levels = [0, 20, 40, 60, 80, 99.9]
for i, compression_percent in enumerate(compression_levels):
compressed, fft_result = compress_image(image, compression_percent)
sparse_matrix_size = np.sum(fft_result != 0) * fft_result.itemsize
sizes.append(sparse_matrix_size)
plt.subplot(2, 3, i + 1)
plt.imshow(compressed, cmap='gray')
plt.title(f'{compression_percent}% compression\nSize: {sparse_matrix_size} bytes')
plt.tight_layout()
plt.show()
for level, size in zip(compression_levels, sizes):
print(f"Compression Level: {level}% -> Compressed Size: {size} bytes")
if __name__ == "__main__":
parameters = set_arguments(sys.argv)
if parameters is not None:
img = cv.imread(parameters['image'], cv.IMREAD_GRAYSCALE)
if parameters['mode'] == 1:
process_image(img)
elif parameters['mode'] == 2:
denoise_image(img, 300, 250)
elif parameters['mode'] == 3:
compression_display(img)
elif parameters['mode'] == 4:
plot()