基於MATLAB的語音信號處理
阿新 • • 發佈:2018-10-27
init http 功能 odi 句柄 open not clas 語音信號處理
一、圖形界面設計
1.新建GUI界面
2.新建空白頁
3.命名為"yydsp",打開界面
4.拖放控件
5.按預定功能修改界面
6.填寫Callback函數
未填寫前的代碼:
function varargout = yydsp(varargin)
% YYDSP MATLAB code for yydsp.fig
% YYDSP, by itself, creates a new YYDSP or raises the existing
% singleton*.
%
% H = YYDSP returns the handle to a new YYDSP or the handle to
% the existing singleton*.
%
% YYDSP(‘CALLBACK‘,hObject,eventData,handles,...) calls the local
% function named CALLBACK in YYDSP.M with the given input arguments.
%
% YYDSP(‘Property‘,‘Value‘,...) creates a new YYDSP or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before yydsp_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to yydsp_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE‘s Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help yydsp
% Last Modified by GUIDE v2.5 27-Oct-2018 13:35:56
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(‘gui_Name‘, mfilename, ...
‘gui_Singleton‘, gui_Singleton, ...
‘gui_OpeningFcn‘, @yydsp_OpeningFcn, ...
‘gui_OutputFcn‘, @yydsp_OutputFcn, ...
‘gui_LayoutFcn‘, [] , ...
‘gui_Callback‘, []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = 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 yydsp is made visible.
function yydsp_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 yydsp (see VARARGIN)
% Choose default command line output for yydsp
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes yydsp wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = yydsp_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} = handles.output;
% --- 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)
% --- 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
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (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 pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (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 pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (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 pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (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)
% --- 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)
% --- 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)
% --- 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)
% --- 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)
% --- 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)
% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton13 (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 pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton14 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
填寫後的代碼
1)打開文件部分
[filename,pathname]=uigetfile({‘*.*‘,‘ALL FILES‘},‘選擇聲音‘);%顯示模態對話框,
%列出當前文件夾中的文件,如果文件有效,點擊打開時會返回文件名,如果點擊取消,返回0
if isequal([filename pathname],[0,0])
return;
end
str=[pathname filename];%合成路徑+文件名
[temp,Fs]=audioread(str);%讀取音頻聲音
temp=temp(:,1); %取一行提取矩陣
temp1=resample(temp,80,441);%信號降采樣處理
handles.y=temp1;%降采樣的句柄
handles.y1=temp;%y1為原聲
handles.Fs=Fs;%采樣頻率
guidata(hObject,handles);%存儲或檢索 UI 數據
程序中,resample為信號降采樣處理,理解如下:
B=resample(x,90,250); %
采樣從250Hz降到90Hz,如果250在前,就是插值從90到250,可以看B的長度,250Hz采樣4000個數據等於90hz采樣1440個數據,這就是降采樣。
2)播放原聲,畫時頻圖
fs=handles.Fs;
Y=handles.y1;
Y=Y(:,1);%取單聲道
t1=1:length(Y);
t=t1/fs;
sound(Y,fs); %播放原聲
F = fft(Y);%快速傅裏葉變換
freq = linspace(-fs/2,fs/2,length(Y)+1);
freq(end) = [];
plot(handles.axes1,t,Y)
xlabel(handles.axes1,‘時間‘);
ylabel(handles.axes1,‘幅度‘);
title(handles.axes1,‘原聲音的波形‘);
y1=fft(Y);
plot(handles.axes4,abs(y1));
xlabel(handles.axes4,‘圓頻率‘);
ylabel(handles.axes4,‘幅度‘);
title(handles.axes4,‘未改變坐標軸的頻率特性‘);
plot(handles.axes2,freq,abs(fftshift(F)));
title(handles.axes2,‘原聲音的真實頻響‘);
xlabel(handles.axes2,‘圓頻率‘);
ylabel(handles.axes2,‘幅度‘);
title(handles.axes2,‘頻率特性‘);
3)男聲變女聲
FL = 80 ; % 幀移
WL = 240 ; % 窗長
P = 10 ; %預測系數個數
s = handles.y;
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.axes4,freq,abs(fftshift(F)));
xlabel(handles.axes4,‘圓頻率‘);
ylabel(handles.axes4,‘幅度‘);
title(handles.axes4,‘頻率特性‘);
handles.y=s_syn_t;
guidata(hObject,handles);
plot(handles.axes3,s_syn_t);
t1=1:length(s_syn_t);
t=t1/8000;
plot(handles.axes3,t,s_syn_t);
title(handles.axes3,‘時域圖‘);
xlabel(handles.axes3,‘時間‘);
ylabel(handles.axes3,‘幅度‘);
sound(handles.y,8000);
4)退出
delete(handles.figure1);
5)快放
fs=handles.Fs;
Y=handles.y1;
Y=Y(:,1);
F = fft(Y);
freq = linspace(-fs/2,fs/2,length(Y)+1);
freq(end) = [];
sound(Y,2*fs);
t1=1:length(Y);
t=t1/(2*fs);
plot(handles.axes3,t,Y)
title(handles.axes3,‘時域圖‘);
xlabel(handles.axes3,‘時間‘);
ylabel(handles.axes3,‘幅度‘);
plot(handles.axes4,freq,abs(fftshift(F)));
xlabel(handles.axes4,‘圓頻率‘);
ylabel(handles.axes4,‘幅度‘);
title(handles.axes4,‘頻率特性‘);
6)慢放
fs=handles.Fs;
Y=handles.y1;
Y=Y(:,1);
sound(Y,0.5*fs);
F = fft(Y);
freq = linspace(-fs/2,fs/2,length(Y)+1);
freq(end) = [];%
t1=1:length(Y);
t=t1/(0.5*fs);
plot(handles.axes3,t,Y)
title(handles.axes3,‘時域圖‘);
xlabel(handles.axes3,‘時間‘);
ylabel(handles.axes3,‘幅度‘);
plot(handles.axes4,freq,abs(fftshift(F)));
xlabel(handles.axes4,‘圓頻率‘);
ylabel(handles.axes4,‘幅度‘);
title(handles.axes4,‘頻率特性‘);
7)制造回音
fs=handles.Fs;
N=length(handles.y1);
x1=handles.y1(1:N);
x2=handles.y1(1:N);
x1=[x1,zeros(1,5000)];
x2=[zeros(1,4000),0.4*x2,zeros(1,1000)];
z=x1+x2;
F = fft(z);
freq = linspace(-fs/2,fs/2,length(z)+1);
freq(end) = [];
t1=1:length(z);
t=t1/fs;
plot(handles.axes3,t,z)
title(handles.axes3,‘含回音波形‘);
xlabel(handles.axes3,‘時間‘);
ylabel(handles.axes3,‘幅度‘);
plot(handles.axes4,freq,abs(fftshift(F)));
xlabel(handles.axes4,‘圓頻率‘);
ylabel(handles.axes4,‘幅度‘);
title(handles.axes4,‘頻率特性‘);
sound(z,fs);
8)回音還原
fs=handles.Fs;
N=length(handles.y1);
x1=handles.y1(1:N);
x2=handles.y1(1:N);
x3=handles.y1(1:N);
x1=[x1,zeros(1,5000)];
x2=[zeros(1,4000),0.4*x2,zeros(1,1000)];
z=x1+x2;
b=1;
a=zeros(1,N);
a(1)=1;
a(4001)=0.4;
z2=filter(b,a,z);
F = fft(z2);
freq = linspace(-fs/2,fs/2,length(z2)+1);
freq(end) = [];
t1=1:length(z2);
t=t1/fs;
plot(handles.axes3,t,z2)
title(handles.axes3,‘濾除回聲的波形‘);
xlabel(handles.axes3,‘時間‘);
ylabel(handles.axes3,‘幅度‘);
plot(handles.axes4,freq,abs(fftshift(F)));
xlabel(handles.axes4,‘圓頻率‘);
ylabel(handles.axes4,‘幅度‘);
title(handles.axes4,‘頻率特性‘);
sound(z2,fs);
9)制造噪聲
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.axes3,t,VNnoise)
xlabel(handles.axes3,‘時間‘);
ylabel(handles.axes3,‘幅度‘);
title(handles.axes3,‘添加噪聲的波形‘);
plot(handles.axes4,freq,abs(fftshift(F)));
xlabel(handles.axes4,‘圓頻率‘);
ylabel(handles.axes4,‘幅度‘);
title(handles.axes4,‘頻率特性‘);
sound(VNnoise,fs);
10)濾除噪聲
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‘;%向量維度一致
%[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,fs);
F = fft(result);
freq = linspace(-fs/2,fs/2,length(result)+1);
freq(end) = [];
t1=1:length(result);
t=t1/fs;
plot(handles.axes3,t,result)
xlabel(handles.axes3,‘時間‘);
ylabel(handles.axes3,‘幅度‘);
title(handles.axes3,‘添加噪聲的波形‘);
plot(handles.axes4,freq,abs(fftshift(F)));
xlabel(handles.axes4,‘圓頻率‘);
ylabel(handles.axes4,‘幅度‘);
title(handles.axes4,‘頻率特性‘);
11)左右聲道合唱
fs=handles.Fs;
sound(original,fs);
a1=1;
a2=-1;
b1=1;
b2=-1;
Soundleft=original(:,1);%左聲道
Soundright=original(:,2);%右聲道
newleft=Soundleft+Soundright; %新的左聲道為原來的全部聲道
newright=b1*Soundleft+b2*Soundright; %新的右聲道為原來的左聲道-原來的右
Sound(:,1)=newleft;
Sound(:,2)=newright;
bp=fir1(300,[500,2000]/(fs/2));
cutdown=filter(bp,1,Sound);
Sound_final=Sound-0.6*abs(cutdown);
sound(Sound_final,fs)
F = fft(Sound_final);
freq = linspace(-fs/2,fs/2,length(Sound_final)+1);
freq(end) = [];
t1=1:length(Sound_final);
t=t1/fs;
plot(handles.axes3,t,Sound_final)
xlabel(handles.axes3,‘時間‘);
ylabel(handles.axes3,‘幅度‘);
title(handles.axes3,‘時域波形‘);
plot(handles.axes4,freq,abs(fftshift(F)));
xlabel(handles.axes4,‘圓頻率‘);
ylabel(handles.axes4,‘幅度‘);
title(handles.axes4,‘頻率特性‘);
12)反放
fs=handles.Fs;
y=handles.y1;
M=length(y):-1:1;
rever=y(M);
sound(rever,fs);%反播
F = fft(rever);
freq = linspace(-fs/2,fs/2,length(rever)+1);
freq(end) = [];
t1=1:length(rever);
t=t1/fs;
plot(handles.axes3,t,rever)
xlabel(handles.axes3,‘時間‘);
ylabel(handles.axes3,‘幅度‘);
title(handles.axes3,‘反播的波形‘);
plot(handles.axes4,freq,abs(fftshift(F)));
xlabel(handles.axes4,‘圓頻率‘);
ylabel(handles.axes4,‘幅度‘);
title(handles.axes4,‘頻率特性‘);
基於MATLAB的語音信號處理