From f25cbeeebb0c0e39f6760c901630bb1ee988e955 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 18 Apr 2025 08:36:01 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E3=83=91=E3=83=AB=E3=82=B9=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=99=A8=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/signal_and_plot.py | 18 +++++------ signal_edit/sampler.py | 62 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/example/signal_and_plot.py b/example/signal_and_plot.py index 0d40d76..c71baa4 100644 --- a/example/signal_and_plot.py +++ b/example/signal_and_plot.py @@ -6,17 +6,17 @@ 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) +input_points = PulseGenerator.generate_pulse_points( + start_time=1.0, + period=2.0, + pulse_width=50.0, + pulse_amplitude=1.0, + duration=10.0, + number_of_pulse=1 +) time, input_signal = Sampler.create_periodical( input_points, diff --git a/signal_edit/sampler.py b/signal_edit/sampler.py index fedcdb2..690a69c 100644 --- a/signal_edit/sampler.py +++ b/signal_edit/sampler.py @@ -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, :] @@ -43,3 +46,62 @@ 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))) From e5dc90282acb484fd79c38cb59763ee619c1634f Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 18 Apr 2025 08:44:25 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/signal_and_plot.py | 11 ++--------- signal_edit/sampler.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/example/signal_and_plot.py b/example/signal_and_plot.py index c71baa4..3ca292e 100644 --- a/example/signal_and_plot.py +++ b/example/signal_and_plot.py @@ -9,7 +9,8 @@ from signal_edit.sampler import Sampler, PulseGenerator # create input signal -input_points = PulseGenerator.generate_pulse_points( +time, input_signal = PulseGenerator.sample_pulse( + sampling_interval=0.1, start_time=1.0, period=2.0, pulse_width=50.0, @@ -18,14 +19,6 @@ number_of_pulse=1 ) -time, input_signal = Sampler.create_periodical( - input_points, - start_time=0.0, - end_time=10.0, - sampling_interval=0.1 -) - - # plot the signal plotter = SimulationPlotter() diff --git a/signal_edit/sampler.py b/signal_edit/sampler.py index 690a69c..fc0dcb9 100644 --- a/signal_edit/sampler.py +++ b/signal_edit/sampler.py @@ -105,3 +105,25 @@ def generate_pulse_points(start_time, period, pulse_width, pulse_amplitude, dura 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)