Skip to content

Commit ffe4aa2

Browse files
added new noise options
1 parent aeffdfe commit ffe4aa2

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ sudo apt install python3-gi-cairo
1010

1111
## Todo
1212

13-
- Störsignal überarbeiten
14-
- Regler verbessern
15-
- regler an einer stelle tSleep einfügen (flag)
16-
- logging neu machen (csv?) (einstellung des systems in den csv header (p, i, d, tSleep, ...)):w
13+
- [x] Störsignal überarbeiten
14+
- [x] Regler verbessern
15+
- [x] regler an einer stelle tSleep einfügen (flag)
16+
- [x] logging neu machen (csv?) (einstellung des systems in den csv header (p, i, d, tSleep, ...)):w

randomNoise.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,20 @@ def getFromDebugFile(debugFile: Path, lastVal: float, args) -> float:
4242

4343
return content
4444

45-
def generateNoise(args) -> float:
45+
def generateNoise(noise_type: str, noise_strength: float, drift: float) -> float:
4646
# TODO: add other noise types (like sin and (sin+normal)/2)
47-
48-
# draws a random value from normal (Gaussian) distribution bewteen -1 and 1
49-
noise = args.noise_strength * np.random.normal(0,1,1)[0] + args.drift
47+
if noise_type == 'normal':
48+
# draws a random value from normal (Gaussian) distribution bewteen -1 and 1
49+
noise = noise_strength * np.random.normal(0,1,1)[0] + drift
50+
elif noise_type == 'sin':
51+
# draws a random value from a sine wave bewteen -1 and 1
52+
noise = noise_strength * np.sin(np.random.uniform(0, 2*np.pi, 1)[0]) + drift
53+
elif noise_type == 'mix':
54+
# draws a random value from a mixture of a normal distribution and a sine wave bewteen -1 and 1
55+
noise = (generateNoise(noise_type='normal', noise_strength=noise_strength, drift=drift) + generateNoise(noise_type='sin', noise_strength=noise_strength, drift=drift)) / 2
56+
else:
57+
print(f'Error: noise type {noise_type} not recognized')
58+
exit()
5059
return noise
5160

5261

@@ -70,7 +79,7 @@ def debugMode(args):
7079
while True:
7180
# Writes a random value to the debugfile
7281
fileVal = getFromDebugFile(debugFile=debugFile, lastVal=lastVal, args=args)
73-
noise = generateNoise(args=args)
82+
noise = generateNoise(noise_type=args.noise_type, noise_strength=args.noise_strength, drift=args.drift)
7483

7584
# conversion to float because python threw an error otherwise
7685
r = float(fileVal) + float(noise)
@@ -126,6 +135,7 @@ def main():
126135
parentParser.add_argument('--version', action='version', version=ver)
127136
parentParser.add_argument('--noise-strength', type=float, default=0.5, help='the strength of the noise. The default value is 0.5')
128137
parentParser.add_argument('--drift', type=float, default=0.0, help='the drift of the noise. The default value is 0.0')
138+
parentParser.add_argument('--noise-type', type=str, default='normal', help='the type of noise that should be generated. The default value is "normal" (a normal distribution). Alternativly, you can use "sin" for noise a sine wave like noise, or "mix for a bixture of both"')
129139

130140
# subcommands
131141
subparsers = parser.add_subparsers(dest='mode', help='the program can use an epics interface or create a debug enviroment for another script')
@@ -156,4 +166,4 @@ def main():
156166

157167

158168
if __name__ == '__main__':
159-
main()
169+
main()

0 commit comments

Comments
 (0)