text_plain_output_1.png
from numba import njit, jit, cuda, guvectorize

from scipy.signal import butter, filtfilt, sosfiltfilt

from scipy.signal import cwt, ricker

from scipy.signal import spectrogram

import glob

import librosa

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pathlib

import torch

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 numba import njit, jit, cuda, guvectorize
@njit(nogil=True)
def min_max_scaler(wave):
    for i in range(len(wave)):
        wave[i] = (wave[i] - min(wave[i])) / (max(wave[i]) - min(wave[i]))
        wave[i] = 2 * wave[i] - 1
    return wave

wave1 = min_max_scaler(wave)

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)

from scipy.signal import spectrogram
for i in range(len(wave)):
    f, t, Sxx = spectrogram(wave1[i], fs=10)
    plt.pcolormesh(t, f, Sxx, shading='gouraud')

def wrapper_plot(m):
    m()

stacked = []
for j in range(len(wave1)):
    melspec = librosa.feature.melspectrogram(wave1[j], sr=4096, n_mels=128, fmin=21.83, fmax=2048)
    melspec = librosa.power_to_db(melspec)
    melspec = melspec.transpose((1, 0))
    stacked.append(melspec)
image = np.vstack(stacked)

wave.shape

cwt(wave[0], signal.ricker, np.arange(1, 300))