spectrogram = np.concatenate((spectrogram, frame_spec), axis=1)
    pos += n_hop
n_frame = spectrogram.shape[1]
t_frame = np.arange(n_frame) * n_hop / fs

# %% plot frames
plt.figure()
plt.imshow(spectrogram,
           aspect='auto',
           origin='lower',
           extent=[0, t_frame[-1], 0, f_frame[-1]],
           vmin=-100,
           vmax=0,
           cmap='viridis')
cbar = plt.colorbar()
plt.xlabel('Time [s]')
plt.ylabel('Frequency [Hz]')
cbar.ax.set_ylabel('Amplitude [dB]')
# plt.axis([0, t[-1], 0, 10000])

# %% spectrograms for different n_win
das.get_spectrogram(fs, wav, 256, win_type='hann')
das.get_spectrogram(fs, wav, 2048, win_type='hann')
das.get_spectrogram(fs, wav, 16384, win_type='hann')

# %% spectrograms for different win_type
das.get_spectrogram(fs, wav, 2048, win_type='boxcar')
das.get_spectrogram(fs, wav, 2048, win_type='hann')
das.get_spectrogram(fs, wav, 2048, win_type='hamming')
das.get_spectrogram(fs, wav, 2048, win_type='blackmanharris')
Example #2
0
for x in xs:
    x_len = x.size
    test_len = int(x_len * test_coef)
    x_train = x[:-test_len]
    x_test = x[-test_len:]
    x_trains.append(x_train)
    x_tests.append(x_test)

# %% feature extraction - spectrograms
x_train = None
x_test = None
y_train = None
y_test = None
for i, (x_train_sig, x_test_sig) in enumerate(
        zip(x_train_sigs, x_test_sigs)):
    __, __, spectrogram = das.get_spectrogram(
            fs, x_train_sig, n_win=256, plot=False)
    x_train_feats = spectrogram.T  # for sklearn
    mask_spectrogram = np.all(x_train_feats < -80, axis=1)
    x_train_feats = x_train_feats[~mask_spectrogram, :60]
    # gen y
    n_samples = x_train_feats.shape[0]
    y_train_targets = np.zeros([n_samples, n_labels])
    y_train_targets[:, i] = 1

    # same for test set
    __, __, spectrogram = das.get_spectrogram(
            fs, x_test_sig, n_win=256, plot=False)
    x_test_feats = spectrogram.T  # for sklearn
    mask_spectrogram = np.all(x_test_feats < -80, axis=1)
    x_test_feats = x_test_feats[~mask_spectrogram, :60]
    n_samples = x_test_feats.shape[0]
Example #3
0
import numpy as np
from matplotlib import pyplot as plt
from scipy.io import wavfile
import os
from scipy import signal as sig
import das

# %% load wav
folder = 'audio/'
filename = 'Mara.wav'
fs, wav = wavfile.read(folder+filename)
wav = wav / 2**15
t = np.arange(wav.shape[0]) / fs
os.system('play '+folder+filename)
plt.plot(t, wav)
das.get_spectrogram(fs, wav)

# %% FIR filters
# low pass
f_l = 150
orders = [5, 11, 51, 101, 501]
plt.figure()
leg = []
for order in orders:
    b_fir = sig.firwin(order, f_l,
                       window='hamming',
                       pass_zero=True,
                       nyq=fs/2)
    w, h_spec = sig.freqz(b_fir, 1)
    plt.subplot(1, 2, 1)
    plt.plot(w/np.pi*fs/2,
Example #4
0
r = 0.99
f0 = 50
w0 = f0 / (fs / 2) * np.pi
b_notch = [1, -2 * np.cos(w0), 1]
a_notch = [1, -2 * r * np.cos(w0), r**2]

w, H_notch = sig.freqz(b_notch, a_notch)
plt.figure()
plt.plot(w / np.pi * fs / 2, 20 * np.log10(np.abs(H_notch)))
plt.grid('on')

# %% filetr
wav_notch = sig.lfilter(b_notch, a_notch, wav_brum)

# %% compare spectrograms
das.get_spectrogram(fs, wav, 256)
das.get_spectrogram(fs, wav_notch, 256)

# %% compare spectrums
f, wav_spec = das.get_spectrum(fs, wav_brum)
f, wav_notch_spec = das.get_spectrum(fs, wav_notch)

plt.figure()
plt.plot(f, wav_spec)
plt.plot(f, wav_notch_spec)
plt.grid()

# %% play
wavfile.write('audio/Pato_notch.wav', fs,
              np.array(wav_notch * 2**15, dtype='int16'))
os.system('play audio/Pato_notch.wav')
Example #5
0
    b, a = sig.iirfilter(order, [f_l, f_h],
                         btype='bandpass', ftype='butter', fs=fs)
    b_frame.append(b)
    a_frame.append(a)

# %% window + wah
win = sig.get_window('hann', n_win)
t_win = n_win / fs
pos = 0
i = 0
wav_wah = np.zeros(wav_pad.size)
while pos <= wav_pad.size - n_win:
    frame = wav_pad[pos : pos+n_win]
    frame = frame * win
    frame_wah = sig.lfilter(b_frame[i], a_frame[i], frame)
    wav_wah[pos : pos+n_win] += frame_wah * win
    pos += n_hop
    i += 1

# %% wav write and play
wav_wah = 0.1*wav_pad + 0.9*wav_wah
wav_wah = das.normalise(wav_wah, -3)
wav_wah_int16 = wav_wah * 2**15
wav_wah_int16 = wav_wah_int16.astype('int16')
wavfile.write(path+file_name+'_wah.wav', fs, wav_wah_int16)
os.system('play '+path+file_name+'_wah.wav')

# %% plot spectrogram
__ = das.get_spectrogram(fs, wav_pad, 2048, win_type='hann', plot=True)
__ = das.get_spectrogram(fs, wav_wah, 2048, win_type='hann', plot=True)