forked from yiqingGit/Deep_CCA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoise.py
executable file
·40 lines (35 loc) · 1.12 KB
/
Noise.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
import numpy
class Noise():
def SaltAndPepper(self, X, rate=0.3):
# Salt and pepper noise
drop = numpy.arange(X.shape[1])
numpy.random.shuffle(drop)
sep = int(len(drop)*rate)
drop = drop[:sep]
X[:, drop[:sep/2]]=0
X[:, drop[sep/2:]]=1
return X
def GaussianNoise(self, X, sd=0.5):
# Injecting small gaussian noise
X += numpy.random.normal(0, sd, X.shape)
return X
def MaskingNoise(self, X, rate=0.5):
mask = (numpy.random.uniform(0,1, X.shape)<rate).astype("i4")
X = mask*X
return X
def SaltAndPepper(rate=0.3):
# Salt and pepper noise
def func(X):
drop = numpy.random.uniform(0,1, X.shape)
z = numpy.where(drop < 0.5*rate)
o = numpy.where(numpy.abs(drop - 0.75*rate) < 0.25*rate)
X[z]=0
X[o]=1
return X
return func
def GaussianNoise(self, sd=0.5):
# Injecting small gaussian noise
def func(X):
X += numpy.random.normal(0, sd, X.shape)
return X
return func