1# Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Outputs tables used for fast calculations at runtime.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21# import soundfile as sf 22import numpy as np 23 24 25def to_cc(x, varname, directory='', scale_factor=1): 26 """Writes table values to a C++ source file.""" 27 x = (x / np.max(np.abs(x))) * 32768 * scale_factor 28 x[x > 32767] = 32767 29 x[x < -32768] = -32768 30 x = x.astype(int) 31 x = [str(v) if i % 10 != 0 else '\n ' + str(v) for i, v in enumerate(x)] 32 33 cmsis_path = 'tensorflow/lite/experimental/micro/examples/micro_speech/CMSIS' 34 xstr = '#include "{}/{}.h"\n\n'.format(cmsis_path, varname) 35 xstr += 'const int g_{}_size = {};\n'.format(varname, len(x)) 36 xstr += 'const int16_t g_{}[{}] = {{{}}};\n'.format(varname, len(x), 37 ', '.join(x)) 38 39 with open(directory + varname + '.cc', 'w') as f: 40 f.write(xstr) 41 42 43def to_h(_, varname, directory=''): 44 """Writes a header file for the table values.""" 45 tf_prepend = 'TENSORFLOW_LITE_EXPERIMENTAL_MICRO_EXAMPLES_MICRO_SPEECH_' 46 xstr = '#ifndef {}{}_H_\n'.format(tf_prepend, varname.upper()) 47 xstr += '#define {}{}_H_\n\n'.format(tf_prepend, varname.upper()) 48 xstr += '#include <cstdint>\n\n' 49 xstr += 'extern const int g_{}_size;\n'.format(varname) 50 xstr += 'extern const int16_t g_{}[];\n\n'.format(varname) 51 xstr += '#endif' 52 53 with open(directory + varname + '.h', 'w') as f: 54 f.write(xstr) 55 56 57# x = sf.read('yes_f2e59fea_nohash_1.wav')[0] 58# to_cc(x, 'yes_waveform') 59# to_h(x, 'yes_waveform') 60# 61# x = sf.read('no_f9643d42_nohash_4.wav')[0] 62# to_cc(x, 'no_waveform') 63# to_h(x, 'no_waveform') 64 65# 30ms of data @ 16 kHz = 480 samples 66hann = np.hanning(int(16000 * 0.03)) # Window 30ms of data 67to_cc(hann, 'hanning', directory='./') 68to_h(hann, 'hanning', directory='./') 69 70t = np.arange(16000. * 0.03) / 16000. 71sin1k = np.sin( 72 2 * np.pi * 1000 * 73 t) # Factor of 10 because micro preprocessing overflows otherwise 74to_cc(sin1k, 'sin_1k', directory='./', scale_factor=0.1) 75to_h(sin1k, 'sin_1k', directory='./') 76