def position_scan_regions(sig, detect_mask, scan_len, detect_scan_olap): detect_segments = segs_list_from_signal(detect_mask) assert (utils_sig.is_array(sig)) assert (utils_sig.is_array(detect_mask)) assert ((scan_len % 2) == 1) half_seg_len = (scan_len - 1) / 2 fsig = condition_signal_for_emph_scanning(sig) res = [] for dseg in detect_segments: dchunk = fsig[dseg['st']:dseg['end']] max_idx = numpy.argmax(dchunk) #assert(len(max_idx) == 1) # check that we have only 1 maximum value in the result, otherwise it is VERY strange scan_set_st = int(dseg['st'] + max_idx - half_seg_len) scan_set_end = int(dseg['st'] + max_idx + half_seg_len) scan_seg = {'st': scan_set_st, 'end': scan_set_end} # make sure that detection segment and corresponding scan-segment overlap on at least 50% if get_seg_overlap(dseg, scan_seg) < detect_scan_olap: scan_seg = adjust_seg_overlap(dseg, scan_seg, len(sig), detect_scan_olap) if scan_seg is not None: res.append(scan_seg) return res
def update_detection_results(mask, samplerate, detect_hysteresis, merge_threshold, min_len, max_len): assert (utils_sig.is_array(mask)) hyst_step = int(numpy.round(detect_hysteresis * samplerate)) merge_step = int(numpy.round(merge_threshold * samplerate)) min_keep_len = int(numpy.round(min_len * samplerate)) max_keep_len = int(numpy.round(max_len * samplerate)) # MY_DBG #print("hyst_step = {0} merge_step = {1} ...".format(hyst_step, merge_step)) sig_len = len(mask) idx = 0 result = numpy.copy(mask) while (idx < sig_len): if (mask[idx] > 0.0): result[idx:idx + hyst_step] = 1.0 idx += hyst_step continue idx += 1 detect_segs = segs_list_from_signal(result) detect_segs = merge_segs(detect_segs, min_len=min_keep_len, max_len=max_keep_len, merge_thr=merge_step) result = segs_list_to_signal(detect_segs, len(result)) return result
def remove_silence(sig, sil_thr=0.001): assert (utils_sig.is_vector(sig) or utils_sig.is_array(sig)) tmp = sig.squeeze() tmp = tmp[numpy.abs(tmp) >= sil_thr] return utils_sig.preserve_shape(tmp, sig)
def plot_emphasis_scan_segs(signal, detect, scan_segs, samplerate, filename=None): assert (utils_sig.is_array(signal)) assert (utils_sig.is_array(detect)) sig_time = numpy.arange(len(signal)) / samplerate sig_cond = utils_emph.condition_signal_for_emph_scanning(signal) plt_y = [signal, sig_cond, detect] plt_x = [sig_time, sig_time, sig_time] (seg_x, seg_y) = get_scan_seg_plots(scan_segs, samplerate, len(signal)) plt_y = plt_y + seg_y plt_x = plt_x + seg_x return plot_curves(plt_y, plt_x, saveto=filename)
def segs_list_from_signal(sig): idx = 0 assert (utils_sig.is_array(sig)) res = [] while (idx < len(sig)): if (sig[idx] > 0): st = idx while (sig[idx] > 0): idx += 1 end = idx res.append({'st': st, 'end': end}) continue idx += 1 return res
def perform_mvn_norm(sig, skip_zeros=False): assert utils_sig.is_vector(sig) or utils_sig.is_array(sig) uv_mask = (sig == 0.0) if skip_zeros: tmp_sig = remove_silence(sig, 0.0) mean = numpy.mean(tmp_sig) std = numpy.std(tmp_sig) result = (sig - mean) / std result[uv_mask] = 0.0 return result else: mean = numpy.mean(sig) std = numpy.std(sig) result = (sig - mean) / std result[uv_mask] = 0.0 return result