|
import os, requests |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
import librosa |
|
import noisereduce as nr |
|
from scipy.signal import butter, lfilter |
|
|
|
|
|
def bandpass_filter(data, lowcut, highcut, sr, order=5): |
|
nyquist = 0.5 * sr |
|
low = lowcut / nyquist |
|
high = highcut / nyquist |
|
b, a = butter(order, [low, high], btype='band') |
|
y = lfilter(b, a, data) |
|
return y |
|
|
|
|
|
def plot_mel(sr, x): |
|
mel_spec = librosa.feature.melspectrogram(y=x, sr=sr, n_mels=128, fmax=10000) |
|
mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max) |
|
mel_spec_db = (mel_spec_db - mel_spec_db.min()) / (mel_spec_db.max() - mel_spec_db.min()) |
|
|
|
fig, ax = plt.subplots(nrows=1, ncols=1, sharex=True) |
|
librosa.display.specshow(mel_spec_db, sr=sr, x_axis='time', y_axis='mel', fmin = 0, fmax=10000, ax = ax) |
|
return fig |
|
|
|
|
|
def plot_wave(sr, x): |
|
ry = nr.reduce_noise(y=x, sr=sr) |
|
fig, ax = plt.subplots(2, 1, figsize=(12, 8)) |
|
|
|
|
|
librosa.display.waveshow(x, sr=sr, ax=ax[0]) |
|
ax[0].set(title='Original Waveform') |
|
ax[0].set_xlabel('Time (s)') |
|
ax[0].set_ylabel('Amplitude') |
|
|
|
|
|
librosa.display.waveshow(ry, sr=sr, ax=ax[1]) |
|
ax[1].set(title='Noise Reduced Waveform') |
|
ax[1].set_xlabel('Time (s)') |
|
ax[1].set_ylabel('Amplitude') |
|
|
|
plt.tight_layout() |
|
return fig |
|
|
|
|
|
def download_model(url, model_path): |
|
response = requests.get(url) |
|
response.raise_for_status() |
|
with open(model_path, 'wb') as f: |
|
f.write(response.content) |