# Path: blond/llrf/signal_processing.py
# def moving_average(x, N, x_prev=None):
#     """Function to calculate the moving average (or running mean) of the input
#     data.
# 
#     Parameters
#     ----------
#     x : float array
#         Data to be smoothed
#     N : int
#         Window size in points
#     x_prev : float array
#         Data to pad with in front
# 
#     Returns
#     -------
#     float array
#         Smoothed data array of size
#             * len(x) - N + 1, if x_prev = None
#             * len(x) + len(x_prev) - N + 1, if x_prev given
# 
#     """
# 
#     if x_prev is not None:
#         # Pad in front with x_prev signal
#         x = np.concatenate((x_prev, x))
# 
#     # based on https://stackoverflow.com/a/14314054
#     mov_avg = np.cumsum(x)
#     mov_avg[N:] = mov_avg[N:] - mov_avg[:-N]
#     return mov_avg[N-1:] / N


import numpy as np
import matplotlib.pyplot as plt 
from blond.llrf.signal_processing import moving_average

# granted to it by virtue of its status as an Intergovernmental Organization or
# submit itself to any jurisdiction.
# Project website: http://blond.web.cern.ch/

"""
Example for llrf.filters and llrf.cavity_feedback

:Authors: **Helga Timko**
"""



n = 10000
n_ma = 100
iterations = 10

time = np.linspace(0, 10.e-6, n)
tau = 1.e-6
signal = 1 - np.exp(-time/tau) + 0.01*(np.random.randn(n) - 0.5)
time = np.linspace(0, 20.e-6, 2*n)
signal = np.concatenate((signal, signal))

plt.figure('Moving average')
plt.clf()
plt.plot(1e6*time, signal)
prev = np.zeros(n_ma-1)

for i in range(iterations):
    print("Average of end of previous signal", np.mean(prev))
    tmp = signal[-n_ma+1:]

# Path: blond/llrf/signal_processing.py
# def moving_average(x, N, x_prev=None):
#     """Function to calculate the moving average (or running mean) of the input
#     data.
# 
#     Parameters
#     ----------
#     x : float array
#         Data to be smoothed
#     N : int
#         Window size in points
#     x_prev : float array
#         Data to pad with in front
# 
#     Returns
#     -------
#     float array
#         Smoothed data array of size
#             * len(x) - N + 1, if x_prev = None
#             * len(x) + len(x_prev) - N + 1, if x_prev given
# 
#     """
# 
#     if x_prev is not None:
#         # Pad in front with x_prev signal
#         x = np.concatenate((x_prev, x))
# 
#     # based on https://stackoverflow.com/a/14314054
#     mov_avg = np.cumsum(x)
#     mov_avg[N:] = mov_avg[N:] - mov_avg[:-N]
#     return mov_avg[N-1:] / N


# Path: __BENCHMARKS/BM3_OTFB_moving_average.py
import numpy as np
import matplotlib.pyplot as plt 
from blond.llrf.signal_processing import moving_average

# granted to it by virtue of its status as an Intergovernmental Organization or
# submit itself to any jurisdiction.
# Project website: http://blond.web.cern.ch/

"""
Example for llrf.filters and llrf.cavity_feedback

:Authors: **Helga Timko**
"""



n = 10000
n_ma = 100
iterations = 10

time = np.linspace(0, 10.e-6, n)
tau = 1.e-6
signal = 1 - np.exp(-time/tau) + 0.01*(np.random.randn(n) - 0.5)
time = np.linspace(0, 20.e-6, 2*n)
signal = np.concatenate((signal, signal))

plt.figure('Moving average')
plt.clf()
plt.plot(1e6*time, signal)
prev = np.zeros(n_ma-1)

for i in range(iterations):
    print("Average of end of previous signal", np.mean(prev))
    tmp = signal[-n_ma+1:]

    signal = moving_average(signal, n_ma, prev)
