Matlab转C++

项目描述

针对已经使用了支持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]
//

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值