Skip to content

Instantly share code, notes, and snippets.

View keunwoochoi's full-sized avatar

Keunwoo Choi keunwoochoi

View GitHub Profile
@keunwoochoi
keunwoochoi / istft-torch.py
Created March 14, 2019 05:23
temporary - inverse STFT
def istft(stft_matrix, hop_length=None, win_length=None, window='hann',
center=True, normalized=False, onesided=True, length=None):
"""stft_matrix = (batch, freq, time, complex)
All based on librosa
- http://librosa.github.io/librosa/_modules/librosa/core/spectrum.html#istft
What's missing?
- normalize by sum of squared window --> do we need it here?
Actually the result is ok by simply dividing y by 2.
"""
@keunwoochoi
keunwoochoi / hpss_torch.py
Created March 14, 2019 00:14
median-filtering based harmonic-percussive (drum) source separation in Pytorch
import torch
import torch.nn.functional as F
EPS = 1e-7
def _enhance_either_hpss(x_padded, out, kernel_size, power, which, offset):
"""x_padded: one that median filtering can be directly applied
kernel_size = int
dim: either 2 (freq-axis) or 3 (time-axis)
which: str, either "harm" or "perc"
import torch
def STFT(src, n_fft, hop_length=None, window=None, **kwargs):
"""A wrapper for torch.stft with some preset parameters.
Returned value keeps both real and imageinary parts.
For STFT magnitude, see spectrogram
"""
class Net(nn.Module):
def __init__(self, src):
super(net, self).__init__()
self.src = src
self.a = a
self.conv1 = nn.Conv1d(1, 2, 3, 4,..)
def _fwd_1(self, x):
x = self.conv1(x)
x = x + self.a
@keunwoochoi
keunwoochoi / pseudo_cqt_pytorch.py
Last active March 22, 2023 15:08
To compute pseudo CQT (Constant-Q-transform using STFT) on Pytorch.
cqt_filter_fft = librosa.constantq.__cqt_filter_fft
class PseudoCqt():
"""A class to compute pseudo-CQT with Pytorch.
Written by Keunwoo Choi
API (+implementations) follows librosa (https://librosa.github.io/librosa/generated/librosa.core.pseudo_cqt.html)
Usage:
src, _ = librosa.load(filename)
src_tensor = torch.tensor(src)
@keunwoochoi
keunwoochoi / speech.py
Created March 28, 2018 12:47
synthesize_formant_speech.py
"""
Code converted by Keunwoo from https://ccrma.stanford.edu/~jos/fp/Formant_Filtering_Example.html (JOS)
"""
import numpy as np
import scipy.signal as signal
import librosa
def gen_speech(F, sig=None, filename='speech'):
nsecs = len(F)
@keunwoochoi
keunwoochoi / kapre_debug.py
Created December 8, 2017 14:56
to debug kapre's spectrogram. copied from ipython notebook.
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import numpy as np
import librosa
import keras
from keras import backend as K
print(keras.__version__)
print('Keras backend: ', keras.backend._BACKEND, ' and image dim ordering: ', keras.backend.image_dim_ordering())
@keunwoochoi
keunwoochoi / BN_test_time.py
Last active November 30, 2017 15:33
Simple batch normalization prediction fix for Keras 1.x weights
import keras.backend as K
# Load the weights
# If there are more than 1 BN, you might wanna have a proper layer
moving_mean = f['batchnormalization_1_running_mean']
moving_std = f['batchnormalization_1_running_std']
beta = f['batchnormalization_1_beta']
gamma = f['batchnormalization_2_beta']
# BE CAREFUL! Keras 1 stores std
@keunwoochoi
keunwoochoi / unet.py
Created October 11, 2017 00:49
Keras-unet
import keras
from keras import backend as K
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers import Input, Dense, Activation
from keras.layers import concatenate # functional interface
from keras.models import Model
from keras.layers.advanced_activations import LeakyReLU
N_INPUT = 512
@keunwoochoi
keunwoochoi / get_ismir2017_paper.py
Created October 6, 2017 02:43
Run it to get all ismir 2017 papers.
links = '''https://ismir2017.smcnus.org/wp-content/uploads/2017/10/126_Paper.pdf
https://ismir2017.smcnus.org/wp-content/uploads/2017/10/79_Paper.pdf
https://ismir2017.smcnus.org/wp-content/uploads/2017/10/89_Paper.pdf
https://ismir2017.smcnus.org/wp-content/uploads/2017/10/119_Paper.pdf
https://ismir2017.smcnus.org/wp-content/uploads/2017/10/51_Paper.pdf
https://ismir2017.smcnus.org/wp-content/uploads/2017/10/85_Paper.pdf
https://ismir2017.smcnus.org/wp-content/uploads/2017/10/164_Paper.pdf
https://ismir2017.smcnus.org/wp-content/uploads/2017/10/220_Paper.pdf
https://ismir2017.smcnus.org/wp-content/uploads/2017/10/172_Paper.pdf
https://ismir2017.smcnus.org/wp-content/uploads/2017/10/180_Paper.pdf