-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
64 lines (48 loc) · 1.66 KB
/
util.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
import numpy as np
import scipy.io as sio
import scipy.misc as smisc
from scipy.optimize import fminbound
def to_rgb(img):
img = np.atleast_3d(img)
channels = img.shape[2]
if channels < 3:
img = np.tile(img, 3)
img[np.isnan(img)] = 0
img -= np.amin(img)
img /= np.amax(img)
img *= 255
return img
def to_double(img):
img = np.atleast_3d(img)
channels = img.shape[2]
if channels < 3:
img = np.tile(img, 3)
img[np.isnan(img)] = 0
img -= np.amin(img)
img /= np.amax(img)
return img
def save_mat(img, path):
sio.savemat(path, {'img':img})
def save_img(img, path):
img = to_rgb(img)
smisc.imsave(path, img.round().astype(np.uint8))
def addwgn(x, inputSnr):
noiseNorm = np.linalg.norm(x.flatten('F')) * 10**(-inputSnr/20)
xBool = np.isreal(x)
real = True
for e in np.nditer(xBool):
if e == False:
real = False
if (real == True):
noise = np.random.randn(np.shape(x)[0],np.shape(x)[1])
else:
noise = np.random.randn(np.shape(x)[0],np.shape(x)[1]) + 1j * np.random.randn(np.shape(x)[0],np.shape(x)[1])
noise = noise/np.linalg.norm(noise.flatten('F')) * noiseNorm
y = x + noise
return y, noise
def optimizeTau(x, algoHandle, taurange, maxfun=10):
# maxfun ~ number of iterations for optimization
evaluateSNR = lambda x, xhat: 20*np.log10(np.linalg.norm(x.flatten('F'))/np.linalg.norm(x.flatten('F')-xhat.flatten('F')))
fun = lambda tau: -evaluateSNR(x,algoHandle(tau)[0])
tau, fval, _, _ = fminbound(fun, taurange[0],taurange[1], xtol = 1e-6, maxfun = maxfun, disp = 3, full_output = True)
return tau, fval