Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience functions for pulses #35

Open
RemDelaporteMathurin opened this issue Nov 3, 2024 · 2 comments
Open

Convenience functions for pulses #35

RemDelaporteMathurin opened this issue Nov 3, 2024 · 2 comments

Comments

@RemDelaporteMathurin
Copy link
Collaborator

It would be convenient to have functions like periodic step function, periodic step function with ramp up/down etc.

def periodic_step_function(x, period_on, period_total, value_on, value_off=0.0):
    """
    Creates a periodic step function with two periods.
    """

    if period_total < period_on:
        raise ValueError("period_total must be greater than period_on")
    
    if x % period_total < period_on:
        return value_on
    else:
        return value_off
@RemDelaporteMathurin
Copy link
Collaborator Author

We also need a trapezoidal membership function:
image

@RemDelaporteMathurin
Copy link
Collaborator Author

Maybe something like this would work:

def periodic_trapezoidal_membership_function(x, period, a, b, c, d, value_on=1.0, value_off=0.0):
    """
    Creates a periodic trapezoidal membership function.
    
    Parameters:
    x : float
        The input value.
    period : float
        The period of the function.
    a : float
        The start of the rising edge within one period.
    b : float
        The start of the top edge within one period.
    c : float
        The end of the top edge within one period.
    d : float
        The end of the falling edge within one period.
    value_on : float
        The value during the plateau (top edge).
    value_off : float
        The value outside the trapezoid (when x < a or x > d).
    
    Returns:
    float
        The membership value.
    """
    x_mod = x % period
    if x_mod < a or x_mod > d:
        return value_off
    elif a <= x_mod < b:
        return value_off + (value_on - value_off) * (x_mod - a) / (b - a)
    elif b <= x_mod <= c:
        return value_on
    elif c < x_mod <= d:
        return value_off + (value_on - value_off) * (d - x_mod) / (d - c)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant