Low pass filter in Python

The following code shows both a (single pole) low pass filter and a two pole low pass filter.

import pandas as pd
import matplotlib.pyplot as plt

data = list(map(lambda v: [0 if v < 20 else 100, None, None], range(100)))
df = pd.DataFrame(data, columns=['input', 'single_pole_low_pass', 'two_pole_low_pass'])

def pas(df, i, output, alpha=0.1, input_column='input'):
    input_i = df.loc[i][input_column]
    output_i_1 = df.loc[i-1][output] if i > 0 else df.loc[0][input_column]
    output_i = output_i_1 + alpha * (input_i - output_i_1)
    return output_i

for i in df.index:
    output = pas(df, i, 'single_pole_low_pass');
    df.loc[i, 'single_pole_low_pass'] = output
    output = pas(df, i, 'two_pole_low_pass', alpha=0.02, input_column='single_pole_low_pass');
    df.loc[i, 'two_pole_low_pass'] = output


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s