基於MATLAB的多功能語音處理器

1、設計功能

  1. 錄製音頻,保存音頻
  2. 對錄製的語音信號進行頻譜分析,肯定該段語音的主要頻率範圍;
  3. 利用採樣定理,對該段語音信號進行採樣,觀察不用採樣頻率(過採樣、欠採樣、臨界採樣)對信號的影響;
  4. 實現語音信號的快放、慢放、倒放、男女變聲;
  5. 對語音信號加噪,而後進行濾波,分析不一樣的濾波方式對信號的影響;
  6. 實現兩音頻的合成、拼接;
  7. 利用MATLAB GUI製做語音信號採集與分析演示系統;

2、設計步驟

1.建立GUI界面

2.新建空白界面

3.拖放控件,雙擊控件修改tag值和string

 

4.最後界面佈置爲圖示,右鍵點擊任何一個控件,進入回調函數callback。

 

5.在OpeningFcn中寫入下面程序

 

% --- Executes just before functionalDSP is made visible.
function functionalDSP_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 functionalDSP (see VARARGIN)

% Choose default command line output for functionalDSP
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);
h1=gcf;
set(h1,'Name','多功能語音處理器','Resize','on');
h2=uimenu(h1,'label','功能');
h3=uimenu(h2,'label','基本功能');
h4=uimenu(h2,'label','採樣定理');
h5=uimenu(h2,'label','快慢放');
h6=uimenu(h2,'label','加噪去噪');
h7=uimenu(h2,'label','濾波器設計');
h8=uimenu(h2,'label','合成拼接');
h12=uimenu(h1,'label','設計人員');
h13=uimenu(h12,'label','孫寧寧');
h14=uimenu(h12,'label','寶曆');
h15=uimenu(h12,'label','李佳桐');
h16=uimenu(h12,'label','馬寧澤');
h17=uimenu(h12,'label','王璐');
h18=uimenu(h12,'label','王智聰');
% UIWAIT makes functionalDSP wait for user response (see UIRESUME)
% uiwait(handles.h1);

 

 

6.編輯「錄製1」功能

 --- Executes on button press in record2.
function record2_Callback(hObject, eventdata, handles)
% hObject    handle to record2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
r=audiorecorder(10000,16,1);
recordblocking(r, 3);
g=getaudiodata(r);
m='C:\Users\孫寧寧\Desktop\孫寧寧程序\x1.wav';
audiowrite(m,g,10000);%將音頻寫入文件

 

7.編輯「錄製2」功能

 

 --- Executes on button press in record2.
function record2_Callback(hObject, eventdata, handles)
% hObject    handle to record2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
r=audiorecorder(10000,16,1);
recordblocking(r, 3);
g=getaudiodata(r);
m='C:\Users\孫寧寧\Desktop\孫寧寧程序\x2.wav';
audiowrite(m,g,10000);%將音頻寫入文件

 

 

 

8.編輯「讀取」功能

