I would like to implement a "fast attack / slow decay" (peak detect with exponential decay) filter on a time series ts (a column in a pandas dataframe), described as follows:
fasd[t] = max(ts[t], 0.9 * fasd[t-1])
The "basic" code (below) works, but is there pythonic and efficient way to do it, using rolling() or vectorized methods ? Thanks.
import pandas as pd
ts = [1,0,0,0,0,1,0,0,0,1,0.95,1,1,1,1,1,0,0,1,1,1,1,1,1,]
df = pd.DataFrame({'ts':ts})
df['fasd'] = 0
df.loc[0,'fasd'] = df.iloc[0]['ts']
for i in range(1, len(df)):
df.loc[i, 'fasd'] = max(df.loc[i,'ts'], 0.9*df.loc[i-1, 'fasd'])
Using numpy is more efficient:
Output: