text_plain_output_1.png image_output_1.png
from scipy.signal import butter, filtfilt, sosfiltfilt

import glob

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pathlib

import torch

import glob
import pathlib
head = pathlib.Path('../input/g2net-gravitational-wave-detection')
train_files = sorted(glob.glob('../input/g2net-gravitational-wave-detection/train/*/*/*/*.npy'))

wave = np.load(train_files[0])

from scipy.signal import butter, filtfilt, sosfiltfilt
T = 2
fs = 2048.0
cutoff = 2.5
nyq = 0.5 * fs
order = 3
n = int(T * fs)
normal_cutoff = cutoff / nyq

normal_cutoff = (21.83 / fs, 500 / fs)
def butter_bandpass_filter(data, normal_cutoff, fs, order=2):
    b, a = butter(order, normal_cutoff, btype='bandpass', analog=False)
    y = filtfilt(b, a, data)
    return y

def butter_bandpass_filter(data, low, high, fs, order):
    sos = butter(order, [low, high], btype='bandpass', output='sos', fs=fs)
    normalization = np.sqrt((high - low) / (fs / 2))
    return sosfiltfilt(sos, data) / normalization

def butter_lowpass_filter(data, normal_cutoff, fs, order):
    b, a = butter(order, normal_cutoff, btype='lowpass', analog=False)
    y = filtfilt(b, a, data)
    return y

data = torch.from_numpy(wave)
y = butter_bandpass_filter(data, 21.83, 500, fs, 4)

plt.figure(dpi=120)
plt.plot(range(len(wave[0])), y[0])