matlab语音识别系统(源代码)

目录

设计任务及要求………………………………………………1语音识别的简单介绍

语者识别的概念……………………………………………2

特征参数的提取……………………………………………3

用矢量量化聚类法生成码本………………………………3

的说话人识别 …………………………………………4

算法程序分析

函数关系………………………………………………….4

代码说明……………………………………………………5

函数mfcc………………………………………………5

函数disteu……………………………………………5

函数vqlbg…………………………………………….6

函数test………………………………………………6

函数testDB……………………………………………7

函数train……………………………………………8

函数melfb………………………………………………8

演示分析…………………………………………………….9心得体会…………………………………………………….11

附:GUI程序代码………………………………………………12

设计任务及要求

用MATLAB实现简单的语音识别功能;

具体设计要求如下:

用MATLAB实现简单的数字1~9的语音识别功能。

语音识别的简单介绍

基于VQ的说话人识别系统,矢量量化起着双重作用。在训练阶段,把每一个说话者所提取的特征参数进行分类,产生不同码字所组成的码本。在识别(匹配)阶段,我们用VQ方法计算平均失真测度(本系统在计算距离d时,采用欧氏距离测度),从而判断说话人是谁。

语音识别系统结构框图如图1所示。

图1 语音识别系统结构框图

语者识别的概念

语者识别就是根据说话人的语音信号来判别说话人的身份。语音是人的自然属性之一,由于说话人发音器官的生理差异以及后天形成的行为差异,每个人的语音都带有强烈的个人色彩,这就使得通过分析语音信号来识别说话人成为可能。用语音来鉴别说话人的身份有着许多独特的优点,如语音是人的固有的特征,不会丢失或遗忘;语音信号的采集方便,系统设备成本低;利用电话网络还可实现远程客户服务等。因此,近几年来,说话人识别越来越多的受到人们的重视。与其他生物识别技术如指纹识别、手形识别等相比较,说话人识别不仅使用方便,而且属于非接触性,容易被用户接受,并且在已有的各种生物特征识别技术中,是唯一可以用作远程验证的识别技术。因此,说话人识别的应用前景非常广泛:今天,说话人识别技术已经关系到多学科的研究领域,不同领域中的进步都对说话人识别的发展做出了贡献。说话人识别技术是集声学、语言学、计算机、信息处理和人工智能等诸多领域的一项综合技术,应用需求将十分广阔。在吃力语音信号的时候如何提取信号中关键的成分尤为重要。语音信号的特征参数的好坏直接导致了辨别的准确性。

特征参数的提取

对于特征参数的选取,我们使用mfcc的方法来提取。MFCC参数是基于人的听觉特性利用人听觉的屏蔽效应,在Mel标度频率域提取出来的倒谱特征参数。

MFCC参数的提取过程如下:

1. 对输入的语音信号进行分帧、加窗,然后作离散傅立叶变换,获得频谱分布信息。

设语音信号的DFT为:

(1)

其中式中x(n)为输入的语音信号,N表示傅立叶变换的点数。

2. 再求频谱幅度的平方,得到能量谱。 3. 将能量谱通过一组Mel尺度的三角形滤波器组。

我们定义一个有M个滤波器的滤波器组(滤波器的个数和临界带的个数相近),采用的滤波器为三角滤波器,中心频率为f(m),m=1,2,3,···,M

本系统取M=100。

4. 计算每个滤波器组输出的对数能量。

(2)

其中

为三角滤波器的频率响应。

5. 经过离散弦变换(DCT)得到MFCC系数。

MFCC系数个数通常取20—30,常常不用0阶倒谱系数,因为它反映的是频谱能量,故在一般识别系统中,将称为能量系数,并不作为倒谱系数,本系统选取20阶倒谱系数。

用矢量量化聚类法生成码本

我们将每个待识的说话人看作是一个信源,用一个码本来表征。码本是从该说话人的训练序列中提取的MFCC特征矢量聚类而生成。只要训练的序列足够长,可认为这个码本有效地包含了说话人的个人特征,而与讲话的内容无关。

本系统采用基于分裂的LBG的算法设计VQ码本,

为训练序列,B为码本。

具体实现过程如下:

1. 取提取出来的所有帧的特征矢量的型心(均值)作为第一个码字矢量B1。2. 将当前的码本Bm根据以下规则分裂,形成2m个码字。

(4)

其中m从1变化到当前的码本的码字数,ε是分裂时的参数,本文ε=。

