samedi 1 avril 2023

Generating random square wave that last for certain time

I am trying to generate a random square wave signal in Python that lasts for a certain amount of time. Specifically,each square wave should have a fixed duration of 5 minutes and a random amplitude that is bounded between 0 and 1. The square wave should remain at each amplitude level for the full 5 minutes and then transition to a new amplitude level.

I've tried adapting existing code, but so far, I haven't been able to generate the square waves as described. I'm using NumPy and Matplotlib for the simulation and visualization.

import numpy as np
import matplotlib.pyplot as plt

# Define the bounds of the random vectors
min_value = 0.0
max_value = 1.0

# Define the duration of each square value in seconds
square_duration = 5 * 60  # 5 minutes

# Define the total number of samples in the simulation
max_sim = 1 * 60 * 60  # 24 hours * 60 minutes/hour * 60 seconds/minute

# Initialize the load and renewable profiles vectors
pl = np.zeros(max_sim)

# Generate the random bounded vectors
for i in range(max_sim):
    if i % (square_duration + 1) == 0:  # Start a new square value
        square_value = np.random.uniform(min_value, max_value)
        square_counter = 0
        square_change = np.random.randint(1, square_duration + 1)
    if square_counter < square_change:  # Use the same square value
        pl[i] = square_value
        square_counter += 1
    else:  # Start a new square value
        square_value = np.random.uniform(min_value, max_value)
        pl[i] = square_value
        square_counter = 0
        square_change = np.random.randint(1, square_duration + 1)

    # Add sinusoidal variation to the load profile
    pl[i] += np.sin(i / max_sim * 2 * np.pi) * 0.15 * 1

    # Clip the values to the defined bounds
    pl[i] = np.clip(pl[i], min_value, max_value)
   

plt.show()

Can anyone suggest how to modify the existing code or provide an alternative solution to generate the random square wave profiles I need?

This is the output I am having:

Random vector

It is almost ready but I need that the duration of each square to be constant.




Aucun commentaire:

Enregistrer un commentaire