예제 #1
0
@author: Branislav Gerazov
"""
from __future__ import division
import numpy as np
from matplotlib import pyplot as plt
from math import pi
from scipy.io import wavfile
from scipy import signal as sig
import das
from scikits.talkbox import lpc

#%% load wav
fs, wav = wavfile.read('audio/glas_aaa.wav')
wav = wav / 2**15
# wav = wav * sig.hamming(wav.size)
f, wav_spec = das.get_spectrum(wav, fs)

#%% get LPC parameters
a_lp, e, k = lpc(wav, 25)
b_inv = np.concatenate(([0], -a_lp[1:]))
wav_est = sig.lfilter(b_inv, 1, wav)
wav_err = wav - wav_est
G = e
f, err_spec = das.get_spectrum(wav_err, fs)

#%% plot
#plt.figure()
#plt.plot(wav)
#plt.plot(est_wav)
#plt.figure()
#plt.plot(err)
예제 #2
0
@author: Branislav Gerazov
"""

import numpy as np
from matplotlib import pyplot as plt
from scipy.io import wavfile
from scipy import signal as sig
import os
import das

# %% load audio
fs, wav = wavfile.read('audio/zvona2.wav')
os.system('play audio/zvona2.wav')
wav = wav / 2**15
t = np.arange(0, wav.size / fs, 1 / fs)
f, wav_amp = das.get_spectrum(fs, wav, plot=True)

# %% aliasing
# wavfile.write('audio/Zvona_alias.wav', fs/4,
#               np.array(wav[0:-1:4] * 2**15, dtype='int16'))

# %% extract spectrogram
# define window
# t_win = .050  # ms
# n_win = int(t_win * fs)
n_win = 2048
win = sig.get_window('hamming', n_win)
n_half = n_win // 2
n_hop = n_half
# pad signal
pad = np.zeros(n_half)
예제 #3
0
plt.legend(leg)

# %% filter signal
b_fir = sig.firwin(501, f_l,
                   window='hamming', pass_zero=True, nyq=fs/2)
wav_lp_fir = sig.lfilter(b_fir, 1, wav)
wav_lp_filtfilt = sig.filtfilt(b_fir, 1, wav)

# %% plot
plt.figure()
plt.plot(t, wav)
plt.plot(t, wav_lp_fir)
plt.plot(t, wav_lp_filtfilt)

# %% plot spectrums
f, wav_spec = das.get_spectrum(fs, wav)
f, wav_lp_spec = das.get_spectrum(fs, wav_lp_fir)
f, wav_lp_spec_filtfilt = das.get_spectrum(fs, wav_lp_filtfilt)

plt.figure()
plt.plot(f, wav_spec)
plt.plot(f, wav_lp_spec)
plt.plot(f, wav_lp_spec_filtfilt)
plt.xscale('log')
plt.axis((20, 16000, -80, 0))
plt.grid()

# %%
orders = [3, 5, 7, 11]
leg = []
plt.figure()
예제 #4
0
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')
plt.plot(w, H_lp, lw=4, alpha=.5)
plt.plot(w_2p, H_lp_2p, lw=2, alpha=.5)

# %% calculate impulse response using DFT
h_lp = fftp.ifft(H_lp, n_fft)
h_lp = fftp.fftshift(h_lp)

# %% window it
n = 128 + 1
n_h = int((n - 1) / 2)
n_ffth = int(n_fft / 2)
win = sig.get_window('boxcar', n)
n_win = np.arange(n_ffth - n_h, n_ffth + n_h + 1, dtype='int')
h_rect = h_lp[n_win] * win

# %% plot designed filters
plt.figure()
plt.subplot(211)
plt.plot(h_lp, lw=2, alpha=.5)
plt.plot(n_win, h_rect, lw=1, alpha=.9)
plt.grid()
plt.axis([n_ffth - 1.5 * n_h, n_ffth + 1.5 * n_h, -0.1, 0.25])

plt.subplot(212)
f, h_lp_spec = das.get_spectrum(fs, h_lp, n_fft)
f, h_rect_spec = das.get_spectrum(fs, h_rect, n_fft)
plt.plot(f, h_lp_spec - np.max(h_lp_spec))
plt.plot(f, h_rect_spec - np.max(h_rect_spec))
plt.grid()
plt.axis([0, 10000, -90, 10])