3. 根据得到的码本把所有的训练序列(特征矢量)进行分类,然后按照下面两个公式计算训练矢量量化失真量的总和

以及相对失真(n为迭代次数,初始n=0,

=∞,B为当前的码书),若相对失真小于某一阈值ε,迭代结束,当前的码书就是设计好的2m个码字的码书,转5。否则,转下一步。

量化失真量和:

(5)

相对失真:

(6)

4. 重新计算各个区域的新型心,得到新的码书,转3。

5. 重复2 ,3 和4步,直到形成有M个码字的码书(M是所要求的码字数),其中D0=10000。

VQ的说话人识别

设是未知的说话人的特征矢量

,共有T帧是训练阶段形成的码书,表示码书第m个码字,每一个码书有M个码字。再计算测试者的平均量化失真D,并设置一个阈值,若D小于此阈值,则是原训练者,反之则认为不是原训练者。

(7)

算法程序分析

在具体的实现过程当中,采用了matlab软件来帮助完成这个项目。在matlab中主要由采集,分析,特征提取,比对几个重要部分。以下为在实际的操作中,具体用到得函数关系和作用一一列举在下面。

函数关系

主要有两类函数文件和

在调用获取训练录音的vq码本,而调用获取单个录音的mel倒谱系数,接着调用将能量谱通过一组Mel尺度的三角形滤波器组。

在函数文件中调用计算训练录音(提供vq码本)与测试录音(提供mfcc)mel倒谱系数的距离,即判断两声音是否为同一录音者提供。调用获取单个录音的mel倒谱系数。调用将能量谱通过一组Mel尺度的三角形滤波器组。

具体代码说明

函数mffc:

function r = mfcc(s, fs)

m = 100;

n = 256;

l = length(s);

nbFrame = floor((l – n) / m) + 1; %沿-∞方向取整

for i = 1:n

for j = 1:nbFrame

M(i, j) = s(((j – 1) * m) + i); %对矩阵M赋值

end

end

h = hamming(n); %加 hamming 窗,以增加音框左端和右端的连续性

M2 = diag(h) * M;

for i = 1:nbFrame

frame(:,i) = fft(M2(:, i)); %对信号进行快速傅里叶变换FFT

end

t = n / 2;

tmax = l / fs;

m = melfb(20, n, fs); %将上述线性频谱通过Mel 频率滤波器组得到Mel 频谱,下面在将其转化成对数频谱

n2 = 1 + floor(n / 2);

z = m * abs(frame(1:n2, :)).^2;

r = dct(log(z)); %将上述对数频谱,经过离散余弦变换(DCT)变换到倒谱域,即可得到Mel 倒谱系数(MFCC参数)

函数disteu

—计算测试者和模板码本的距离

function d = disteu(x, y)

[M, N] = size(x); %音频x赋值给【M,N】

[M2, P] = size(y); %音频y赋值给【M2,P】

if (M ~= M2)

error(不匹配!) %两个音频时间长度不相等

end

d = zeros(N, P);

if (N < P)%在两个音频时间长度相等的前提下

copies = zeros(1,P);

for n = 1:N

d(n,:) = sum((x(:, n+copies) – y) .^2, 1);

end

else

copies = zeros(1,N);

for p = 1:P

d(:,p) = sum((x – y(:, p+copies)) .^2, 1);

end%%成对欧氏距离的两个矩阵的列之间的距离

end

d = d.^;

函数vqlbg

—该函数利用矢量量化提取了音频的vq码本

function r = vqlbg(d,k)

e = .01;

r = mean(d, 2);

dpr = 10000;

for i = 1:log2(k)

r = [r*(1+e), r*(1-e)];

while (1 == 1)

z = disteu(d, r);

[m,ind] = min(z, [], 2);

t = 0;

for j = 1:2^i

r(:, j) = mean(d(:, find(ind == j)), 2);

x = disteu(d(:, find(ind == j)), r(:, j));

for q = 1:length(x)

t = t + x(q);

end

end

if (((dpr – t)/t) < e)

break;

else

dpr = t;

end

end

end

函数test

function finalmsg = test(testdir, n, code)

for k = 1:n % read test sound file of each speaker

file = sprintf(%ss%, testdir, k);

[s, fs] = wavread(file);

v = mfcc(s, fs); % 得到测试人语音的mel倒谱系数

distmin = 4; %阈值设置处

