""" Solution Ex. 'Exponential Averaging Filter', Chapter 'Data Filtering' """

# author:   Thomas Haslwanter
# date:     April-2021


# Import the required packages
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal


def applyLeakyIntegrator(alpha:float, x:np.ndarray) -> np.ndarray:
    """
    Parameters
    ----------
        alpha : Decay rate of leaky integrator
        x : ndarry (N,)
            input data

    Return
    ------
    filtered : numpy vector (N,)
            Filtered data

    Example
    -------
    filtered = applyLeakyIntegrator(0.3, np.random.randn(200))
    """

    b = [alpha]
    a = [1, -(1-alpha)]
    filtered = signal.lfilter(b, a, x)

    return filtered


if __name__ == '__main__':
    # Prepare the input step, and the time-axis for plotting
    x = np.zeros(50)
    x[10:] = 1
    t = np.arange(len(x))

    # Plot the response, for different values of alpha
    alphas = [0.1, 0.2, 0.5, 0.9]
    colors = 'brgk'

    for (alpha, color) in zip(alphas, colors):
        plt.plot(t, applyLeakyIntegrator(alpha, x), color=color)

    plt.xlabel('Time')
    plt.ylabel('Step-responses')
    plt.legend(alphas)

    plt.show()

2aa15d3cabe823952c2e43d07ed4e6dff7b103dc
<mask token>

<mask token>


def applyLeakyIntegrator(alpha: float, x: np.ndarray) ->np.ndarray:
    """
    Parameters
    ----------
        alpha : Decay rate of leaky integrator
        x : ndarry (N,)
            input data

    Return
    ------
    filtered : numpy vector (N,)
            Filtered data

    Example
    -------
    filtered = applyLeakyIntegrator(0.3, np.random.randn(200))
    """
    b = [alpha]
    a = [1, -(1 - alpha)]
    filtered = signal.lfilter(b, a, x)
    return filtered


<mask token>

<mask token>


def applyLeakyIntegrator(alpha: float, x: np.ndarray) ->np.ndarray:
    """
    Parameters
    ----------
        alpha : Decay rate of leaky integrator
        x : ndarry (N,)
            input data

    Return
    ------
    filtered : numpy vector (N,)
            Filtered data

    Example
    -------
    filtered = applyLeakyIntegrator(0.3, np.random.randn(200))
    """
    b = [alpha]
    a = [1, -(1 - alpha)]
    filtered = signal.lfilter(b, a, x)
    return filtered


if __name__ == '__main__':
    x = np.zeros(50)
    x[10:] = 1
    t = np.arange(len(x))
    alphas = [0.1, 0.2, 0.5, 0.9]
    colors = 'brgk'
    for alpha, color in zip(alphas, colors):
        plt.plot(t, applyLeakyIntegrator(alpha, x), color=color)
    plt.xlabel('Time')
    plt.ylabel('Step-responses')
    plt.legend(alphas)
    plt.show()

<mask token>
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal


def applyLeakyIntegrator(alpha: float, x: np.ndarray) ->np.ndarray:
    """
    Parameters
    ----------
        alpha : Decay rate of leaky integrator
        x : ndarry (N,)
            input data

    Return
    ------
    filtered : numpy vector (N,)
            Filtered data

    Example
    -------
    filtered = applyLeakyIntegrator(0.3, np.random.randn(200))
    """
    b = [alpha]
    a = [1, -(1 - alpha)]
    filtered = signal.lfilter(b, a, x)
    return filtered


if __name__ == '__main__':
    x = np.zeros(50)
    x[10:] = 1
    t = np.arange(len(x))
    alphas = [0.1, 0.2, 0.5, 0.9]
    colors = 'brgk'
    for alpha, color in zip(alphas, colors):
        plt.plot(t, applyLeakyIntegrator(alpha, x), color=color)
    plt.xlabel('Time')
    plt.ylabel('Step-responses')
    plt.legend(alphas)
    plt.show()

""" Solution Ex. 'Exponential Averaging Filter', Chapter 'Data Filtering' """

# author:   Thomas Haslwanter
# date:     April-2021


# Import the required packages
import numpy as np
import matplotlib.pyplot as plt
from scipy import signal


def applyLeakyIntegrator(alpha:float, x:np.ndarray) -> np.ndarray:
    """
    Parameters
    ----------
        alpha : Decay rate of leaky integrator
        x : ndarry (N,)
            input data

    Return
    ------
    filtered : numpy vector (N,)
            Filtered data

    Example
    -------
    filtered = applyLeakyIntegrator(0.3, np.random.randn(200))
    """

    b = [alpha]
    a = [1, -(1-alpha)]
    filtered = signal.lfilter(b, a, x)

    return filtered


if __name__ == '__main__':
    # Prepare the input step, and the time-axis for plotting
    x = np.zeros(50)
    x[10:] = 1
    t = np.arange(len(x))

    # Plot the response, for different values of alpha
    alphas = [0.1, 0.2, 0.5, 0.9]
    colors = 'brgk'

    for (alpha, color) in zip(alphas, colors):
        plt.plot(t, applyLeakyIntegrator(alpha, x), color=color)

    plt.xlabel('Time')
    plt.ylabel('Step-responses')
    plt.legend(alphas)

    plt.show()
