-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsinenoise.py
executable file
·64 lines (53 loc) · 2.02 KB
/
sinenoise.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
#!/usr/bin/python3
import numpy as np
import argparse
from pathlib import Path
from time import sleep
ver = "1.0.0"
author = "Valentin Reichenbach"
description = """
Generates a sine wave and writes it to a file.
"""
epilog = """
Author: Valentin Reichenbach
Version: 1.0.0
License: GPLv3+
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=description, epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-f", "--frequency", help="Frequency of the sine wave in Hz. Default is 50", type=int, default=50)
parser.add_argument("-o", "--output", help="Output file. Default is sin.txt", default=Path("sin.txt"))
parser.add_argument("-F", "--force", help="Force overwrite of output file", action="store_true")
parser.add_argument("-v", "--verbose", help="Verbose output", action="count", default=0)
parser.add_argument("-V", "--version", help="Print version and exit", action="version", version=ver)
args = parser.parse_args()
x = 0
if args.frequency < 0:
print("Frequency must be positive")
exit(1)
if args.output.exists():
if args.force:
print("Overwriting " + str(args.output) + "")
else:
print("File " + str(args.output) + " already exists. Use -F to force overwrite")
exit(1)
else:
print("Writing to " + str(args.output) + "")
# convert to string for python 3.5
args.output = str(args.output)
try:
while True:
# Generate sine wave
y = np.sin(2 * np.pi * args.frequency * x)
x = x + 1/args.frequency
if args.verbose > 0:
print("Frequency: " + str(args.frequency) + " Hz, x: " + str(x) + ", y: " + str(y) + "")
sleep(1/args.frequency)
# Write to file
with open(args.output, "w") as f:
f.write(str(y))
except KeyboardInterrupt:
print("\n Keyboard interrupt")
print("Exiting...")
f.close()
exit(0)