% 就判断一次,因为模板里面只有一个文件

d = disteu(v, code{1}); %计算得到模板和要判断的声音之间的“距离”

dist = sum(min(d,[],2)) / size(d,1); %变换得到一个距离的量

%测试阈值数量级

msgc = sprintf(与模板语音信号的差值为:%10f , dist);

disp(msgc);

%此人匹配

if dist <= distmin %一个阈值,小于阈值,则就是这个人。

msg = sprintf(第%d位说话者与模板语音信号匹配,符合要求!\n, k);

finalmsg = 此位说话者符合要求!; %界面显示语句,可随意设定

disp(msg);

end

%此人不匹配

if dist > distmin

msg = sprintf(第%d位说话者与模板语音信号不匹配,不符合要求!\n, k);

finalmsg = 此位说话者不符合要求!; %界面显示语句,可随意设定

disp(msg);

end

end

函数testDB

这个函数实际上是对数据库一个查询,根据测试者的声音,找相应的文件,并且给出是谁的提示

function testmsg = testDB(testdir, n, code)

nameList={1,2,3,4,5,6,7,8,9 }; %这个是我们要识别的9个数

for k = 1:n % 数据库中每一个说话人的特征

file = sprintf(%ss%, testdir, k); %找出文件的路径

[s, fs] = wavread(file);

v = mfcc(s, fs); % 对找到的文件取mfcc变换

distmin = inf;

k1 = 0;

for l = 1:length(code)

d = disteu(v, code{l});

dist = sum(min(d,[],2)) / size(d,1);

if dist < distmin

distmin = dist;%%这里和test函数里面一样 但多了一个具体语者的识别

k1 = l;

end

end

msg=nameList{k1}

msgbox(msg);

end

函数train

—该函数就是对音频进行训练,也就是提取特征参数

function code = train(traindir, n)

k = 16; % number of centroids required

for i = 1:n % 对数据库中的代码形成码本

file = sprintf(%ss%, traindir, i);

disp(file);

[s, fs] = wavread(file);

v = mfcc(s, fs); % 计算 MFCCs 提取特征特征,返回值是Mel倒谱系数,是一个log的dct得到的

code{i} = vqlbg(v, k); % 训练VQ码本 通过矢量量化,得到原说话人的VQ码本

end

函数melfb

—确定矩阵的滤波器

function m = melfb(p, n, fs)

f0 = 700 / fs;

fn2 = floor(n/2);

lr = log(1 + f0) / (p+1);

% convert to fft bin numbers with 0 for DC term

bl = n * (f0 * (exp([0 1 p p+1] * lr) – 1));

直接转换为FFT的数字模型

b1 = floor(bl(1)) + 1;

b2 = ceil(bl(2));

b3 = floor(bl(3));

b4 = min(fn2, ceil(bl(4))) – 1;

pf = log(1 + (b1:b4)/n/f0) / lr;

fp = floor(pf);

pm = pf – fp;

r = [fp(b2:b4) 1+fp(1:b3)];

c = [b2:b4 1:b3] + 1;

v = 2 * [1-pm(b2:b4) pm(1:b3)];

m = sparse(r, c, v, p, 1+fn2);

演示分析

我们的功能分为两部分:对已经保存的9个数字的语音进行辨别和实时的判断说话人说的是否为一个数.在前者的实验过程中,先把9个数字的声音保存成wav的格式,放在一个文件夹中,作为一个检测的数据库.然后对检测者实行识别,系统给出提示是哪个数字.

在第二个功能中,实时的录取一段说话人的声音作为模板,提取mfcc特征参数,随后紧接着进行遇着识别,也就是让其他人再说相同的话,看是否是原说话者.

实验过程及具体功能如下:

先打开Matlab 使Current Directory为录音及程序所所在的文件夹

再打开文件“”,点run运行,打开enter界面,点击“进入”按钮进入系统。(注:文件包未封装完毕,目前只能通过此方式打开运行。)(如下图figure1)

figure1

在对数据库中已有的语者进行识别模块:

选择载入语音库语音个数;

点击语音库录制模版进行已存语音信息的提取;

点击录音-test进行现场录音;

点击语者判断进行判断数字,并显示出来。

在实时语者识别模块:

