Python High Pass Filter

前端 未结 3 1396
梦如初夏
梦如初夏 2020-12-14 11:49

I implemented an high pass filter in python using this code:

from scipy.signal import butter, filtfilt
import numpy as np

def butter_highpass(cutoff, fs, or         


        
3条回答
  •  旧时难觅i
    2020-12-14 12:03

    I hope this can help you:

    import numpy as np
    import pandas as pd
    from scipy import signal
    import matplotlib.pyplot as plt
    def sine_generator(fs, sinefreq, duration):
        T = duration
        nsamples = fs * T
        w = 2. * np.pi * sinefreq
        t_sine = np.linspace(0, T, nsamples, endpoint=False)
        y_sine = np.sin(w * t_sine)
        result = pd.DataFrame({ 
            'data' : y_sine} ,index=t_sine)
        return result
    
    def butter_highpass(cutoff, fs, order=5):
        nyq = 0.5 * fs
        normal_cutoff = cutoff / nyq
        b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
        return b, a
    
    def butter_highpass_filter(data, cutoff, fs, order=5):
        b, a = butter_highpass(cutoff, fs, order=order)
        y = signal.filtfilt(b, a, data)
        return y
    
    fps = 30
    sine_fq = 10 #Hz
    duration = 10 #seconds
    sine_5Hz = sine_generator(fps,sine_fq,duration)
    sine_fq = 1 #Hz
    duration = 10 #seconds
    sine_1Hz = sine_generator(fps,sine_fq,duration)
    
    sine = sine_5Hz + sine_1Hz
    
    filtered_sine = butter_highpass_filter(sine.data,10,fps)
    
    plt.figure(figsize=(20,10))
    plt.subplot(211)
    plt.plot(range(len(sine)),sine)
    plt.title('generated signal')
    plt.subplot(212)
    plt.plot(range(len(filtered_sine)),filtered_sine)
    plt.title('filtered signal')
    plt.show()
    

提交回复
热议问题