def telemetry(): global telemetry_df data = json.loads(request.data) telemetry_df = utils.prepare_input_data( telemetry_df, data, NUMBER_OF_LAST_N_SECONDS_TO_BE_DISPLAYED, MOVING_WINDOW_SIZE) return "200"
def encode_lookup(x, ord, lag, tab, trustme=False): """ Extract and encode ordinal patterns using the 'lookup' algorithm. Parameters ---------- x : array_like Time series data to be encoded. Can be multi-dimensional, and must be convertible to an ndarray of data type 'double'. The input array is flattened prior to encoding. ord : int Order of the encoding, an integer between 2 and 10. lag : int Time lag used for creating embedding vectors. Must be a positive integer. tab : ndarray of uint64 A lookup table containing factorial(ord) * ord pattern codes, as returned by the `create_lookup_table` function. Note that custom mappings are NOT supported, and will lead to undefined behaviour. trustme : bool, optional If set to True, the function arguments are not validated before processing. This will speed up the calculation. Returns ------- pat : ndarray of uint64 One-dimensional array, with each value representing an ordinal pattern. It holds that ``pat.size == x.size - (ord - 1) * lag``. """ if not trustme: x = _utils.prepare_input_data(x, ord, lag) if ord > 10: raise ValueError("lookup algorithm does not support ord > 10") _utils.check_lookup_table(tab, ord) n_rel = ord - 1 n_pat = x.size - n_rel * lag pat = _np.zeros(n_pat, dtype=_np.uint64) for i in range(0, lag): pat[i] = _encode_pattern(x[i:i + ord * lag:lag]) next_val = x[n_rel * lag:] ranks = _np.zeros(next_val.shape, _np.uint8) for i in range(0, n_rel): ranks += x[i * lag:(i - n_rel) * lag] > next_val for i in range(lag, n_pat): pat[i] = tab[pat[i - lag], ranks[i]] return pat
def main(args): # Read command line args audio_file = args.audio_file_path # Create the ArmNN inference runner network = ArmnnNetworkExecutor(args.model_file_path, args.preferred_backends) # Specify model specific audio data requirements audio_capture_params = AudioCaptureParams(dtype=np.float32, overlap=31712, min_samples=47712, sampling_freq=16000, mono=True) buffer = capture_audio(audio_file, audio_capture_params) # Extract features and create the preprocessor mfcc_params = MFCCParams(sampling_freq=16000, num_fbank_bins=128, mel_lo_freq=0, mel_hi_freq=8000, num_mfcc_feats=13, frame_len=512, use_htk_method=False, n_fft=512) wmfcc = Wav2LetterMFCC(mfcc_params) preprocessor = W2LAudioPreprocessor(wmfcc, model_input_size=296, stride=160) current_r_context = "" is_first_window = True print("Processing Audio Frames...") for audio_data in buffer: # Prepare the input Tensors input_data = prepare_input_data( audio_data, network.get_data_type(), network.get_input_quantization_scale(0), network.get_input_quantization_offset(0), preprocessor) # Run inference output_result = network.run([input_data]) # Slice and Decode the text, and store the right context current_r_context, text = decode_text(is_first_window, labels, output_result) is_first_window = False display_text(text) print(current_r_context, flush=True)
def encode_overlap_mp(x, ord, lag, trustme=False): """ Extract and encode ordinal patterns using the 'overlap_mp' algorithm. Parameters ---------- x : array_like Time series data to be encoded. Can be multi-dimensional, and must be convertible to an ndarray of data type 'double'. The input array is flattened prior to encoding. ord : int Order of the encoding, an integer between 2 and 255. lag : int Time lag used for creating embedding vectors. Must be a positive integer. trustme : bool, optional If set to True, the function arguments are not validated before processing. This will speed up the calculation. Returns ------- pat : ndarray of uint64 Two-dimensional array, with each vector along the second axis representing an ordinal pattern. It holds that ``pat.shape[0] == x.size - (ord - 1) * lag``, whereas ``pat.shape[1]`` depends on the pattern order. """ if not trustme: x = _utils.prepare_input_data(x, ord, lag) if ord > 255: raise ValueError("overlap_mp algorithm does not support ord > 255") n_pat = x.size - (ord - 1) * lag width = _utils.pattern_uint64_width(ord) n_pat *= width pat = _np.empty(n_pat, dtype=_np.uint64) ret = _lib.ordpat_encode_overlap_mp(x, x.size, pat, pat.size, ord, lag) if not ret == 0: _raise_api_error(ret) pat = pat.reshape((-1, width)) return pat
def encode_plain(x, ord, lag, trustme=False): """ Extract and encode ordinal patterns using the 'plain' algorithm. Parameters ---------- x : array_like Time series data to be encoded. Can be multi-dimensional, and must be convertible to an ndarray of data type 'double'. The input array is flattened prior to encoding. ord : int Order of the encoding, an integer between 2 and 20. lag : int Time lag used for creating embedding vectors. Must be a positive integer. trustme : bool, optional If set to True, the function arguments are not validated before processing. This will speed up the calculation. Returns ------- pat : ndarray of uint64 One-dimensional array, with each value representing an ordinal pattern. It holds that ``pat.size == x.size - (ord - 1) * lag``. """ if not trustme: x = _utils.prepare_input_data(x, ord, lag) if ord > 20: raise ValueError("plain algorithm does not support ord > 20") n_rel = ord - 1 # number of order relations n_pat = x.size - n_rel * lag # sequence length pat = _np.zeros(n_pat, dtype=_np.uint64) for k in range(0, n_pat): for i in range(0, n_rel): for j in range(i + 1, ord): pat[k] += x[k + i * lag] > x[k + j * lag] pat[k] *= n_rel - i return pat
def encode_plain(x, ord, lag, trustme=False): """ Extract and encode ordinal patterns using the 'plain' algorithm. Parameters ---------- x : array_like Time series data to be encoded. Can be multi-dimensional, and must be convertible to an ndarray of data type 'double'. The input array is flattened prior to encoding. ord : int Order of the encoding, an integer between 2 and 20. lag : int Time lag used for creating embedding vectors. Must be a positive integer. trustme : bool, optional If set to True, the function arguments are not validated before processing. This will speed up the calculation. Returns ------- pat : ndarray of uint64 One-dimensional array, with each value representing an ordinal pattern. It holds that ``pat.size == x.size - (ord - 1) * lag``. """ if not trustme: x = _utils.prepare_input_data(x, ord, lag) if ord > 20: raise ValueError("plain algorithm does not support ord > 20") n_pat = x.size - (ord - 1) * lag pat = _np.empty(n_pat , dtype=_np.uint64) ret = _lib.ordpat_encode_plain(x, x.size, pat, pat.size, ord, lag) if not ret == 0: _raise_api_error(ret) return pat
def recognise_speech(audio_data, network, preprocessor, threshold): # Prepare the input Tensors input_data = prepare_input_data(audio_data, network.get_data_type(), network.get_input_quantization_scale(0), network.get_input_quantization_offset(0), preprocessor) # Run inference output_result = network.run([input_data]) dequantized_result = [] for index, ofm in enumerate(output_result): dequantized_result.append( dequantize_output(ofm, network.is_output_quantized(index), network.get_output_quantization_scale(index), network.get_output_quantization_offset(index))) # Decode the text and display result if above threshold decoded_result = decode(dequantized_result, labels) if decoded_result[1] > threshold: display_text(decoded_result)
def ordpat(x, ord, lag=1, algorithm=None, axis=-1): """ Return a sequence of ordinal patterns in numerical representation. The function extracts the consecutive ordinal patterns of order `ord` from the (possibly multi-variate) time series contained in `x`, using the time lag `lag` and the encoding algorithm `algorithm`. The ordinal patterns are then encoded into numerical values ranging from 0 to factorial(ord) - 1. Parameters ---------- x : array_like Time series data to be encoded. Can be multi-dimensional, and must be convertible to an ndarray of data type 'double'. ord : int Order of the encoding, an integer greater than 2. The upper bound is algorithm-dependent (see below). lag : int, optional Time lag used for creating embedding vectors. Must be a positive integer. algorithm : {'plain', 'plain_c', 'overlap', 'overlap_c', 'overlap_mp_c', 'lookup', 'lookup_c', 'vectorised'}, optional One of the encoding algorithms described below. By default, the algorithm is selected automatically. axis : int, optional The axis of the input data along which the extraction shall be performed. By default, the last axis is used. Returns ------- pat : ndarray of uint64 Ordinal patterns in numerical representation and stored as integers in uint64 format. The shape of the `pat` array is equal to the input array `x`, except for the axis on which the encoding is performed. For this axis, it holds that ``pat.shape[axis] == x.shape[axis] - (ord - 1) * lag``. The 'overlap_mp_c' algorithm represents ordinal patterns by vectors of uint64 values. Thus, an extra dimension is appended to `pat` in this case. The size of this dimension depends on the pattern order. Details ------- The `ordpat` function provides various encoding algorithms, all yielding the same numerical result. The algorithms differ in run-time efficiency, as well as in the maximum pattern order supported. Possible values for `algorithm` are: -------------------------------------------------------------------------- ALGORITHM MAX ORD NEEDS C LIBRARY REMARKS -------------------------------------------------------------------------- lookup 10 no overlap 20 no plain 20 no Very slow. vectorised 20 no Best native choice. -------------------------------------------------------------------------- lookup_c 10 yes Fast for small orders. overlap_c 20 yes Good general-purpose algorithm. overlap_mp_c 255 yes Supports orders > 20. plain_c 20 yes -------------------------------------------------------------------------- The 'overlap_mp_c' algorithm supports very high pattern orders. When using this algorithm, the last dimension of the returned array indexes the 64-bit wide elements of each single ordinal pattern. For example, >>> ordpat(np.zeros((8, 1000)), 21, 1, 'overlap_mp_c') returns a uint64-valued array of shape (8, 980, 2), because a sequence of length 1000 has 980 ordinal patterns of order 21, whereas storing each such pattern requires two 64-bit values. """ x = _utils.prepare_input_data(x, ord, lag, axis) alg = _check_algorithm(ord, algorithm) enc_fnc = _algorithms[alg]['fcn'] if alg in ("lookup", "lookup_c"): _update_lookup_table(ord) if alg == "vectorised": return enc_fnc(x, ord, lag, axis, True) else: return _np.apply_along_axis(enc_fnc, axis, x, ord, lag, True)
def encode_vectorised(x, ord, lag, axis=-1, trustme=False): """ Extract and encode ordinal patterns using the 'vectorised' algorithm. Parameters ---------- x : array_like Time series data to be encoded. Can be multi-dimensional, and must be convertible to an ndarray of data type 'double'. ord : int Order of the encoding, an integer between 2 and 20. lag : int Time lag used for creating embedding vectors. Must be a positive integer. axis : int, optional The axis of the input data along which the extraction shall be performed. By default, the last axis is used. trustme : bool, optional If set to True, the function arguments are not validated before processing. This will speed up the calculation. Returns ------- pat : ndarray of uint64 Each value represents an ordinal pattern. The shape of the `pat` array is equal to the input array `x`, except for the axis on which the encoding is performed. For this axis, it holds that ``pat.shape[axis] == x.shape[axis] - (ord - 1) * lag``. """ if not trustme: x = _utils.prepare_input_data(x, ord, lag, axis) if ord > 20: raise ValueError("vectorised algorithm does not support ord > 20") _scope['x'] = _np.moveaxis(x, axis, 0) if (_scope['order'] != ord) or (_scope['lag'] != lag): cmd = "" for idx in range(1, ord + 1): fwd = (idx - 1) * lag rev = (ord - idx) * lag cmd += "x{0} = x[{1}:{2}]\n".format(idx, fwd, -rev or None) cmd += "\ny = " word_size = _utils.pattern_word_size(ord) for i in range(1, ord): cmd += "{0} * (".format(_factorial(ord - i)) cmd += "(x{0} > x{1})".format(i, i + 1) cmd += ".astype(_np.uint8)" for j in range(i + 2, ord + 1): cmd += " + (x{0} > x{1})".format(i, j) cmd += ").astype(_np.uint{0})".format(word_size) if i == ord - 1: cmd += "\n" else: cmd += " + \\\n" + " " * 4 _scope['cmd'] = compile(cmd, "<string>", "exec") _scope['order'] = ord _scope['lag'] = lag exec(_scope['cmd'], None, _scope) pat = _scope['y'] pat = _np.moveaxis(pat, 0, axis) return pat.astype(_np.uint64)
def encode_overlap(x, ord, lag, trustme=False): """ Extract and encode ordinal patterns using the 'overlap' algorithm. Parameters ---------- x : array_like Time series data to be encoded. Can be multi-dimensional, and must be convertible to an ndarray of data type 'double'. The input array is flattened prior to encoding. ord : int Order of the encoding, an integer between 2 and 20. lag : int Time lag used for creating embedding vectors. Must be a positive integer. trustme : bool, optional If set to True, the function arguments are not validated before processing. This will speed up the calculation. Returns ------- pat : ndarray of uint64 One-dimensional array, with each value representing an ordinal pattern. It holds that ``pat.size == x.size - (ord - 1) * lag``. """ if not trustme: x = _utils.prepare_input_data(x, ord, lag) if ord > 20: raise ValueError("overlap algorithm does not support ord > 20") n_rel = ord - 1 # number of order relations n_pat = x.size - n_rel * lag # sequence length pat = _np.zeros(n_pat, dtype=_np.uint64) ranks = _np.zeros([lag, n_rel], dtype=_np.uint8) radix = [_factorial(r) for r in range(n_rel, 0, -1)] radix = _np.array(radix, dtype=_np.uint64) for k in range(0, lag): for i in range(0, n_rel - 1): for j in range(i + 1, n_rel): ranks[k, i + 1] += x[k + i * lag] > x[k + j * lag] i = 0 for k in range(0, n_pat): ranks[i] = _np.roll(ranks[i], -1) ranks[i, -1] = 0 updates = x[k:k + n_rel * lag:lag] > x[k + n_rel * lag] ranks[i] += updates pat[k] = _np.matmul(radix, ranks[i]) i = (i + 1) % lag return pat