-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaveforms_mp.py
More file actions
166 lines (126 loc) · 5.28 KB
/
waveforms_mp.py
File metadata and controls
166 lines (126 loc) · 5.28 KB
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
"""
Copyright (c) 2026 Peter Robinson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
This file contains a collection of generator factories that yield floats for cyclic functions.
They are all uses of gb.WaveGeneratorFactory
Example:
Generate a sine wave for 10 steps:
>>> sine = sine_wave_factory(10)
>>> values = list(sine())
>>> print(len(values)) # Will be 8 (rounded to multiple of 4)
8
"""
import generator_builder_mp as gb
import math
TWO_PI = 2 * math.pi
def sine_function(x):
"""Sine wave function mapping [0, 1] to [0, 1].
Generates one complete sine wave cycle that oscillates between 0 and 1.
Args:
x: Input value in [0, 1] representing position in the cycle.
Returns:
Output value in [0, 1] representing the sine wave at that position.
Example:
>>> sine_function(0.0) # Start of cycle
0.5
>>> sine_function(0.25) # Quarter cycle
1.0
>>> sine_function(0.5) # Half cycle
0.5
"""
return (math.sin(TWO_PI * x) + 1) / 2
def square_wave_function(x):
"""Square wave function mapping [0, 1] to [0, 1].
Generates a square wave that is 1.0 for the first half of the cycle
and 0.0 for the second half.
Args:
x: Input value in [0, 1] representing position in the cycle.
Returns:
1.0 if x < 0.5, otherwise 0.0.
Example:
>>> square_wave_function(0.25)
1.0
>>> square_wave_function(0.75)
0.0
"""
return 1.0 if x < 0.5 else 0.0
def sawtooth_wave_function(x):
"""Sawtooth wave function mapping [0, 1] to [0, 1].
Generates a sawtooth wave that is similar to the sine function but using line segments instead.
Args:
x: Input value in [0, 1] representing position in the cycle.
Returns:
Output value in [0, 1] representing the sawtooth wave at that position.
Example:
>>> sawtooth_wave_function(0.125) # First quarter ramp up
0.75
>>> sawtooth_wave_function(0.25) # Peak
1.0
>>> sawtooth_wave_function(0.5) # Midpoint ramp down
0.5
>>> sawtooth_wave_function(0.75) # Trough
0.0
>>> sawtooth_wave_function(0.875) # Last quarter ramp down
0.25
"""
if x < 0.25:
return 0.5 + 2 * x
elif x < 0.75:
return 1.5 - 2 * x
else:
return 2 * (x - 0.75)
def sine_wave_factory(*args, **kwargs):
"""Function to create a sine wave generator factory.
Provides a convenient way to create a highly configurable instance of WaveGeneratorFactory configured for
sine waveforms.
Returns:
A GeneratorFactory that yields sine wave values in [0, 1].
Example:
>>> sine = sine_wave_factory(16)
>>> values = list(sine())
>>> len(values)
16
>>> print([round(v, 2) for v in values])
[0.5, 0.69, 0.85, 0.96, 1.0, 0.96, 0.85, 0.69, 0.5, 0.31, 0.15, 0.04, 0.0, 0.04, 0.15, 0.31]
"""
return gb.WaveGeneratorFactory(sine_function, *args, **kwargs)
def square_wave_factory(*args, **kwargs):
"""Factory function to create a square wave generator.
Provides a convenient way to create a highly configurable instance of WaveGeneratorFactory configured for
square waveforms.
Returns:
A GeneratorFactory that yields square wave values (0.0 or 1.0).
Example:
>>> square = square_wave_factory(8)
>>> values = list(square())
>>> print(values)
[1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0]
"""
return gb.WaveGeneratorFactory(square_wave_function, *args, **kwargs)
def sawtooth_wave_factory(*args, **kwargs):
"""Factory function to create a sawtooth wave generator.
Provides a convenient way to create a highly configurable instance of WaveGeneratorFactory configured for
sawtooth waveforms.
Returns:
A GeneratorFactory that yields sawtooth wave values in [0, 1].
Example:
>>> sawtooth = sawtooth_wave_factory(12)
>>> values = list(sawtooth())
>>> print([round(v, 2) for v in values])
[0.5, 0.67, 0.83, 1.0, 0.83, 0.67, 0.5, 0.33, 0.17, 0.0, 0.17, 0.33]
"""
return gb.WaveGeneratorFactory(sawtooth_wave_function, *args, **kwargs)