#!/usr/bin/python3 import numpy as np from lasp import SLM from lasp.filter import SPLFilterDesigner import matplotlib.pyplot as plt def test_cppslm1(): """ Generate a sine wave """ fs = 48000 omg = 2*np.pi*1000 slm = SLM.fromBiquads(fs, 2e-5, 1, 0.125, [1.,0,0,1,0,0]) t = np.linspace(0, 10, 10*fs, endpoint=False) # Input signal with an rms of 1 Pa in_ = np.sin(omg*t)*np.sqrt(2) # Compute overall RMS rms = np.sqrt(np.sum(in_**2)/in_.size) # Compute overall level level = 20*np.log10(rms/2e-5) # Output of SLM out = slm.run(in_) # Output of SLM should be close to theoretical # level, at least for reasonable time constants # (Fast, Slow etc) assert(np.isclose(out[-1,0], level)) def test_cppslm2(): """ Generate a sine wave, now A-weighted """ fs = 48000 omg = 2*np.pi*1000 filt = SPLFilterDesigner(fs).A_Sos_design() slm = SLM.fromBiquads(fs, 2e-5, 0, 0.125, filt.flatten(), [1.,0,0,1,0,0]) t = np.linspace(0, 10, 10*fs, endpoint=False) # Input signal with an rms of 1 Pa in_ = np.sin(omg*t) *np.sqrt(2) # Compute overall RMS rms = np.sqrt(np.sum(in_**2)/in_.size) # Compute overall level level = 20*np.log10(rms/2e-5) # Output of SLM out = slm.run(in_) # Output of SLM should be close to theoretical # level, at least for reasonable time constants # (Fast, Slow etc) assert np.isclose(out[-1,0], level, atol=1e-2) if __name__ == '__main__': test_cppslm1() test_cppslm2() # plt.plot(t,out[:,0]) # plt.close('all') # from scipy.signal import sosfreqz # omg, H = sosfreqz(filt) # freq = omg / (2*np.pi)*fs # plt.figure() # plt.semilogx(freq[1:],20*np.log10(np.abs(H[1:])))