# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

from spudtr import filters
from spudtr import get_demo_df, P3_1500_FEATHER
# -

# # Design a lowpass filter

# +
ftype = "lowpass"
cutoff_hz = 40.0  # 1/2 amplitude 
sfreq = 250.0

filters.show_filter(ftype=ftype, cutoff_hz=cutoff_hz, sfreq=sfreq);
# -

# # Design a bandpass filter
#
# and use Python `**` trickery to expand the dictionary

bp_filt = {
    "cutoff_hz": [8, 12],
    "sfreq": 250.0,
    "ftype": "bandpass",
}
f1, f2, s_edge, n_edge = filters.show_filter(**bp_filt)

# **Look up reasonable default parameters for the bandpass**

bp_params = filters.check_filter_params(**bp_filt, allow_defaults=True)
bp_params

# **How to generate your own test sinewave data**

sines_specs = {
    "freq_list": [10, 25, 40],
    "amplitude_list": [1.0, 1.0, 1.0],
    "sampling_freq": 250,
}

t, y = filters._sins_test_data(**sines_specs, show_plot=True)
sines_dt = np.array(y, dtype=np.dtype([("sines", float)]))

# **You must apply the filter with ALL the parameters**

# The partial specification with only `ftype`, `cutoff_hz`, and `sfreq` doesn't work ...

try:
    display(bp_filt)
    sines_dt_filt = filters.fir_filter_data(
    sines_dt,
    **bp_filt,
    )
except Exception as fail:
    print(type(fail), fail)

# The complete parameter specification, in any order, works ...

display(bp_params)
sines_dt_filt = filters.fir_filter_data(
    sines_dt,
    **bp_params,
)

# **Plot before and after**

f, ax = plt.subplots(figsize=(18,4))
ax.plot(t, sines_dt, color='gray')
ax.plot(t, sines_dt_filt, color='red');

StarcoderdataJupyterScriptsDedupFiltered