项目描述
针对已经使用了支持NEON扩展的ARM®处理器的嵌入式平台,将基于已经训练完成的卷积神经网络的语音识别Matlab代码,生成适用于嵌入式平台的c++代码。
环境要求
- MATLAB Coder Interface for Deep Learning Libraries.
- ARM Compute Library
- Deep Learning Toolbox™
- GNU Toolchain for the A-profile architecture
- Environment variables for the compilers and libraries
MATLAB Code(VoiceRecognize.m)
function out = VoiceRecognize(x)%#codegen
out = '';
fs = 16000;
%persistent trainedNet;
persistent spec;
frameDuration = 0.025;
hopDuration = 0.010;
frameLength = floor(frameDuration*fs);
hopLength = floor(hopDuration*fs);
waveBuffer = zeros([fs,1]);
numBands = 40;
trainedNet = coder.loadDeepLearningNetwork('commandNet2.mat','trainedNet');
audioBuffer = dsp.AsyncBuffer(fs);
epsil = 1e-6;
write(audioBuffer,x);
waveBuffer(1:end-numel(x)) = waveBuffer(numel(x)+1:end);
waveBuffer(end-numel(x)+1:end) = x;
spec = melSpectrogram(waveBuffer,fs, ...
'WindowLength',frameLength, ...
'OverlapLength',frameLength - hopLength, ...
'FFTLength',512, ...
'NumBands',numBands, ...
'FrequencyRange',[50,7000]);
feature =spec;
feature = log10(feature + epsil);
[YPredicted,probs] = classify(trainedNet,feature);
out = cellstr(YPredicted);
end
流程
1.Select Source Files
选择需要转换的Matlab源码文件:
Entry-Point Functions: VoiceRecognize
Project location: …/VoiceRecognize.prj
Numeric Conversion: None
2.Define Input Types
定义函数输入参数类型,即输入音频:
Double: inf x 1
3.Check for Run-Time Issues
检查Matlab Coder不支持生成的函数,
不需要生成Mex function则留空。
定义Target Library:
Target Library: ARM Compute
ARM Compute Library version: 19.05
ARM Compute Architecture: armv7
4.Generate Code
代码生成配置参数:
Build type: Dynamic Library
Output file: VoiceRecognize
Language: C++, Generate code only
Interface style: Functions
Hardware Board: None-Select device below
Device: ARM Compatible, ARM Cotex-A
Toolchain: Automatically locate an installed toolchain
5.Finish Workflow
完成代码转换,将代码进行打包。
C++ Code(VoiceRecognize.cpp)
//
// File: VoiceRecognize.cpp
// Author by Dan Long
//
// Include Files
#include "VoiceRecognize.h"
#include "AsyncBuffercg.h"
#include "DeepLearningNetwork.h"
#include "VoiceRecognize_data.h"
#include "VoiceRecognize_initialize.h"
#include "cellstr.h"
#include "classify.h"
#include "matlabCodegenHandle.h"
#include "melSpectrogram.h"
#include "rt_nonfinite.h"
#include <cmath>
// Function Definitions
//
// Arguments : const double x[16000]
// cell_wrap_0 out[1]
// Return Type : void
//
void VoiceRecognize(const double x[16000], cell_wrap_0 out[1])
{
dsp_private_AsyncBuffercg audioBuffer;
b_trainedNet_0 trainedNet;
double spec[3920];
c_matlab_internal_coder_categor YPredicted;
float probs[12];
if (!isInitialized_VoiceRecognize) {
VoiceRecognize_initialize();
}
audioBuffer.pBuffer.matlabCodegenIsDeleted = true;
audioBuffer.matlabCodegenIsDeleted = true;
// persistent trainedNet;
DeepLearningNetwork_setup(&trainedNet);
audioBuffer.init();
audioBuffer.write();
melSpectrogram(x, spec);
for (int k = 0; k < 3920; k++) {
spec[k] = std::log10(spec[k] + 1.0E-6);
}
DeepLearningNetwork_classify(&trainedNet, spec, &YPredicted, probs);
YPredicted.cellstr(out);
audioBuffer.matlabCodegenDestructor();
audioBuffer.pBuffer.matlabCodegenDestructor();
}
//
// File trailer for VoiceRecognize.cpp
//
// [EOF]
//