点击实时录制模板上的“录音-train”按钮,是把新语者的声音以wav格式存放在”实时模板”文件夹中, 接着点击“实时录制模板”,把新的模板提取特征值。随后点击实时语者识别模板上的“录音-train”按钮,是把语者的声音以wav格式存放在”测试”文件夹中,再点击“实时语者识别”,在对测得的声音提取特征值的同时,和实时模板进行比对,然后得出是否是实时模板中的语者。另外面板上的播放按钮都是播放相对应左边录取的声音。

想要测量多次,只要接着录音,自动保存,然后程序比对音频就可以。

退出只要点击菜单File/Exit,退出程序。

程序运行截图:

()运行后系统界面

六、结论

实验表明,该系统能较好地进行语音的识别,同时,基于矢量量化技术 (VQ)的语音识别系统具有分类准确,存储数据少,实时响应速度快等综合性能好的特点.

矢量量化技术在语音识别的应用方面,尤其是在孤立词语音识别系统中得到很好的应用,特别是有限状态矢量量化技术,对于语音识别更为有效。

通过这次课程设计,我对语音识别有了更加形象化的认识,也强化了MATLAB的应用,对将来的学习奠定了基础。

附:GUI程序代码

function varargout = untitled2(varargin)

% UNTITLED2 M-file for

% UNTITLED2, by itself, creates a new UNTITLED2 or raises the existing

% singleton*.

%

% H = UNTITLED2 returns the handle to a new UNTITLED2 or the handle to

% the existing singleton*.

%

% UNTITLED2(CALLBACK,hObject,eventData,handles,…) calls the local

% function named CALLBACK in with the given input arguments.

%

% UNTITLED2(Property,Value,…) creates a new UNTITLED2 or raises the

% existing singleton*. Starting from the left, property value pairs are

% applied to the GUI before untitled2_OpeningFunction gets called. An

% unrecognized property name or invalid value makes property application

% stop. All inputs are passed to untitled2_OpeningFcn via varargin.

%

% *See GUI Options on GUIDEs Tools menu. Choose “GUI allows only one

% instance to run (singleton)”.

%

% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help untitled2

% Last Modified by GUIDE 08-Jun-2010 23:58:57

% Begin initialization code – DO NOT EDIT

gui_Singleton = 1;

gui_State = struct(gui_Name, mfilename, …

gui_Singleton, gui_Singleton, …

gui_OpeningFcn, @untitled2_OpeningFcn, …

gui_OutputFcn, @untitled2_OutputFcn, …

gui_LayoutFcn, [] , …

gui_Callback, []);

if nargin && ischar(varargin{1})

= str2func(varargin{1});

end

if nargout

[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});

else

gui_mainfcn(gui_State, varargin{:});

end

% End initialization code – DO NOT EDIT

% — Executes just before untitled2 is made visible.

function untitled2_OpeningFcn(hObject, eventdata, handles, varargin)

% This function has no output args, see OutputFcn.

% hObject handle to figure

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% varargin command line arguments to untitled2 (see VARARGIN)

% Choose default command line output for untitled2

= hObject;

% Update handles structure

guidata(hObject, handles);

axes(findobj(tag,axes13));

imshow();

axes(findobj(tag,axes12));

imshow();

% UIWAIT makes untitled2 wait for user response (see UIRESUME)

% uiwait;

% — Outputs from this function are returned to the command line.

function varargout = untitled2_OutputFcn(hObject, eventdata, handles)

% varargout cell array for returning output args (see VARARGOUT);

% hObject handle to figure

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure

varargout{1} = ;

% — Executes on button press in pushbutton1.

function pushbutton1_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton1 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

Channel_Str=get,String);

Channel_Number=str2double(Channel_Str{get,Value)});

global moodle;

moodle = train(模版\,Channel_Number) %¶Ô´ýÇóÓïÒô½øÐÐÌáÈ¡Âë±¾

% — Executes on button press in pushbutton2.

function pushbutton2_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton2 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handglobal data1;

global moodle ;

test(测试\,1,moodle)%ʵʱÓïÒô¼ì²â

% ——————————————————————–

function Open_Callback(hObject, eventdata, handles)

% hObject handle to Open (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

[filename,pathname]=uigetfile()

file=get,[filename,pathname])

[y,f,b]=wavread(file);

% ——————————————————————–

function Exit_Callback(hObject, eventdata, handles)

