Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions example/signal_and_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,19 @@
import matplotlib.pyplot as plt

from visualize.simulation_plotter import SimulationPlotter
from signal_edit.sampler import Sampler
from signal_edit.sampler import Sampler, PulseGenerator

# create input signal
input_points = np.array([
[0, 0],
[1, 0],
[1, 1],
[2, 1],
[2, 0],
[10, 0]
], dtype=np.float64)

time, input_signal = Sampler.create_periodical(
input_points,
start_time=0.0,
end_time=10.0,
sampling_interval=0.1
time, input_signal = PulseGenerator.sample_pulse(
sampling_interval=0.1,
start_time=1.0,
period=2.0,
pulse_width=50.0,
pulse_amplitude=1.0,
duration=10.0,
number_of_pulse=1
)


# plot the signal
plotter = SimulationPlotter()

Expand Down
84 changes: 84 additions & 0 deletions signal_edit/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
class Sampler:
@staticmethod
def modify_same_sample_time_value(data, interval):
if data[0, 0] == data[1, 0]:
data = data[1:, :]

for i, val in enumerate(data):
if i == 0:
data_out = data[0, :]
Expand Down Expand Up @@ -43,3 +46,84 @@ def create_periodical(data, start_time, end_time, sampling_interval):
sample_times = sample_times.reshape(-1, 1)

return sample_times, sampled_values


class PulseGenerator:
@staticmethod
def generate_pulse_points(start_time, period, pulse_width, pulse_amplitude, duration, number_of_pulse=np.inf):
"""
Generate pulse points for a pulse signal.

Parameters:
start_time (float): The start time of the pulse [s].
period (float): The period of the pulse [s].
pulse_width (float): The width of the pulse as a percentage of the period [%].
pulse_amplitude (float): The amplitude of the pulse.
duration (float): Total duration of the signal [s].
number_of_pulse (int): Number of pulses to generate. Default is np.inf for infinite pulses.

Returns:
np.ndarray: Array of input points with columns [time, value].
"""
time_points = []
value_points = []
pulse_count = 0

if start_time > 0.0:
time_points.append(0.0)
value_points.append(0.0)

current_time = start_time
while current_time <= duration:
# Pulse ON
time_points.append(current_time)
value_points.append(0.0)
time_points.append(current_time)
value_points.append(pulse_amplitude)

pulse_on_duration = period * (pulse_width / 100.0)
current_time += pulse_on_duration

if current_time > duration:
break

# Pulse OFF
time_points.append(current_time)
value_points.append(pulse_amplitude)
time_points.append(current_time)
value_points.append(0.0)

pulse_off_duration = period - pulse_on_duration
current_time += pulse_off_duration

pulse_count += 1
if pulse_count >= number_of_pulse:
break

if time_points[-1] < duration:
time_points.append(duration)
value_points.append(value_points[-1])

return np.array(list(zip(time_points, value_points)))

@staticmethod
def sample_pulse(sampling_interval, start_time, period, pulse_width, pulse_amplitude, duration, number_of_pulse=np.inf):
"""
Sample the pulse signal based on the provided parameters.

Parameters:
sampling_interval (float): Sampling interval [s].
start_time (float): The start time of the pulse [s].
period (float): The period of the pulse [s].
pulse_width (float): The width of the pulse as a percentage of the period [%].
pulse_amplitude (float): The amplitude of the pulse.
duration (float): Total duration of the signal [s].
number_of_pulse (int): Number of pulses to generate. Default is np.inf for infinite pulses.

Returns:
np.ndarray: Array of sampled points with columns [time, value].
"""
input_points = PulseGenerator.generate_pulse_points(
start_time, period, pulse_width, pulse_amplitude, duration, number_of_pulse)

return Sampler.create_periodical(input_points, 0.0, duration, sampling_interval)