2022-09-22 08:11:47 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Created on Mon Jan 15 19:45:33 2018
|
|
|
|
|
|
|
|
@author: anne
|
|
|
|
"""
|
|
|
|
import numpy as np
|
|
|
|
from lasp import AvPowerSpectra, Window
|
|
|
|
|
|
|
|
def test_aps1():
|
|
|
|
|
|
|
|
nfft = 16384
|
|
|
|
fs = 48000
|
|
|
|
tend = 10
|
|
|
|
t = np.linspace(0, (tend*fs)//fs, int(fs*tend), endpoint=False)
|
|
|
|
|
|
|
|
w = Window.WindowType.Hann
|
|
|
|
# w = Window.WindowType.Rectangular
|
|
|
|
aps = AvPowerSpectra(nfft, w, 50, -1)
|
|
|
|
|
|
|
|
sig = np.random.randn(int(fs*tend))
|
|
|
|
|
2022-10-11 12:50:44 +00:00
|
|
|
cps = aps.compute(sig[:,None])
|
2022-09-22 08:11:47 +00:00
|
|
|
|
|
|
|
pow1 = np.sum(sig**2)/sig.size
|
|
|
|
pow2 = np.sum((cps[:,0,0]).real)
|
|
|
|
|
2022-10-05 11:39:45 +00:00
|
|
|
# Check for Parseval
|
2022-09-22 08:11:47 +00:00
|
|
|
assert np.isclose(pow2 - pow1,0, atol=1e-2)
|
|
|
|
|
2022-10-05 11:39:45 +00:00
|
|
|
def test_aps2():
|
|
|
|
"""
|
|
|
|
Test whether we are able to estimate a simple transfer function of a gain
|
|
|
|
of 2 between two channels
|
|
|
|
"""
|
|
|
|
|
|
|
|
nfft = 16384
|
|
|
|
fs = 48000
|
|
|
|
tend = 10
|
|
|
|
t = np.linspace(0, (tend*fs)//fs, int(fs*tend), endpoint=False)
|
|
|
|
|
|
|
|
# w = Window.WindowType.Hann
|
|
|
|
w = Window.WindowType.Rectangular
|
|
|
|
aps = AvPowerSpectra(nfft, w, 50, -1)
|
|
|
|
|
|
|
|
sig1 = np.random.randn(int(fs*tend))
|
|
|
|
sig2 = 2*sig1
|
|
|
|
|
|
|
|
sig = np.concatenate((sig1[None,:], sig2[None,:])).T
|
|
|
|
|
|
|
|
cps = aps.compute(sig)
|
|
|
|
|
|
|
|
H = cps[:,0,1]/cps[:,0,0]
|
|
|
|
|
|
|
|
# Check if transfer function is obtained
|
|
|
|
assert np.all(np.isclose(H,2))
|
|
|
|
|
2022-09-22 08:11:47 +00:00
|
|
|
if __name__ == '__main__':
|
2022-10-05 11:39:45 +00:00
|
|
|
test_aps1()
|