-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfreq_filters.py
224 lines (186 loc) · 7.65 KB
/
freq_filters.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
import numpy as np
# Lowpass Filter
###############################################################################
# INPUT
# shape: P x Q shape of filter
# d0: Cutoff frequency
# ftype: Filter type ('ideal', 'butterworth' or 'gaussian')
# n: Order of filter (only applies for butterworth filters)
# u_k: u position of notch pair (only applies for notch filters)
# v_k: v position of notch pair (only applies for notch filters)
###############################################################################
# OUTPUT
# H: A lowpass filter with input parameters
###############################################################################
def lowpass_filter(shape, d0=160, ftype='butterworth', n=2, u_k=0, v_k=0):
P, Q = shape
# Initialize filter with zeros
H = np.zeros((P, Q))
# Traverse through filter
for u in range(0, P):
for v in range(0, Q):
# Get euclidean distance from point D(u,v) to the center
D_uv = np.sqrt((u - P/2 + u_k)**2 + (v - Q/2 + v_k)**2)
# Define lowpass transfer funtion according to filter type
if ftype == 'ideal':
if D_uv <= d0:
H[u, v] = 1.0
elif ftype == 'butterworth':
H[u, v] = 1/(1 + (D_uv/d0)**(2*n))
elif ftype == 'gaussian':
H[u, v] = np.exp(-D_uv**2 / (2*d0)**2)
return H
# Highpass Filter
###############################################################################
# INPUT
# shape: P x Q shape of filter
# d0: Cutoff frequency
# ftype: Filter type ('ideal', 'butterworth' or 'gaussian')
# n: Order of filter (only applies for butterworth filters)
# u_k: u position of notch pair (only applies for notch filters)
# v_k: v position of notch pair (only applies for notch filters)
###############################################################################
# OUTPUT
# H: A highpass filter with input parameters
###############################################################################
def highpass_filter(shape, d0=160, ftype='butterworth', n=2, u_k=0, v_k=0):
# Inverse of lowpass
H = 1.0 - lowpass_filter(shape, d0, ftype, n, u_k, v_k)
return H
# Bandreject Filter
###############################################################################
# INPUT
# shape: P x Q shape of filter
# d0: Cutoff frequency
# w: Width of the band
# ftype: Filter type ('ideal', 'butterworth' or 'gaussian')
# n: Order of filter (only applies for butterworth filters)
###############################################################################
# OUTPUT
# H: A bandreject filter with input parameters
###############################################################################
def bandreject_filter(shape, d0=160, w=20, ftype='butterworth', n=2):
P, Q = shape
# Initialize filter with ones
H = np.ones((P, Q))
# Traverse through filter
for u in range(0, P):
for v in range(0, Q):
# Get euclidean distance from point D(u,v) to the center
D_uv = np.sqrt((u - (P/2))**2 + (v - (Q/2))**2)
# Define bandreject transfer funtion for each filter type
if ftype == 'ideal':
if (d0 - (w/2)) <= D_uv <= (d0 + (w/2)):
H[u, v] = 0.0
elif ftype == 'butterworth':
if D_uv == d0: # To avoid dividing by zero
H[u, v] = 0
else:
H[u, v] = 1/(1 + ((D_uv*w)/(D_uv**2 - d0**2))**(2*n))
elif ftype == 'gaussian':
if D_uv == 0: # To avoid dividing by zero
H[u, v] = 1
else:
H[u, v] = 1.0 - np.exp(-((D_uv**2 - d0**2) / (D_uv * w))**2)
return H
# Bandpass Filter
###############################################################################
# INPUT
# shape: P x Q shape of filter
# d0: Cutoff frequency
# w: Width of the band
# ftype: Filter type ('ideal', 'butterworth' or 'gaussian')
# n: Order of filter (only applies for butterworth filters)
###############################################################################
# OUTPUT
# H: A bandpass filter with input parameters
###############################################################################
def bandpass_filter(shape, d0=160, w=20, ftype='butterworth', n=2):
# Inverse of bandreject
H = 1.0 - bandreject_filter(shape, d0, w, ftype, n)
return H
# Notch Reject Filter (several notch pairs not implemented)
###############################################################################
# INPUT
# shape: P x Q shape of filter
# d0: Cutoff frequency
# ftype: Filter type ('ideal', 'butterworth' or 'gaussian')
# n: Order of filter (only applies for butterworth filters)
# u_k: u position of notch pair
# v_k: v position of notch pair
###############################################################################
# OUTPUT
# H: A notch reject filter with input parameters
###############################################################################
def notch_reject_filter(shape, d0=160, ftype='butterworth', n=2, u_k=0, v_k=0):
# Form product of highpass filters at position (-u_k, -v_k) and (u_k, v_k)
H = highpass_filter(shape, d0, ftype, n, -u_k, -v_k) * highpass_filter(shape, d0, ftype, n, u_k, v_k)
return H
# Notch Pass Filter (several notch pairs not implemented)
###############################################################################
# INPUT
# shape: P x Q shape of filter
# d0: Cutoff frequency
# ftype: Filter type ('ideal', 'butterworth' or 'gaussian')
# n: Order of filter (only applies for butterworth filters)
# u_k: u position of notch pair
# v_k: v position of notch pair
###############################################################################
# OUTPUT
# H: A notch pass filter with input parameters
###############################################################################
def notch_pass_filter(shape, d0=160, ftype='butterworth', n=2, u_k=0, v_k=0):
# Inverse of notch reject
H = 1.0 - notch_reject_filter(shape, d0, ftype, n, u_k, v_k)
return H
# Filter image in frequency domain
###############################################################################
# INPUT
# img: Input image
# fclass: Filter class ('lowpass', 'highpass', 'bandreject' or 'bandpass')
# ftype: Filter type ('ideal', 'butterworth' or 'gaussian')
# d0: Cutoff frequency
# w: Width of the band (only applies for bandpass/bandreject filters)
# n: Order of filter (only applies for butterworth filters)
###############################################################################
# OUTPUT
# G: Output image
# H: Filter image
# P: Power spectrum of input image
###############################################################################
def filter_image_freq(img, fclass='lowpass', ftype='butterworth', d0=160, w=20, n=2, u_k=0, v_k=0):
# Get padding parameters
M, N = img.shape
P = 2*M
Q = 2*N
# Take the fourier transform of the image, with padding to shape P X Q
F = np.fft.fft2(img, s=(P,Q))
# Shift the low frequencies to the center.
F = np.fft.fftshift(F)
# Get power spectrum of the image
pow_spec = np.abs(F)**2
# Create a filter with input parameters
if fclass == 'lowpass':
H = lowpass_filter(F.shape, d0, ftype, n)
elif fclass == 'highpass':
H = highpass_filter(F.shape, d0, ftype, n)
elif fclass == 'bandreject':
H = bandreject_filter(F.shape, d0, w, ftype, n)
elif fclass == 'bandpass':
H = bandpass_filter(F.shape, d0, w, ftype, n)
elif fclass == 'notchreject':
H = notch_reject_filter(F.shape, d0, ftype, n, u_k, v_k)
elif fclass == 'notchpass':
H = notch_pass_filter(F.shape, d0, ftype, n, u_k, v_k)
# Form product of image with filter
G = F * H
# Shift frequencies back
G = np.fft.ifftshift(G)
# Inverse fourier transform to get output image in spatial domain
G = np.fft.ifft2(G)
# Get real values
G = np.abs(G)
# Extract M x N image from top left quadrant
G = G[0:M, 0:N]
# Return output image, the filter used and the power spectrum of input image
return G, np.abs(H), np.log(pow_spec)