主要内容

语音命令识别代码生成树莓π

这个例子显示了如何部署特征提取和卷积神经网络(CNN)的语音命令识别覆盆子π™。生成特征提取和网络代码,使用MATLAB编码器™,MATLAB®支持包树莓π硬件,手臂®计算库。万博1manbetx在本例中,生成的代码是一个可执行文件在你的树莓π,叫做通过MATLAB脚本显示预测语音命令信号和听觉谱图。之间的交互MATLAB脚本和可执行文件在你的树莓π是使用用户数据报协议(UDP)处理。音频预处理和网络训练的详细信息,请参阅使用深度学习训练语音命令识别模型

先决条件

  • 为深度学习MATLAB编码器接口库

  • ARM处理器,支持霓虹灯扩展万博1manbetx

  • 臂计算库版本20.02.1(目标ARM硬件)

  • 环境变量的编译器和库

为支持版万博1manbetx本的库和设置环境变量信息,明白了先决条件与MATLAB编码器深度学习(MATLAB编码器)

流媒体在MATLAB演示

使用相同的管道参数特征提取和分类发展使用深度学习训练语音命令识别模型

定义相同的采样率的网络训练(16赫兹)。定义分类率和音频样本输入每帧的数量。树皮特征输入到网络是一个光谱图对应于1秒的音频数据。树皮谱图计算了25 ms windows 10 ms啤酒花。在每个谱图计算个体数量的光谱。

fs = 16000;classificationRate = 20;samplesPerCapture = fs / classificationRate;segmentDuration = 1;segmentSamples =圆(segmentDuration * fs);frameDuration = 0.025;frameSamples =圆(frameDuration * fs);hopDuration = 0.010;hopSamples =圆(hopDuration * fs);地板numSpectrumPerSpectrogram = ((segmentSamples-frameSamples) / hopSamples) + 1;

创建一个audioFeatureExtractor标准化对象提取50-band树皮色没有窗口。计算每个光谱图中的元素数量。

afe = audioFeatureExtractor (“SampleRate”fs,“FFTLength”,512,“窗口”损害(frameSamples“周期”),“OverlapLength”frameSamples - hopSamples“barkSpectrum”,真正的);numBands = 50;setExtractorParameters (afe“barkSpectrum”,“NumBands”numBands,“WindowNormalization”、假);numElementsPerSpectrogram = numSpectrumPerSpectrogram * numBands;

加载pretrained CNN和标签。

负载(“commandNet.mat”. class)标签= trainedNet.Layers(结束);NumLabels =元素个数(标签);BackGroundIdx =找到(标签= =“背景”);

定义缓冲区和决策阈值过程网络的预测。

probBuffer =单(0 ([NumLabels classificationRate / 2]));YBuffer =单(NumLabels * (1, classificationRate / 2));countThreshold =装天花板(classificationRate * 0.2);probThreshold =单(0.7);

创建一个audioDeviceReader从你的设备对象读取音频。创建一个dsp.AsyncBuffer对象将音频缓冲区到块。

adr = audioDeviceReader (“SampleRate”fs,“SamplesPerFrame”samplesPerCapture,“OutputDataType”,“单一”);audioBuffer = dsp.AsyncBuffer (fs);

创建一个dsp.MatrixViewer对象和一个timescope对象来显示结果。

matrixViewer = dsp.MatrixViewer (“ColorBarLabel”,“权力/乐队(dB /乐队)”,“包含”,“帧”,“YLabel”,“汪汪”乐队,“位置”(400 100 600 250),“ColorLimits”2.6445 [4],“AxisOrigin”,“左下角”,“名称”,“使用深度学习语音命令识别”);timeScope = timeScope (“SampleRate”fs,“YLimits”[1],“位置”(400 380 600 250),“名称”,“使用深度学习语音命令识别”,“TimeSpanSource”,“财产”,“时间间隔”,1“BufferLength”fs,“YLabel”,“振幅”,“ShowGrid”,真正的);

显示的时间范围和矩阵查看器。检测命令,只要时间范围和矩阵查看器打开或直到到达的期限。停止现场检测在期限到达之前,关闭时间范围矩阵查看器窗口或窗口。

