Added offset_t option to SLM(); removed trailing spaces

This commit is contained in:
Casper Jansen 2022-11-10 11:39:44 +01:00
parent e405ab8eea
commit fbb14f475c

View File

@ -27,35 +27,35 @@ class SLM:
Multi-channel Sound Level Meter. Input data: time data with a certain Multi-channel Sound Level Meter. Input data: time data with a certain
sampling frequency. Output: time-weighted (fast/slow) sound pressure sampling frequency. Output: time-weighted (fast/slow) sound pressure
levels in dB(A/C/Z). Possibly in octave bands. levels in dB(A/C/Z). Possibly in octave bands.
""" """
def __init__(self, def __init__(self,
fs, fs,
fbdesigner=None, fbdesigner=None,
tw: TimeWeighting =TimeWeighting.fast, tw: TimeWeighting =TimeWeighting.fast,
fw: FreqWeighting =FreqWeighting.A, fw: FreqWeighting =FreqWeighting.A,
xmin = None, xmin = None,
xmax = None, xmax = None,
include_overall=True, include_overall=True,
level_ref_value=P_REF): level_ref_value=P_REF,
offset_t=0):
""" """
Initialize a sound level meter object. Initialize a sound level meter object.
Args: Args:
fs: Sampling frequency of input data [Hz] fs: Sampling frequency of input data [Hz]
fbdesigner: FilterBankDesigner to use for creating the fbdesigner: FilterBankDesigner to use for creating the
(fractional) octave bank filters. Set this one to None to only do (fractional) octave bank filters. Set this one to None to only do
overalls overalls
fs: Sampling frequency [Hz] fs: Sampling frequency [Hz]
tw: Time Weighting to apply tw: Time Weighting to apply
fw: Frequency weighting to apply fw: Frequency weighting to apply
xmin: Filter designator of lowest band. xmin: Filter designator of lowest band.
xmax: Filter designator of highest band xmax: Filter designator of highest band
include_overall: If true, a non-functioning filter is added which include_overall: If true, a non-functioning filter is added which
is used to compute the overall level. is used to compute the overall level.
level_ref_value: Reference value for computing the levels in dB level_ref_value: Reference value for computing the levels in dB
offset_t: Offset to be added to output time data [s]
""" """
self.fbdesigner = fbdesigner self.fbdesigner = fbdesigner
@ -63,8 +63,8 @@ class SLM:
xmin = fbdesigner.xs[0] xmin = fbdesigner.xs[0]
elif fbdesigner is None: elif fbdesigner is None:
xmin = 0 xmin = 0
if xmax is None and self.fbdesigner is not None: if xmax is None and self.fbdesigner is not None:
xmax = fbdesigner.xs[-1] xmax = fbdesigner.xs[-1]
elif fbdesigner is None: elif fbdesigner is None:
xmax = 0 xmax = 0
@ -74,7 +74,8 @@ class SLM:
nfilters = len(self.xs) nfilters = len(self.xs)
if include_overall: nfilters +=1 if include_overall: nfilters +=1
self.include_overall = include_overall self.include_overall = include_overall
self.offset_t = offset_t
spld = SPLFilterDesigner(fs) spld = SPLFilterDesigner(fs)
if fw == FreqWeighting.A: if fw == FreqWeighting.A:
prefilter = spld.A_Sos_design().flatten() prefilter = spld.A_Sos_design().flatten()
@ -104,7 +105,7 @@ class SLM:
self.nom_txt.append(fbdesigner.nominal_txt(x)) self.nom_txt.append(fbdesigner.nominal_txt(x))
if include_overall: if include_overall:
# Create a unit impulse response filter, every third index equals # Create a unit impulse response filter, every third index equals
# 1, so b0 = 1 and a0 is 1 (by definition) # 1, so b0 = 1 and a0 is 1 (by definition)
# a0 = 1, b0 = 1, rest is zero # a0 = 1, b0 = 1, rest is zero
sos[:,-1] = sos_overall sos[:,-1] = sos_overall
@ -119,7 +120,7 @@ class SLM:
# Downsampling factor, determine from single pole low pass filter time # Downsampling factor, determine from single pole low pass filter time
# constant, such that aliasing is ~ allowed at 20 dB lower value # constant, such that aliasing is ~ allowed at 20 dB lower value
# and # and
dsfac = cppSLM.suggestedDownSamplingFac(fs, tw[0]) dsfac = cppSLM.suggestedDownSamplingFac(fs, tw[0])
if prefilter is not None: if prefilter is not None:
@ -153,14 +154,14 @@ class SLM:
Ncur = levels.shape[0] Ncur = levels.shape[0]
tend = tstart + Ncur / self.fs_slm tend = tstart + Ncur / self.fs_slm
t = np.linspace(tstart, tend, Ncur, endpoint=False) t = np.linspace(tstart, tend, Ncur, endpoint=False) + self.offset_t
self.N += Ncur self.N += Ncur
output = {} output = {}
for i, x in enumerate(self.xs): for i, x in enumerate(self.xs):
# '31.5' to '16k' # '31.5' to '16k'
output[self.nom_txt[i]] = {'t': t, output[self.nom_txt[i]] = {'t': t,
'data': levels[:, [i]], 'data': levels[:, [i]],
'x': x} 'x': x}
if self.include_overall and self.fbdesigner is not None: if self.include_overall and self.fbdesigner is not None: