Skip to content

Commit 024f585

Browse files
committed
rewrite derivatives of triangle wave
1 parent ff9f0ca commit 024f585

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

waveform_editor/tendencies/periodic/triangle_wave.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
import numpy as np
24
from param import depends
35

@@ -7,7 +9,9 @@
79
class TriangleWaveTendency(PeriodicBaseTendency):
810
"""A tendency representing a triangle wave."""
911

10-
def generate(self, time=None):
12+
def get_value(
13+
self, time: Optional[np.ndarray] = None
14+
) -> tuple[np.ndarray, np.ndarray]:
1115
"""Generate time and values based on the tendency. If no time array is provided,
1216
a time array will be created from the start to the end of the tendency, where
1317
time points are defined for every peak and trough in the tendency.
@@ -23,21 +27,17 @@ def generate(self, time=None):
2327
values = self._calc_triangle_wave(time)
2428
return time, values
2529

26-
def get_start_value(self) -> float:
27-
"""Returns the value of the tendency at the start."""
28-
return self._calc_triangle_wave(self.start)
29-
30-
def get_end_value(self) -> float:
31-
"""Returns the value of the tendency at the end."""
32-
return self._calc_triangle_wave(self.end)
30+
def get_derivative(self, time: np.ndarray) -> np.ndarray:
31+
"""Get the derivative values on the provided time array.
3332
34-
def get_derivative_start(self) -> float:
35-
"""Returns the derivative of the tendency at the start."""
36-
return self._calc_triangle_derivative(self.start)
33+
Args:
34+
time: The time array on which to generate points.
3735
38-
def get_derivative_end(self) -> float:
39-
"""Returns the derivative of the tendency at the end."""
40-
return self._calc_triangle_derivative(self.end)
36+
Returns:
37+
numpy array containing the derivatives
38+
"""
39+
derivatives = self._calc_triangle_derivative(time)
40+
return derivatives
4141

4242
def _calc_triangle_wave(self, time):
4343
"""Calculates the point of the triangle wave at a given time point or
@@ -63,8 +63,8 @@ def _calc_triangle_derivative(self, time):
6363
The derivative of the triangle wave.
6464
"""
6565
is_rising = self._calc_phase(time) % (2 * np.pi) > np.pi
66-
67-
return self.rate if is_rising else -self.rate
66+
rate = 4 * self.frequency * self.amplitude
67+
return np.where(is_rising, rate, -rate)
6868

6969
def _calc_phase(self, time):
7070
"""Calculates the phase of the triangle wave at a given time point or
@@ -78,11 +78,6 @@ def _calc_phase(self, time):
7878
"""
7979
return 2 * np.pi * self.frequency * (time - self.start) + self.phase - np.pi / 2
8080

81-
@depends("values_changed", watch=True)
82-
def _update_rate(self):
83-
"""Calculates the rate of change."""
84-
self.rate = 4 * self.frequency * self.amplitude
85-
8681
def _calc_minimal_triangle_wave(self):
8782
"""Calculates the time points at which the peaks and troughs of the triangle
8883
wave occur, which are minimally required to represent the triangle wave fully.

0 commit comments

Comments
 (0)