def test_hamming(): """Test window functions""" wlen = 257 hopfrac = .25 window = hamming(wlen, hop=hopfrac, synth=True) assert np.allclose(tcola(window, hopfrac), 1) window = hamming(wlen, nchan=wlen, synth=True) assert np.allclose(fcola(window, wlen), 1)
def test_hamming(): """Test window functions""" wlen = 256 hopfrac = .25 wind = hamming(wlen, hop=hopfrac, synth=True) amp = tcola(wind, hopfrac) assert amp == 1
def test_ssf(): powerspec = stpowspec(WELCOME, hamming(int(.025 * SR)), .5, 512) wts = Gammatone(SR, 40).gammawgt(512) mask = ssf(powerspec @ wts, .4) assert mask.shape[0] == powerspec.shape[0] assert mask.shape[1] == wts.shape[1]
def test_mfcc(): # TODO: need to add proper testing. nfft = 512 nmel = 40 melbank = MelFreq(sr, nfft, nmel) window_length = 0.032 wind = hamming(int(window_length*sr)) hop = .25 mfcc = stmfcc(sig, wind, hop, nfft, melbank) return mfcc
def test_ssf(): sig, sr = audioread( '/home/xyy/Documents/SSF_Package_11k/sb01_Reverb0P5sec.in.11k.wav') wlen = .05 hop = .2 nfft = 1024 wind = hamming(int(wlen * sr), hop=hop, synth=True) enhancer = SSFEnhancer(sr, wind, hop, nfft) sigsynth = enhancer(sig) audiowrite(sigsynth, sr, 'test-ssf.wav')
def test_stft(): """Test STFT and iSTFT.""" sig = WELCOME for zp in (True, False): for wlen in (0.02, 0.025, 0.032, 0.5): for hop in (0.25, 0.5): wind = hamming(int(wlen * SR), hop=hop, synth=True) nfft = nextpow2(len(wind)) spec = stft(sig, wind, hop, nfft, synth=True, zphase=zp) sigsynth = istft(spec, wind, hop, nfft, zphase=zp) assert np.allclose(sig, sigsynth[:len(sig)])
def test_stproc(): """Test the stana-ola framework.""" sr = 16000 sig = np.ones(10000) hop = .5 wind = hamming(512, hop=hop, synth=True) frames = stana(sig, wind, hop, synth=True) sigsynth = ola(frames, wind, hop) assert np.allclose(sig, sigsynth[:len(sig)])
def __init__(self, sr, hop, nfft, apply_fit: bool = True, apply_predict: bool = True): super(SSFPreprocessor, self).__init__() window = hamming(nfft, hop=hop) self._apply_fit = apply_fit self._apply_predict = apply_predict self.enhancer = SSFEnhancer(sr, window, hop, nfft) self.lambda_lp = 1e-3
def test_fbs(): """Test filterbank synthesis.""" window_length = 0.02 window_size = int(window_length * sr) window = hamming(window_size, nchan=window_size, synth=True) synth = np.zeros(sig.shape, dtype=np.complex_) for kk in range(window_size): wk = 2 * np.pi * (kk / window_size) band = signal.lfilter( window * np.exp(1j * wk * np.arange(window_size)), 1, sig) synth[:] = synth[:] + band assert np.allclose(synth.real, sig) return
def test_pncc(): wlen = .025 hop = .01 nfft = 1024 wind = hamming(int(wlen * SR)) powerspec = stpowspec(WELCOME, wind, int(hop * SR), nfft, synth=False) gtbank = Gammatone(SR, 40) wts = gtbank.gammawgt(nfft, powernorm=True, squared=True) gammaspec = powerspec @ wts g1 = powerspec @ wts coef = pncc(gammaspec, tempmask=True) for ii in range(10): assert np.allclose(g1, gammaspec) assert np.allclose(coef, pncc(gammaspec, tempmask=True)) return coef
def __init__(self, sr, hop, nfft, gaussian_sigma=None, high_freq=False, apply_fit: bool = True, apply_predict: bool = True): super(ASNRWiener, self).__init__() self.sr = sr self.window = hamming(nfft, hop=hop) self.hop = hop self.nfft = nfft self._apply_fit = apply_fit self._apply_predict = apply_predict self.gaussian_sigma = gaussian_sigma self.lpc_order = 12 self.high_freq = high_freq
def test_invpnspec(): # TODO wlen = .025 hop = .01 nfft = 1024 wind = hamming(int(wlen * SR)) spec = stft(WELCOME, wind, hop, nfft, synth=True, zphase=True) pspec = spec.real**2 + spec.imag**2 gtbank = Gammatone(SR, 40) wts = gtbank.gammawgt(nfft, powernorm=True, squared=True) gammaspec = pspec @ wts mask40 = pncc(gammaspec, tempmask=True, synth=True) maskfull = invspec(mask40, wts) sigsynth = istft((maskfull**.5) * spec, wind, hop, nfft, zphase=True) return
def test_griffin_lim(): """Test STFT and iSTFT.""" sig = WELCOME wlen = .032 hop = 128 wsize = int(wlen * SR) # modified Hamming window a = .54 b = -.46 phi = np.pi / wsize wind = 2 / (4 * a**2 + 2 * b**2)**.5 * ( a + b * np.cos(2 * np.pi * np.arange(wsize) / wsize + phi)) wind = hamming(wsize, hop=hop) nfft = nextpow2(len(wind)) spec = stft(sig, wind, hop, nfft) magspec, _ = magphasor(spec) sig, err = griffin_lim(magspec, wind, hop, nfft, 100, init=0) assert all(e1 >= e2 for (e1, e2) in zip(err[:-1], err[1:]))
def __init__(self, srate, wlen=.0256, frate=100, nfft=512, nmel=40): """Initialize Mel filterbanks.""" super(Melspec, self).__init__() self.sr = srate self.melbank = MelFreq(srate, nfft, nmel, unity=True) self.nmel = nmel hop = int(srate / frate) wind = hamming(int(wlen * srate), hop=hop) def _melspec(sig): """Compute melspec after cepstral-mean-norm.""" pspec = stpowspec(sig, wind, hop, nfft) melspec = pspec @ self.melbank.wgts smallpower = melspec < 10**(-8) # -80dB power floor melspec[~smallpower] = np.log(melspec[~smallpower]) melspec[smallpower] = np.log(10**(-8)) mfcc = dct(melspec, norm='ortho') melspec = idct(mfcc - mfcc.mean(axis=0), norm='ortho') return melspec self._melspec = _melspec
"""Test enhancement functions.""" from audlib.quickstart import welcome from audlib.sig.window import hamming WELCOME, SR = welcome() HOP = .25 WIND = hamming(SR * .025, HOP, synth=True) def test_SSFEnhancer(): from audlib.enhance import SSFEnhancer enhancer = SSFEnhancer(SR, WIND, HOP, 512) sigssf = enhancer(WELCOME, .4) # sounds okay return if __name__ == '__main__': test_SSFEnhancer()
import os import pytest from audlib.data.enhance import RandSample, Additive from audlib.sig.window import hamming # Pre-determined transform to be applied on signal sr = 16000 window_length = 0.032 hopfrac = 0.25 wind = hamming(int(window_length * sr), hop=hopfrac) nfft = 512 def test_randsamp(): """Test for random sampling class.""" #TODO pass def test_additive(): """Test for additive noise.""" #TODO pass if __name__ == '__main__': test_randsamp() test_additive()