% hObject handle to Exit (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

exit

% ——————————————————————–

function About_Callback(hObject, eventdata, handles)

% hObject handle to About (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

H=[语者识别]

helpdlg(H,help text)

% ——————————————————————–

function File_Callback(hObject, eventdata, handles)

% hObject handle to File (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% ——————————————————————–

function Edit_Callback(hObject, eventdata, handles)

% hObject handle to Edit (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% ——————————————————————–

function Help_Callback(hObject, eventdata, handles)

% hObject handle to Help (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% — Executes on button press in pushbutton7.

function pushbutton7_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton7 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

msg=请速度录音¡

msgbox(msg)

clear

global data1;

%global dataDN1;

AI = analoginput(winsound);

chan = addchannel(AI,1:2);

duration = 3; %1 second acquisition

set(AI,SampleRate,8000)

ActualRate = get(AI,SampleRate);

set(AI,SamplesPerTrigger,duration*ActualRate)

set(AI,TriggerType,Manual)

blocksize = get(AI,SamplesPerTrigger);

Fs = ActualRate;

start(AI)

trigger(AI)

[data1,time,abstime,events] = getdata(AI);

fname=sprintf(E:\\Matlab语音识别系统\\实时模版\\)

%dataDN1=wden(data1,heursure,s,one,5,sym8);denoise

wavwrite(data1,fname)

msgbox(fname)

% — Executes on button press in pushbutton8.

function pushbutton8_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton8 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global data1;

%global dataDN1;

sound(data1)

%sound(dataDN1)

axes%set to plot at axes1

plot(data1);

%plot(dataDN1);

xlabel(训练采样序列),ylabel(信号幅);

%xlabel(ѵÁ·²ÉÑùÐòÁÐ),ylabel(sym8С²¨½µÔëºóµÄÐźŷù);

grid on;

clear

% — Executes on button press in pushbutton9.

function pushbutton9_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton9 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

msg=请速度录音¡

msgbox(msg)

clear

global data2;

%global dataDN2;

AI = analoginput(winsound);

chan = addchannel(AI,1:2);

duration = 3; %1 second acquisition

set(AI,SampleRate,8000)

ActualRate = get(AI,SampleRate);

set(AI,SamplesPerTrigger,duration*ActualRate)

set(AI,TriggerType,Manual)

blocksize = get(AI,SamplesPerTrigger);

Fs = ActualRate;

start(AI)

trigger(AI)

[data2,time,abstime,events] = getdata(AI);

fname=sprintf(E:\\Matlab语音识别系统\\测试\\)

%dataDN1=wden(data1,heursure,s,one,5,sym8);denoise

wavwrite(data2,fname)

msgbox(fname)

% — Executes on button press in pushbutton10.

function pushbutton10_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton10 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global data2;

%global dataDN2;

sound(data2)

%sound(dataDN2)

axes%set to plot at axes1

plot(data2);

%plot(dataDN2);

xlabel(测试采样序列),ylabel(信号幅);

%xlabel(²âÊÔ²ÉÑùÐòÁÐ),ylabel(sym8С²¨½µÔëºóµÄÐźŷù);%%

grid on;

clear

% — Executes on button press in pushbutton11.

function pushbutton11_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton11 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global moodle ;

testDB(测试\,1,moodle)

% — Executes on button press in pushbutton12.

function pushbutton12_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton12 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

global moodle;

moodle = train(实时模板\,1)

% — Executes on selection change in popupmenu3.

function popupmenu3_Callback(hObject, eventdata, handles)

% hObject handle to popupmenu3 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,String) returns popupmenu3 contents as cell array

% contents{get(hObject,Value)} returns selected item from popupmenu3

str=get,String);

val=str2num(str{get,Value)});

switch val

case 1

case 2

case 3

case 4

case 5

case 6

case 7

case 8

case 9

end

% — Executes during object creation, after setting all properties.

function popupmenu3_CreateFcn(hObject, eventdata, handles)

% hObject handle to popupmenu3 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles empty – handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.

% See ISPC and COMPUTER.

if ispc && isequal(get(hObject,BackgroundColor), get(0,defaultUicontrolBackgroundColor))

set(hObject,BackgroundColor,white);

end

% — Executes on button press in pushbutton9.

function pushbutton13_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton9 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% — Executes on button press in pushbutton10.

function pushbutton14_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton10 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% — Executes during object creation, after setting all properties.

%function axes8_CreateFcn(hObject, eventdata, handles)

% hObject handle to axes8 (see GCBO)

% eventdata reserved – to be defined in a future version of MATLAB

% handles empty – handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes8

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片