%讀取信號
function read_Callback(hObject, eventdata, handles)
% hObject    handle to read (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({'.'},'讀取音頻文件');
%文件打開對話框,返回文件名和文件路徑,當文件存在時會成功返回,若是不存在,則返回不存在。
if isequal([filename pathname],[0,0])%用戶取消對話框返回0
return;
end
str=[pathname filename];%將文件名和路徑名組合爲一個字符串,賦值給str。
[x1,Fs]=audioread(str);%讀取聲音信號,採樣值放在向量x1中,fs爲採樣頻率
x=x1(:,1);  %對雙聲道信號取單聲道,若是是x=x1(1:5000,1),則表示取了5000點。
handles.y1=x;%將原先採樣的序列向量x給句柄y1
handles.Fs=Fs;%採樣頻率句柄
guidata(hObject,handles);%儲存handles

 9.編輯「分析」功能

% --- Executes on button press in original.
function original_Callback(hObject, eventdata, handles)
% hObject    handle to original (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fs1=handles.Fs;%fs1爲原信號的採樣頻率
Y=handles.y1;%Y爲原採樣的原信號
Y=Y(:,1);%取單值
sound(Y,fs1);%播放聲音
M=length(Y);%M爲信號長度
t=0:1/fs1:(M-1)/fs1;%傅里葉變換時間長度(30000個點,變換爲以s爲單位)
plot(handles.shiyu1,t,Y);%繪製時域圖
xlabel(handles.shiyu1,'時間/s');
ylabel(handles.shiyu1,'幅度');
title(handles.shiyu1,'原聲時域圖');
F=fft(Y,M);%快速傅里葉變換
f=linspace(-fs1/2,fs1/2,length(Y)+1);
f(end) = [];;%頻率序列(0~50000個點)
plot(handles.pinyu1,f,abs(F));%繪製頻譜圖
title(handles.pinyu1,'原聲頻譜圖');
xlabel(handles.pinyu1,'頻率/Hz');
ylabel(handles.pinyu1,'幅值');

10.編輯「原信號」功能

 

function origin_Callback(hObject, eventdata, handles)
% hObject    handle to origin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
wm=10;
wc=wm;
Ts=pi/wm;
ws=2*pi/Ts;
n=-100:100;
nTs=n*Ts;
f=sinc(nTs/pi);
t=-20:0.2:20;
f=sinc(t/pi);
N=length(f);
plot(handles.shiyu1,t,f);%繪製時域圖
xlabel(handles.shiyu1,'時間/s');
ylabel(handles.shiyu1,'幅度');
title(handles.shiyu1,'原信號時域圖');
F=fft(f);%快速傅里葉變換
f=[0:N-1]*wc/N;%頻率序列
plot(handles.pinyu1,f,abs(F));%繪製頻譜圖
title(handles.pinyu1,'原信號頻譜圖');
xlabel(handles.pinyu1,'頻率/Hz');
ylabel(handles.pinyu1,'幅值');

 

11.編輯「欠採樣」功能

 

% --- Executes on button press in qian.
function qian_Callback(hObject, eventdata, handles)
% hObject    handle to qian (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
wm=10;%帶寬
wc=wm;%頻率
Ts=2*pi/wm;%週期
ws=2*pi/Ts;
n=-100:100;
nTs=n*Ts;
f=sinc(nTs/pi);
N=length(f);
t=-20:0.2:20;
stem(handles.shiyu4,t,f);%繪製時域圖
xlabel(handles.shiyu4,'時間/s');
ylabel(handles.shiyu4,'幅度');
title(handles.shiyu4,'欠採樣時域圖');
F=fft(f);%快速傅里葉變換
f=[0:N-1]*wc/N;%頻率序列
plot(handles.pinyu4,f,abs(F));%繪製頻譜圖
title(handles.pinyu4,'欠採樣頻譜圖');
xlabel(handles.pinyu4,'頻率/Hz');
ylabel(handles.pinyu4,'幅值');

 

12.編輯「臨界採樣」功能

 

% --- Executes on button press in lincai.
function lincai_Callback(hObject, eventdata, handles)
% hObject    handle to lincai (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
wm=10;%帶寬
wc=wm;%頻率
Ts=pi/wm;%週期
ws=2*pi/wm;
n=-100:100;
nTs=n*Ts;
f=sinc(nTs/pi);
t=-20:0.2:20;
N=length(f);
stem(handles.shiyu3,t,f);%繪製時域圖
xlabel(handles.shiyu3,'時間/s');
ylabel(handles.shiyu3,'幅度');
title(handles.shiyu3,'臨界採樣時域圖');
F=fft(f);%快速傅里葉變換
f=[0:N-1]*wc/N;%頻率序列
plot(handles.pinyu3,f,abs(F));%繪製頻譜圖
title(handles.pinyu3,'臨界採樣頻譜圖');
xlabel(handles.pinyu3,'頻率/Hz');
ylabel(handles.pinyu3,'幅值');

 

13.編輯「過採樣」功能

 

% --- Executes on button press in guo.
function guo_Callback(hObject, eventdata, handles)
% hObject    handle to guo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
wm=10;
wc=wm;
Ts=0.5*pi/wm;
ws=2*pi/Ts;
n=-100:100;
nTs=n*Ts;
f=sinc(nTs/pi);
t=-20:0.2:20;
f=sinc(t/pi);
N=length(f);
stem(handles.shiyu2,t,f);%繪製時域圖
xlabel(handles.shiyu2,'時間/s');
ylabel(handles.shiyu2,'幅度');
title(handles.shiyu2,'過採樣時域圖');
F=fft(f);%快速傅里葉變換
f=[0:N-1]*wc/N;%頻率序列
plot(handles.pinyu2,f,abs(F));%繪製頻譜圖
title(handles.pinyu2,'過採樣頻譜圖');
xlabel(handles.pinyu2,'頻率/Hz');
ylabel(handles.pinyu2,'幅值');

 

14.編輯「倒放」功能

% --- Executes on button press in dao.
function dao_Callback(hObject, eventdata, handles)
% hObject    handle to dao (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
dfs=handles.Fs;
dy=handles.y1;
M=length(dy):-1:1;%M序列顛倒
rY=dy(M);%顛倒後的信號
sound(rY,dfs);%倒放
t=0:1/dfs:(length(rY)-1)/dfs;%時間序列
plot(handles.shiyu2,t,rY);%繪製時域圖
xlabel(handles.shiyu2,'時間/s');
ylabel(handles.shiyu2,'幅度');
title(handles.shiyu2,'倒放時域圖');
F=fft(rY,length(rY));%快速傅里葉變換
f=[0:length(dy)-1]*dfs/length(dy);%頻率序列
plot(handles.pinyu2,f,abs(F));%繪製頻譜圖
title(handles.pinyu2,'倒放頻譜圖');
xlabel(handles.pinyu2,'頻率/Hz');
ylabel(handles.pinyu2,'幅值');

15.編輯「快放」功能

% --- Executes on button press in kuai.
function kuai_Callback(hObject, eventdata, handles)
% hObject    handle to kuai (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
kfs=3*handles.Fs; %fs2爲採樣頻率
kx=handles.y1;%mx爲原信號
sound(kx,kfs);
M=length(kx);%M爲信號長度
t=0:1/kfs:(M-1)/kfs;%傅里葉變換時間長度
plot(handles.shiyu3,t,kx);%繪製時域圖
xlabel(handles.shiyu3,'時間/s');
ylabel(handles.shiyu3,'幅度');
title(handles.shiyu3,'快放時域圖');
F=fft(kx,M);%快速傅里葉變換
f=[0:M-1]*kfs/M;%頻率序列
plot(handles.pinyu3,f,abs(F));%繪製頻譜圖
title(handles.pinyu3,'快放頻譜圖');
xlabel(handles.pinyu3,'頻率/Hz');
ylabel(handles.pinyu3,'幅值');

16.編輯「慢放」功能

% --- Executes on button press in man.
function man_Callback(hObject, eventdata, handles)
% hObject    handle to man (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
mfs=0.7*handles.Fs; %fs2爲採樣頻率
mx=handles.y1;%mx爲原信號
sound(mx,mfs);
M=length(mx);%M爲信號長度
t=0:1/mfs:(M-1)/mfs;%傅里葉變換時間長度
plot(handles.shiyu4,t,mx);%繪製時域圖
xlabel(handles.shiyu4,'時間/s');
ylabel(handles.shiyu4,'幅度');
title(handles.shiyu4,'慢放時域圖');
F=fft(mx,M);%快速傅里葉變換
f=[0:M-1]*mfs/M;%頻率序列
plot(handles.pinyu4,f,abs(F));%繪製頻譜圖
title(handles.pinyu4,'慢放頻譜圖');
xlabel(handles.pinyu4,'頻率/Hz');
ylabel(handles.pinyu4,'幅值');

17.編輯「音一」功能

 

function yin1_Callback(hObject, eventdata, handles)
% hObject    handle to yin1 (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({'.'},'讀取音頻文件');
%文件打開對話框,返回文件名和文件路徑,當文件存在時會成功返回,若是不存在,則返回不存在。
if isequal([filename pathname],[0,0])%用戶取消對話框返回0
return;
end
str=[pathname filename];%將文件名和路徑名組合爲一個字符串,賦值給str。
[x1,Fs]=audioread(str);%讀取聲音信號,採樣值放在向量x1中,fs爲採樣頻率
x=x1(:,1);  %對雙聲道信號取單聲道,若是是x=x1(1:5000,1),則表示取了5000點。
Y=x;%取單值
fs1=Fs;
sound(Y,fs1);%播放聲音
M=length(Y);%M爲信號長度
t=0:1/fs1:(M-1)/fs1;%傅里葉變換時間長度(30000個點,變換爲以s爲單位)
plot(handles.shiyu1,t,Y);%繪製時域圖
xlabel(handles.shiyu1,'時間/s');
ylabel(handles.shiyu1,'幅度');
title(handles.shiyu1,'音一時域圖');
F=fft(Y,M);%快速傅里葉變換
f=[0:M-1]*fs1/M;%頻率序列(0~50000個點)
plot(handles.pinyu1,f,abs(F));%繪製頻譜圖
title(handles.pinyu1,'音一頻譜圖');
xlabel(handles.pinyu1,'頻率/Hz');
ylabel(handles.pinyu1,'幅值');
handles.y2=x;%將原先採樣的序列向量x給句柄y1
handles.Fs2=Fs;%採樣頻率句柄
guidata(hObject,handles);%儲存handles

 

 

 

18.編輯「音二」功能

 

% --- Executes on button press in yin2.
function yin2_Callback(hObject, eventdata, handles)
% hObject    handle to yin2 (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({'.'},'讀取音頻文件');
%文件打開對話框,返回文件名和文件路徑,當文件存在時會成功返回,若是不存在,則返回不存在。
if isequal([filename pathname],[0,0])%用戶取消對話框返回0
return;
end
str=[pathname filename];%將文件名和路徑名組合爲一個字符串,賦值給str。
[x1,Fs]=audioread(str);%讀取聲音信號,採樣值放在向量x1中,fs爲採樣頻率
x=x1(:,1);  %對雙聲道信號取單聲道,若是是x=x1(1:5000,1),則表示取了5000點。
Y=x;%取單值
fs1=Fs;
sound(Y,fs1);%播放聲音
M=length(Y);%M爲信號長度
t=0:1/fs1:(M-1)/fs1;%傅里葉變換時間長度(30000個點,變換爲以s爲單位)
plot(handles.shiyu2,t,Y);%繪製時域圖
xlabel(handles.shiyu2,'時間/s');
ylabel(handles.shiyu2,'幅度');
title(handles.shiyu2,'音二時域圖');
F=fft(Y,M);%快速傅里葉變換
f=[0:M-1]*fs1/M;%頻率序列(0~50000個點)
plot(handles.pinyu2,f,abs(F));%繪製頻譜圖
title(handles.pinyu2,'音二頻譜圖');
xlabel(handles.pinyu2,'頻率/Hz');
ylabel(handles.pinyu2,'幅值');
handles.y3=x;%將原先採樣的序列向量x給句柄y1
handles.Fs3=Fs;%採樣頻率句柄
guidata(hObject,handles);%儲存handles

 

 

 

19.編輯「合成」功能

% --- Executes on button press in hecheng.
function hecheng_Callback(hObject, eventdata, handles)
% hObject    handle to hecheng (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y=handles.y2;
fs=handles.Fs2;
y2=handles.y3;
[m,n]=size(y);%查看y的大小
[m2,n2]=size(y2);%查看y2的大小
z=zeros(max(m,m2)-min(m,m2),n);%生成一個零矩陣
if length(y)<length(y2);
y1=[y;z];%將y與零矩陣z組合,變成一個與Y2同等大小的矩陣
new=y1+y2;%生成的新的矩陣與y2相加,獲得新的音頻的矩陣
sound(new,fs);%播放新的音頻
else
    y1=[y2;z];%同上,當y2的長度較小時,將零矩陣和Y2組合
    new=y1+y;%生成新的矩陣和Y相加
    sound(new,fs);%播放
end;
time_new=(1:length(new))/fs;
N=length(new);
plot(handles.shiyu3,time_new,new);
xlabel(handles.shiyu3,'時間/s');
ylabel(handles.shiyu3,'幅度');
title(handles.shiyu3,'拼接信號時域圖');
F=fft(new);%快速傅里葉變換
f=[0:N-1]*fs/N;%頻率序列
plot(handles.pinyu3,f,abs(F));%繪製頻譜圖
title(handles.pinyu3,'原信號頻譜圖');
xlabel(handles.pinyu3,'頻率/Hz');
ylabel(handles.pinyu3,'幅值');

 

20.編輯「拼接」功能

% --- Executes on button press in pj.
function pj_Callback(hObject, eventdata, handles)
% hObject    handle to pj (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
y=handles.y2;
fs=handles.Fs2;
y2=handles.y3;
[m,n]=size(y);%查看y的大小
[m2,n2]=size(y2);%查看y2的大小
z=zeros(max(m,m2)-min(m,m2),n);%生成一個零矩陣
if length(y)<=length(y2);
y1=[y;z];%將y與零矩陣z組合,變成一個與Y2同等大小的矩陣
new=[y1;y2];%生成的新的矩陣與y2相加,獲得新的音頻的矩陣
sound(new,fs);%播放新的音頻
else
    y1=[y2;z];%同上,當y2的長度較小時,將零矩陣和Y2組合
    new=[y1;y2];%生成新的矩陣和Y相加
    sound(new,fs);%播放
end;
time_new=(1:length(new))/fs;
N=length(new);
plot(handles.shiyu4,time_new,new);
xlabel(handles.shiyu4,'時間/s');
ylabel(handles.shiyu4,'幅度');
title(handles.shiyu4,'拼接信號時域圖');
F=fft(new);%快速傅里葉變換
f=[0:N-1]*fs/N;%頻率序列
plot(handles.pinyu4,f,abs(F));%繪製頻譜圖
title(handles.pinyu4,'原信號頻譜圖');
xlabel(handles.pinyu4,'頻率/Hz');
ylabel(handles.pinyu4,'幅值');

21.編輯「加噪」功能

% --- Executes on button press in jia.
function jia_Callback(hObject, eventdata, handles)
% hObject    handle to jia (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fs=handles.Fs;
x=handles.y1;
y=x(:,1);  %取一行提取矩陣
noise=0.2*sin(pi*20000*(1:length(y))/fs)+0.3*sin(pi*21000*(1:length(y))/fs)...
    +0.4*sin(pi*22000*(1:length(y))/fs);%噪聲 10000rad/s+10500+11000
VNnoise=y+noise';%向量維度一致
F = fft(VNnoise);
freq = linspace(-fs/2,fs/2,length(VNnoise)+1);
freq(end) = [];
t1=1:length(VNnoise);
t=t1/fs;
plot(handles.shiyu3,t,VNnoise)
xlabel(handles.shiyu3,'時間');
ylabel(handles.shiyu3,'幅度');
title(handles.shiyu3,'加噪的時域圖');
plot(handles.pinyu3,freq,abs(fftshift(F)));
xlabel(handles.pinyu3,'圓頻率');
ylabel(handles.pinyu3,'幅度');
title(handles.pinyu3,'加噪的頻譜圖');
sound(VNnoise,fs);

22.編輯「去噪」功能

 

function qu_Callback(hObject, eventdata, handles)
% hObject    handle to qu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
qfs=handles.Fs;
X=handles.y1;
y=X(:,1);  %取一行提取矩陣
n=0.2*sin(pi*20000*(1:length(y))/qfs)+0.3*sin(pi*21000*(1:length(y))/qfs)+0.4*sin(pi*22000*(1:length(y))/qfs);%噪聲 10000rad/s+10500+11000
jz=y+n';%向量維度一致
%[b,a] = butter(8,5000*2/fs,'LOW') ;   %巴特沃斯濾波器
%result=filter(b,a,VNnoise);
Hd = ditong1;%Fdatool濾波
result=filter(Hd,X);
result=result(:,1);
sound(result,qfs);
F = fft(result);
freq = linspace(-qfs/2,qfs/2,length(result)+1);
freq(end) = [];
t1=1:length(result);
t=t1/qfs;
plot(handles.shiyu4,t,result)
xlabel(handles.shiyu4,'時間');
ylabel(handles.shiyu4,'幅度');
title(handles.shiyu4,'去噪的時域圖');
plot(handles.pinyu4,freq,abs(fftshift(F)));
xlabel(handles.pinyu4,'頻率');
ylabel(handles.pinyu4,'幅度');
title(handles.pinyu4,'去噪的頻譜圖');

 

23.編輯「回聲」功能

 

% --- Executes on button press in hui.
function hui_Callback(hObject, eventdata, handles)
% hObject    handle to hui (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fs=handles.Fs;
y=handles.y1;
x1=[zeros(9000,1);y];
x2=[y;zeros(9000,1)];
z=x1+x2;
sound(2*z,fs);
F=fft(z);
f=linspace(-fs/2,fs/2,length(z)+1);
f(end) = [];
t1=1:length(z);
t=t1/fs;
plot(handles.shiyu2,t,z);
title(handles.shiyu2,'加入迴音時域圖');
xlabel(handles.shiyu2,'時間');
ylabel(handles.shiyu2,'幅度');
plot(handles.pinyu2,f,abs(F));
xlabel(handles.pinyu2,'圓頻率');
ylabel(handles.pinyu2,'幅度');
title(handles.pinyu2,'加入迴音頻譜圖');
sound(z,fs);

 

24.編輯「去回聲」功能

 

% --- Executes on button press in lv.
function lv_Callback(hObject, eventdata, handles)
% hObject    handle to lv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fs=handles.Fs;
y=handles.y1;
N=length(y);
x1=[zeros(9000,1);y];
x2=[y;zeros(9000,1)];
z=x1+x2;
b=1;
a=zeros(1,N);
a(1)=1;
a(9001)=1;
z2=filter(b,a,z);
F=fft(z2);
f=linspace(-fs/2,fs/2,length(z2)+1);
f(end) = [];
t1=1:length(z2);
t=t1/fs;
plot(handles.shiyu4,t,z2);
title(handles.shiyu4,'濾除迴音時域圖');
xlabel(handles.shiyu4,'時間');
ylabel(handles.shiyu4,'幅度');
plot(handles.pinyu4,f,abs(F));
xlabel(handles.pinyu4,'頻率');
ylabel(handles.pinyu4,'幅度');
title(handles.pinyu4,'濾除迴音頻譜圖');
sound(z2,fs);

 

25.編輯「男女聲轉化」功能

 

% --- Executes on button press in nv.
function nv_Callback(hObject, eventdata, handles)
% hObject    handle to nv (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
FL =  80 ;               % 幀移
WL = 240 ;               % 窗長
    P = 10 ;                 %預測係數個數
    s = handles.y1;
fs = handles.Fs;
% 定義常數
    s = s/max(s);             % 歸一化
    L = length(s);            % 讀入語音長度
    FN = floor(L/FL)-2;       % 計算幀長,floor;向負無窮方向
% 預測和重建濾波器
exc = zeros(L,1);         % 激勵信號,double類零矩陣L行1列
zi_pre = zeros(P,1);      % 預測濾波器狀態
s_rec = zeros(L,1);       % 重建語音
zi_rec = zeros(P,1);
% 變調濾波器
exc_syn_t = zeros(L,1);   % 合成的激勵信號,建立一個L行1列的0脈衝
s_syn_t = zeros(L,1);     % 合成語音
last_syn_t = 0;           % 存儲上一個段的最後一個脈衝的下標
zi_syn_t = zeros(P,1);    % 合成濾波器
hw = hamming(WL);         %漢明窗
%濾波器
% 依次處理每幀語音
for n = 3:FN             %從第三個子數組開始
% 計算預測係數
s_w = s(n*FL-WL+1:n*FL).*hw;    %漢明窗加權
        [A,E]=lpc(s_w,P);               %線性預測計算預測係數
% A是預測係數,E會被用來計算合成激勵的能量
s_f=s((n-1)*FL+1:n*FL);        % 本幀語音  
%利用filter函數重建語音
[exc1,zi_pre] = filter(A,1,s_f,zi_pre); 
exc((n-1)*FL+1:n*FL) = exc1;           %計算激勵
%利用filter函數重建語音
        [s_rec1,zi_rec] = filter(1,A,exc1,zi_rec);
s_rec((n-1)*FL+1:n*FL) = s_rec1; %重建語音
% 下面只有獲得exc後才能夠
s_Pitch = exc(n*FL-222:n*FL);
        PT(n) = findpitch(s_Pitch);    %計算基音週期pt
        G = sqrt(E*PT(n));            %計算合成激勵的能量G
  PT1 =floor(PT(n)/2);    %減少基音週期
poles = roots(A);
deltaOMG =100*2*pi/fs;

for p=1:10   %增長共振峯
if imag(poles(p))>0
poles(p) = poles(p)*exp(1j*deltaOMG);
elseif imag(poles(p))<0 
poles(p) = poles(p)*exp(-1j*deltaOMG);
end
end
 A1=poly(poles);
 tempn_syn_t=(1:n*FL-last_syn_t);
        exc_syn1_t = zeros(length(tempn_syn_t),1);
        exc_syn1_t(mod(tempn_syn_t,PT1)==0) = G; 
        exc_syn1_t = exc_syn1_t((n-1)*FL-last_syn_t+1:n*FL-last_syn_t);
        [s_syn1_t,zi_syn_t] = filter(1,A1,exc_syn1_t,zi_syn_t);
exc_syn_t((n-1)*FL+1:n*FL) = exc_syn1_t;        %合成激勵
s_syn_t((n-1)*FL+1:n*FL) = s_syn1_t;            %合成語音
last_syn_t = last_syn_t+PT1*floor((n*FL-last_syn_t)/PT1);
end
Y = s_syn_t;
F = fft(Y);
freq = linspace(-fs/2,fs/2,length(Y)+1);
freq(end) = [];
plot(handles.pinyu2,freq,abs(fftshift(F)));
     xlabel(handles.pinyu2,'圓頻率');
     ylabel(handles.pinyu2,'幅度');
     title(handles.pinyu2,'頻率特性');
     handles.y=s_syn_t;
     guidata(hObject,handles);
plot(handles.shiyu2,s_syn_t);
    t1=1:length(s_syn_t);
    t=t1/8000;
    plot(handles.shiyu2,t,s_syn_t);
    title(handles.shiyu2,'時域圖');
    xlabel(handles.shiyu2,'時間');
    ylabel(handles.shiyu2,'幅度');
    sound(handles.y,8000); 

3、導出GUI界面爲可執行文件

1.在命令行中輸入「deploytool」

 

2.在彈出的窗口中選擇第一個

 3.選擇文件,填寫信息

 4.填寫完畢後,package的√會變綠,點擊√

 5.保存工程

 

6.等候打包

7.生成.exe文件

8.完成數組

相關文章
相關標籤/搜索