MCPcopy Create free account
hub / github.com/PyTTaMaster/PyTTa / _start_sample_ISO3382

Function _start_sample_ISO3382

pytta/classes/analysis.py:1192–1237  ·  view source on GitHub ↗
(timeSignal, threshold)

Source from the content-addressed store, hash-verified

1190
1191# @njit
1192def _start_sample_ISO3382(timeSignal, threshold) -> np.ndarray:
1193 squaredIR = timeSignal**2
1194 # assume the last 10% of the IR is noise, and calculate its noise level
1195 last10Idx = -int(len(squaredIR)//10)
1196 noiseLevel = np.mean(squaredIR[last10Idx:])
1197 # get the maximum of the signal, that is the assumed IR peak
1198 max_val = np.max(squaredIR)
1199 max_idx = np.argmax(squaredIR)
1200 # check if the SNR is enough to assume that the signal is an IR. If not,
1201 # the signal is probably not an IR, so it starts at sample 1
1202 idxNoShift = np.asarray([max_val < 100*noiseLevel or
1203 max_idx > int(0.9*squaredIR.shape[0])])
1204 # less than 20dB SNR or in the "noisy" part
1205 if idxNoShift.any():
1206 print("noiseLevelCheck: The SNR too bad or this is not an " +
1207 "impulse response.")
1208 return 0
1209 # find the first sample that lies under the given threshold
1210 threshold = abs(threshold)
1211 startSample = 1
1212# # TODO - envelope mar/pdi - check!
1213# if idxNoShift:
1214# print("Something wrong!")
1215# return
1216 # if maximum lies on the first point, then there is no point in searching
1217 # for the beginning of the IR. Just return this position.
1218 if max_idx > 0:
1219 abs_dat = 10*np.log10(squaredIR[:max_idx]) \
1220 - 10.*np.log10(max_val)
1221 thresholdNotOk = True
1222 thresholdShift = 0
1223 while thresholdNotOk:
1224 if len(np.where(abs_dat < (-threshold+thresholdShift))[0]) > 0:
1225 lastBelowThreshold = \
1226 np.where(abs_dat < (-threshold+thresholdShift))[0][-1]
1227 thresholdNotOk = False
1228 else:
1229 thresholdShift += 1
1230 if thresholdShift > 0:
1231 print("_start_sample_ISO3382: 20 dB threshold too high. " +
1232 "Decreasing it.")
1233 if lastBelowThreshold > 0:
1234 startSample = lastBelowThreshold
1235 else:
1236 startSample = 1
1237 return startSample
1238
1239
1240# @njit

Callers 1

_circular_time_shiftFunction · 0.85

Calls 1

meanMethod · 0.80

Tested by

no test coverage detected