显示(timeScope) (matrixViewer)期限= 10;抽搐isVisible (timeScope) & & isVisible (matrixViewer) & & toc <时限%捕捉音频x = adr ();写(audioBuffer x);fs, y =阅读(audioBuffer fs-samplesPerCapture);%计算听觉功能=特征提取(afe y);+ 1 e-6 auditoryFeatures = log10(特性);%进行预测聚合氯化铝=预测(trainedNet auditoryFeatures);[~,YPredicted] = max(聚合氯化铝);%进行统计后加工YBuffer = [YBuffer(2:结束),YPredicted);probBuffer = [probBuffer(:, 2:结束)、聚合氯化铝(:));[YModeIdx,计数]=模式(YBuffer);maxProb = max (probBuffer (YModeIdx:));如果YModeIdx单一(BackGroundIdx) = = | |单(count) < countThreshold | | maxProb < probThreshold speechCommandIdx = BackGroundIdx;其他的speechCommandIdx = YModeIdx;结束%更新图matrixViewer (auditoryFeatures ');timeScope (x);如果(speechCommandIdx = = BackGroundIdx) timeScope。Title =' ';其他的timeScope。Title =char(labels(speechCommandIdx));结束drawnowlimitrate结束

隐藏的范围。

隐藏(matrixViewer)隐藏(timeScope)

MATLAB代码为部署做好准备

创建一个函数来执行特征提取与代码生成兼容,电话generateMATLABFunctionaudioFeatureExtractor对象。的generateMATLABFunction对象函数创建一个独立的函数执行等效特征提取和兼容代码生成。

generateMATLABFunction (afe“extractSpeechFeatures”)

HelperSpeechCommandRecognitionRasPi万博1manbetx支持函数封装了特征提取和网络预测过程演示。因此特征提取与代码生成兼容,特征提取是由生成的extractSpeechFeatures函数。所以网络兼容的代码生成,支持函数使用万博1manbetxcoder.loadDeepLearningNetwork(MATLAB编码器)函数加载网络。支持函数万博1manbetx使用一个dsp.UDPReceiver系统对象发送听觉谱图和相对应的指数预测语音命令从树莓πMATLAB。支持函数万博1manbetx使用dsp.UDPReceiver系统对象接收音频捕捉到你的麦克风在MATLAB。

生成可执行的覆盆子π

取代hostIPAddress与你的机器的地址。你的树莓π听觉谱图和预测语音命令发送给这个IP地址。

hostIPAddress = coder.Constant (“172.18.230.30”);

创建一个代码生成配置对象生成一个可执行程序。指定目标语言c++。

cfg = coder.config (exe”);cfg。TargetLang =“c++”;

为深度学习创建一个配置对象代码生成的手臂计算图书馆在你的树莓π。指定覆盆子π的架构和深度学习配置对象附加到代码生成配置对象。

dlcfg = coder.DeepLearningConfig (“arm-compute”);dlcfg。ArmArchitecture =v7的;dlcfg。ArmComputeVersion =“20.02.1”;cfg。DeepLearningConfig = dlcfg;

使用的覆盆子π支持包功能,万博1manbetxraspi创建一个连接你的树莓π。在以下代码中,替换:

  • raspiname覆盆子π的名称

  • π与您的用户名

  • 密码用你的密码

r = raspi (“raspiname”,“π”,“密码”);

创建一个coder.hardware(MATLAB编码器)对象树莓π和附加的代码生成配置对象。

hw = coder.hardware (“树莓π”);cfg。硬件= hw;

指定构建文件夹树莓π。

buildDir =“~ / remoteBuildDir”;cfg.Hardware。BuildDir = BuildDir;

使用一个自动生成c++主要文件的生成一个独立的可执行文件。

cfg。GenerateExampleMain =“GenerateCodeAndCompile”;

调用codegen(MATLAB编码器)生成c++代码和可执行你的树莓π。默认情况下,树莓π应用程序名称是一样的MATLAB函数。

codegen配置cfgHelperSpeechCommandRecognitionRasPiarg游戏{hostIPAddress}报告- v
部署代码。这可能需要几分钟。# # #编译函数(s) HelperSpeechCommandRecognitionRasPi……- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -位置生成的精灵:/home/pi/remoteBuildDir / MATLAB_ws R2022a / W / / ExampleManager / sporwal.Bdoc22a交货。使用工具链j1844576 / deeplearning_shared-ex00376115 # # #: GNU GCC嵌入式Linux # # # ' W: \ \ ExampleManager \交货sporwal.Bdoc22a.j1844576 \ deeplearning_shared-ex00376115 \ codegen \ exe \ HelperSpeechCommandRecognitionRasPi \ HelperSpeechCommandRecognitionRasPi_rtw。可“是最新的# # #建筑”HelperSpeechCommandRecognitionRasPi”:让- j $ (((nproc) + 1美元))-Otarget - f HelperSpeechCommandRecognitionRasPi_rtw。可所有- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # #生成编译报告……警告:函数的HelperSpeechCommandRecognitionRasPi并不因无限循环终止。= = > HelperSpeechCommandRecognitionRasPi行警告:86列:1代码生成成功(警告):查看报告

初始化应用程序覆盆子π

创建一个命令来打开HelperSpeechCommandRasPi应用覆盆子π。使用系统将命令发送到你的树莓π。

applicationName =“HelperSpeechCommandRecognitionRasPi”;applicationDirPaths = raspi.utils.getRemoteBuildDirectory (“applicationName”applicationName);targetDirPath = applicationDirPaths {1} .directory;exeName = strcat (applicationName,“.elf”);命令= [“cd”targetDirPath”;。/”exeName“& > 1 &”];系统(r,命令);

创建一个dsp.UDPReceiver系统对象发送音频捕获在MATLAB覆盆子π。更新targetIPAddress你的树莓π。覆盆子π接收音频捕获来自同一个端口使用dsp.UDPReceiver系统对象。

targetIPAddress =“172.18.231.92”;UDPSend = dsp.UDPSender (“RemoteIPPort”,26000,“RemoteIPAddress”,targetIPAddress);

创建一个dsp.UDPReceiver系统对象接受听觉特性和覆盆子的预测语音命令索引π。每一个UDP数据包收到覆盆子π由听觉特性列为主的订单预测语音命令指数紧随其后。的最大消息长度dsp.UDPReceiver对象是65507个字节。计算缓冲区的大小,以适应UDP数据包的最大数量。

sizeOfFloatInBytes = 4;maxUDPMessageLength =地板(65507 / sizeOfFloatInBytes);samplesPerPacket = 1 + numElementsPerSpectrogram;numPackets =地板(maxUDPMessageLength / samplesPerPacket);bufferSize = numPackets * samplesPerPacket * sizeOfFloatInBytes;UDPReceive = dsp.UDPReceiver (“LocalIPPort”,21000,“MessageDataType”,“单身”,“MaximumMessageLength”samplesPerPacket,“下面的”bufferSize);

减少初始化开销通过发送一个帧的零树莓π上运行的可执行文件。

UDPSend (0 (samplesPerCapture 1“单身”));

使用部署代码执行语音命令识别

检测命令,只要时间范围和矩阵查看器打开或直到到达的期限。停止现场检测在期限到达之前,关闭时间范围或矩阵查看器窗口。

显示(timeScope) (matrixViewer)期限= 20;抽搐isVisible (timeScope) & & isVisible (matrixViewer) & & toc <时限% RasPi捕捉音频和发送x = adr ();UDPSend (x);%从RasPi接收数据包udpRec = UDPReceive ();如果~ isempty (udpRec)%提取预测指数,最后的样品收到UDP数据包speechCommandIdx = udpRec(结束);%提取听觉谱图规范=重塑(udpRec (1: numElementsPerSpectrogram), [numBands numSpectrumPerSpectrogram]);%显示时域信号和听觉谱图timeScope (x) matrixViewer(规范)如果speechCommandIdx = = BackGroundIdx timeScope。Title =' ';其他的timeScope。Title =char(labels(speechCommandIdx));结束drawnowlimitrate结束结束隐藏(matrixViewer)隐藏(timeScope)

stopLiveDetectionRaspi.png

停止执行你的树莓π,使用stopExecutable。释放UDP的对象。

stopExecutable (codertarget.raspi.raspberrypi exeName)发布(UDPSend)发布(UDPReceive)

配置文件使用公益诉讼工作流

你可以测量上的执行时间使用processor-in-the-loop覆盆子π(公益诉讼)工作流的嵌入式编码®。的ProfileSpeechCommandRecognitionRaspi万博1manbetx支持函数相当于HelperSpeechCommandRecognitionRaspi功能,除了演讲前返回命令指数和听觉谱图而后者使用UDP发送相同的参数。UDP调用所花的时间小于1 ms,这是相对较小的总执行时间相比。

创建一个公益诉讼配置对象。

cfg = coder.config (“自由”,“是”,真正的);cfg。VerificationMode =“公益诉讼”;

设置部门计算库和架构。

dlcfg = coder.DeepLearningConfig (“arm-compute”);cfg。DeepLearningConfig = dlcfg;cfg.DeepLearningConfig。ArmArchitecture =v7的;cfg.DeepLearningConfig。ArmComputeVersion =“19.05”;

与你的目标硬件设置连接。

如果(~ (“r”,“var”)r = raspi (“raspiname”,“π”,“密码”);结束hw = coder.hardware (“树莓π”);cfg。硬件= hw;

设置构建目录和目标语言。

buildDir =“~ / remoteBuildDir”;cfg.Hardware。BuildDir = BuildDir;cfg。TargetLang =“c++”;

启用剖析,然后生成公益诉讼代码。一个墨西哥人的文件命名ProfileSpeechCommandRecognition_pil是你当前文件夹中生成。

cfg。CodeExecutionProfiling = true;codegen配置cfgProfileSpeechCommandRecognitionRaspiarg游戏{兰德(samplesPerCapture 1 '单')}报告- v
部署代码。这可能需要几分钟。# # #编译函数(s) ProfileSpeechCommandRecognitionRaspi……# # #连接配置功能的ProfileSpeechCommandRecognitionRaspi:覆盆子π的# # #使用工具链:GNU GCC嵌入式Linux创建“W: # # # \ \ ExampleManager \ sporwal.Bdoc22a.j1844576交货\ deeplearning_shared-ex00376115 \ codegen \ lib \ ProfileSpeechCommandRecognitionRaspi \ coderassumptions \ lib \ ProfileSpeechCommandRecognitionRaspi_ca。可”……# # #建筑“ProfileSpeechCommandRecognitionRaspi_ca”:让- j $ (((nproc) + 1)美元)-Otarget - f ProfileSpeechCommandRecognitionRaspi_ca。可使用工具链所有# # #:GNU GCC嵌入式Linux创建“W: # # # \ \ ExampleManager \ sporwal.Bdoc22a.j1844576交货\ deeplearning_shared-ex00376115 \ codegen \ lib \ ProfileSpeechCommandRecognitionRaspi \ \ ProfileSpeechCommandRecognitionRaspi_rtw趁手。可”……# # #建筑“ProfileSpeechCommandRecognitionRaspi”:让- j $ (((nproc) + 1)美元)-Otarget - f ProfileSpeechCommandRecognitionRaspi_rtw。可生成的精灵的位置:/home/pi/remoteBuildDir / MATLAB_ws R2022a / W / / ExampleManager / sporwal.Bdoc22a交货。j1844576 / deeplearning_shared-ex00376115 codegen / lib / ProfileSpeechCommandRecognitionRaspi /公益诉讼- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # #使用工具链:GNU GCC嵌入式Linux # # #的W: \ \ ExampleManager \ sporwal.Bdoc22a.j1844576交货\ deeplearning_shared-ex00376115 \ codegen \ lib \ ProfileSpeechCommandRecognitionRaspi \ ProfileSpeechCommandRecognitionRaspi_rtw。可“是最新的# # #建筑”ProfileSpeechCommandRecognitionRaspi”:让- j $ (((nproc) + 1美元))-Otarget - f ProfileSpeechCommandRecognitionRaspi_rtw。可所有- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # # #生成编译报告……代码生成成功:查看报告

评估覆盆子π执行时间

多次调用生成的公益诉讼函数得到的平均执行时间。

testDur = 50 e - 3;我会= 100;k = 1:我会x = pinknoise (fs * testDur,“单一”);[speechCommandIdx, auditoryFeatures] = ProfileSpeechCommandRecognitionRaspi_pil (x);结束
# # #开始应用程序:“codegen \ lib \ ProfileSpeechCommandRecognitionRaspi \ \ ProfileSpeechCommandRecognitionRaspi趁手。精灵的终止执行:明确ProfileSpeechCommandRecognitionRaspi_pil # # #启动应用程序ProfileSpeechCommandRecognitionRaspi.elf……执行分析数据可供浏览。开放的仿真数据检查员。执行分析报告后终止。

公益诉讼执行终止。

清晰的ProfileSpeechCommandRecognitionRaspi_pil
# # #主机应用程序产生以下标准输出(stdout)和标准错误(stderr)消息:执行分析报告:报告(getCoderExecutionProfile (ProfileSpeechCommandRecognitionRaspi))

生成执行概要报告评估执行时间。

executionProfile = getCoderExecutionProfile (“ProfileSpeechCommandRecognitionRaspi”);报告(executionProfile,“单位”,“秒”,“ScaleFactor”,“1 e 03”,“NumericFormat”,' % 0.4 f ')
ans = ' W: \ \ ExampleManager \ sporwal.Bdoc22a.j1844576交货\ deeplearning_shared-ex00376115 \ codegen \ lib \ ProfileSpeechCommandRecognitionRaspi \ html \ \ ExecutionProfiling_d82c7024f87064b9.html孤儿”

profiler.png

最大的执行时间ProfileSpeechCommandRecognitionRaspi函数是近两倍的平均执行时间。你可以注意到执行时间是最大的公益诉讼的第一次调用函数,由于初始化发生在第一次调用。的平均执行时间是大约20 ms,低于50毫秒的预算(音频捕捉时间)。性能是衡量覆盆子π4模型B牧师1.1。

timing.png