
from scipy.signal import butter, lfilter, decimate

def anti_alias(data, sample_rate, max_frequency):
    nyquist_rate = 2 * max_frequency

    if sample_rate > nyquist_rate:
        filtered_data = lowpass_filter(data, max_frequency, sample_rate)

        downsample_factor = int(sample_rate / nyquist_rate)

        filtered_data = decimate(filtered_data, downsample_factor)
        sample_rate = sample_rate // downsample_factor
    else:
        filtered_data = data

    return filtered_data, sample_rate

# These originally came from https://scipy.github.io/old-wiki/pages/Cookbook/ButterworthBandpass,
# but they've been copied around the internet so many times that ChatGPT now produces them verbatim.
def butter_bandpass(lowcut, highcut, fs, order=5):
    nyquist = 0.5 * fs
    low = lowcut / nyquist
    high = highcut / nyquist
    b, a = butter(order, [low, high], btype='band')
    return b, a

def bandpass_filter(data, lowcut, highcut, fs, order=5):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    y = lfilter(b, a, data)
    return y

def butter_lowpass(cutoff, fs, order=5):
    nyquist = 0.5 * fs
    normal_cutoff = cutoff / nyquist
    b, a = butter(order, normal_cutoff, btype='low', analog=False)
    return b, a

def lowpass_filter(data, cutoff, fs, order=5):
    b, a = butter_lowpass(cutoff, fs, order=order)
    y = lfilter(b, a, data)
    return y