
前一阵子用MATLAB GUI做了一个语音信号采集与分析的界面。功能包括录音、播放、时域、频域分析吧。也导入本地文件进行分析。
效果如下:
源码如下:
function varargout = GUI_1(varargin)
% GUI_1 MATLAB code for GUI_1.fig
% GUI_1, by itself, creates a new GUI_1 or raises the existing
% singleton*.
%
% H = GUI_1 returns the handle to a new GUI_1 or the handle to
% the existing singleton*.
%
% GUI_1('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GUI_1.M with the given input arguments.
%
% GUI_1('Property','Value',...) creates a new GUI_1 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_1_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 GUI_1
% Last Modified by GUIDE v2.5 25-May-2015 20:52:27
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_1_OpeningFcn, ...
'gui_OutputFcn', @GUI_1_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 GUI_1 is made visible.
function GUI_1_OpeningFcn(hObject, ~, 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 GUI_1 (see VARARGIN)
% Choose default command line output for GUI_1
set(handles.axes_t_m,'visible','off');
set(handles.axes_fft_m,'visible','off');
set(handles.axes_t_w,'visible','off');
set(handles.axes_fft_w,'visible','off');
global local_yn;
local_yn=0;
% axes(handles.axes_t_m)
% image(a);
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI_1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
ha=axes('units','normalized','position',[0 0 1 1]);
uistack(ha,'down')
II=imread('暖色.JPG');
image(II)
colormap gray
set(ha,'handlevisibility','off','visible','off');
%--- Outputs from this function are returned to the command line.
function varargout = GUI_1_OutputFcn(~, ~, 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 pushbutton_luyin.
function pushbutton_luyin_Callback(hObject, ~, handles)
% hObject handle to pushbutton_luyin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% global recObj;
% keyboard;
% global sex_flag
% switch sex_flag
% case 0
% set(handles.axes_t_m,'visible','on');
% set(handles.axes_fft_m,'visible','on');
% set(handles.axes_t_w,'visible','off');
% set(handles.axes_fft_w,'visible','off');
% case 1
% set(handles.axes_t_m,'visible','off');
% set(handles.axes_fft_m,'visible','off');
% set(handles.axes_t_w,'visible','off');
% set(handles.axes_fft_w,'visible','off');
% case 2
% set(handles.axes_t_m,'visible','off');
% set(handles.axes_fft_m,'visible','off');
% set(handles.axes_t_w,'visible','off');
% set(handles.axes_fft_w,'visible','off');
% end
global local_yn;
local_yn=0;
fs_chose=handles.fs_chose;
nBits=8;
nChannels=1;
%canshu={fs_chose,nBits,nChannels};
if(str2double(get(handles.et_time,'String'))==0) %添加str2double 后正常
set(handles.set_time,'String','时间参数输入错误!');
msgbox('认真点好吗亲 输入时间参数先。','错误提示','Icon');
set(handles.set_time,'BackgroundColor','red');
pause(1);
set(handles.set_time,'BackgroundColor','default');
set(handles.set_time,'String','请输入录音时长');
else
%cla (handles.axes_t_m,'reset');
% set(handles.axes_t_m,'visible','off');
set(handles.pushbutton_luyin,'BackgroundColor','green');
set(handles.pushbutton_luyin,'String','正在录音...');
recObj = audiorecorder(fs_chose,nBits,nChannels);
disp('Start speaking.')
recordblocking(recObj, handles.recor_time);
disp('End of Recording.');
set(handles.pushbutton_luyin,'BackgroundColor','yellow');
set(handles.pushbutton_luyin,'String','录音结束');
%play(recObj);
pause(1);
%新加代码 加入语音识别---刘帅20150526
% myRecording = getaudiodata(recObj);
% y1=myRecording; % ù
% L=length(y1);
% NFFT = 2^nextpow2(L); % Next power of 2 from length of y
% Y = fft(y1,NFFT)/L;
% % f = fs_chose/2*linspace(0,1,NFFT/2+1);
% % keyboard;
% a=0;
% for i=1:fs_chose/2
% % if (2*abs(Y))>0.00015
% if 2*abs(Y)>(1e-5)
% %if(i==2)
% %msgbox('你个','性别提示','Icon');
% a=a+1;
% end
% end
% if a>10
% msgbox('你个','性别提示','Icon');
% end
% keyboard;
% axes(handles.axes_fft_m);
% plot(f,2*abs(Y(1:NFFT/2+1)));
%语音识别刘帅 20150526
set(handles.pushbutton_luyin,'BackgroundColor','red');
set(handles.pushbutton_luyin,'String','点我录音');
handles.recObj=recObj;
guidata(hObject,handles); %更新handles里的数据
end
% % 回放录音数据
% play(recObj);
% % 获取录音数据
% myRecording = getaudiodata(recObj);
% % 绘制录音数据波形
% plot(myRecording);
%keyboard;
% --- Executes on button press in pushbutton_plot.
function pushbutton_plot_Callback(hObject, ~, handles)
% hObject handle to pushbutton_plot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% 回放录音数据
%set(handles.axes_t_m,'visible','on');
%set(handles.axes_fft_m,'visible','on');
global local_yn;
global sex_flag;
switch local_yn
case 0
if(str2double(get(handles.et_time,'String'))==0) %添加str2double 后正常
msgbox('亲 别逗好吗?请录音或者选择本地文件先。 ','Error','Icon');
%errordlg('亲 别逗好吗?请输入时间参数或者选择本地文件先。','Error','on');
else
switch sex_flag
case 0
recObj=handles.recObj;
% 获取录音数据
%myRecording = getaudiodata(recObj,'uint8');
myRecording = getaudiodata(recObj);
d=myRecording;
name=get(handles.et_savme,'String');
%audiowrite(name,d,8000);
audiowrite(name,d,handles.fs_chose);
%msgbox('数据保存成功','消息提示','Icon');
% 绘制录音数据波形
axes(handles.axes_t_m);
draw_m=plot(myRecording);
title('时域信号');
xlabel('t');
ylabel('幅值');
% Fs=16000; %
fs=handles.fs_chose; %采集频率
y1=myRecording; % 一维矩阵
L=length(y1); % 计算元素个数
NFFT = 2^nextpow2(L); % Next power of 2 from length of y 提高运算速度
Y = fft(y1,NFFT)/L; %傅里叶变换 NFFT级蝶形运算 ?
f = fs/2*linspace(0,1,NFFT/2+1); % x轴点数
% Plot single-sided amplitude spectrum.
axes(handles.axes_fft_m);
draw_fft_m=plot(f,2*abs(Y(1:NFFT/2+1)));
title('频域信号')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
% xFFT = fft(data); % ±
% xfft = abs(xFFT); % ·ù
% % Avoid taking the log of 0.
% index = xfft == 0;
% xfft(index) = 1e-17;
%
% mag = 20*log10(xfft); % ·ù·±
% mag = mag(1:blockSize/2);
%
% f = (0:length(mag)-1)*Fs/blockSize;
%f = f(:);
% axes(handles.axes_fft_m);
% draw_fft_m=plot(f(:,1),mag(:,1));
% ylabel('Magnitude (dB)');
% xlabel('Frequency (Hz)');
% title('频谱图');
% Fs=1000;
% nbit=24;
% wavwrite(myRecording,1000,24,'test_1.wav'); %MATLAB 写wav文件,看最大最小值
% [myRecording_fft,Fs,nbits] = wavread('test_1.wav'); % MATLAB读wav文件,看最大最小值,预计是在-1和1之间
% N=length(myRecording_fft)
% Fm=fft(myRecording_fft,N);
% axes(handles.axes_fft_m);
% draw_fft_m=plot(handles.axes_fft_m,100*abs(Fm));
% title('频域信号');
% xlabel('w');
% ylabel('幅值');
handles.draw_fft_m=draw_fft_m;
guidata(hObject,handles);
handles.draw_plot_m=draw_m;
guidata(hObject,handles);
case 1
recObj=handles.recObj;
% 获取录音数据
myRecording = getaudiodata(recObj);
% 绘制录音数据波形
axes(handles.axes_t_w);
draw_w=plot(handles.axes_t_w,myRecording,'m');
ylabel('Amplitude');
xlabel('Time ');
title('时域信号');
fs=handles.fs_chose; %
y1=myRecording; %
L=length(y1);
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y1,NFFT)/L;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
axes(handles.axes_fft_w);
draw_fft_w=plot(f,2*abs(Y(1:NFFT/2+1)),'m');
title('频域信号')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
% xFFT = fft(data); %
% xfft = abs(xFFT); %
% % Avoid taking the log of 0.
% index = xfft == 0;
% xfft(index) = 1e-17;
%
% mag = 20*log10(xfft); %
% mag = mag(1:blockSize/2);
%
% f = (0:length(mag)-1)*Fs/blockSize;
% f = f(:);
% axes(handles.axes_fft_w);
% draw_fft_w=plot(f(:,1),mag(:,1));
% ylabel('Magnitude (dB)');
% xlabel('Frequency (Hz)');
% title('频谱图');
% Fw=fft(myRecording,4096);
% axes(handles.axes_fft_w);
% draw_fft_w=plot(handles.axes_fft_w,abs(Fw));
% title('频域信号');
% xlabel('w');
% ylabel('幅值');
handles.draw_fft_w=draw_fft_w;
guidata(hObject,handles);
handles.draw_plot_w=draw_w;
guidata(hObject,handles);
% case 2
% msgbox('妖孽哪里逃','haha','Icon');
end
end
case 1
FileName=handles.FileName;
[y1,fs]=audioread(FileName);
axes(handles.axes_t_m);
draw_fft_m=plot(y1);
ylabel('Amplitude');
xlabel('Time ');
title('时域信号');
L=length(y1);
%%set(gca,'ylim',[0 2])
%% myRecording = getaudiodata(aa,'uint8');
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y1,NFFT)/L;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
axes(handles.axes_fft_m);
draw_fft_m=plot(f,2*abs(Y(1:NFFT/2+1)));
title('频域信号');
xlabel('Frequency (Hz)');
ylabel('|Y(f)|');
% Fs=16000;
% %data=myRecording; %
% blockSize=length(data);
% xFFT = fft(data); %
% xfft = abs(xFFT); %
% % Avoid taking the log of 0.
% index = xfft == 0;
% xfft(index) = 1e-17;
% mag = 20*log10(xfft); %
% mag = mag(1:blockSize/2);
% f = (0:length(mag)-1)*Fs/blockSize;
% f = f(:);
% axes(handles.axes_fft_m);
% draw_fft_m=plot(f(:,1),mag(:,1));
% ylabel('Magnitude (dB)');
% xlabel('Frequency (Hz)');
% title('频谱图');
end
% --- Executes on button press in pushbutton_play.
function pushbutton_play_Callback(hObject, ~, handles)
% hObject handle to pushbutton_play (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global local_yn;
%keyboard;
switch local_yn
case 0
if(str2double(get(handles.et_time,'String'))==0) %添加str2double 后正常
msgbox('亲 别逗好吗?请录音或者选择本地文件先。 ','Error','Icon');
%errordlg('亲 别逗好吗?请输入时间参数或者选择本地文件先。','Error','on');
else
recObj_play=handles.recObj;
play(recObj_play);
set(hObject,'BackgroundColor','green');
set(hObject,'String','正在播放');
pause(handles.recor_time);
set(hObject,'BackgroundColor','yellow');
set(hObject,'String','播放完毕');
pause(1);
set(hObject,'String','点我播放');
guidata(hObject,handles);
end
case 1
[y ,fs]=audioread(handles.FileName);
sound(y,fs);
end
function et_time_Callback(hObject, ~, handles)
% hObject handle to et_time (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of et_time as text
% str2double(get(hObject,'String')) returns contents of et_time as a double
if(isempty(get(hObject,'String')))
set(hObject,'String','0');
set(handles.set_time,'String','时间参数输入错误!');
pause(2);
set(handles.set_time,'String','请输入录音时长');
guidata(hObject,handles);
else
input=str2double(get(hObject,'String'));
handles.recor_time=input;
guidata(hObject,handles);
end
% --- Executes during object creation, after setting all properties.
function et_time_CreateFcn(hObject, ~, ~)
% hObject handle to et_time (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit 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 during object creation, after setting all properties.
function axes_t_m_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes_t_m (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 axes_t_m
% --- Executes on button press in rb_man.
function rb_man_Callback(hObject, eventdata, handles)
% hObject handle to rb_man (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of rb_man
% --- Executes on button press in rb_women.
function rb_women_Callback(hObject, eventdata, handles)
% hObject handle to rb_women (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of rb_women
% --- Executes on button press in rb_Neither.
function rb_Neither_Callback(hObject, eventdata, handles)
% hObject handle to rb_Neither (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of rb_Neither
% --------------------------------------------------------------------
% --- Executes when selected object is changed in uipanel5.
function uipanel5_SelectionChangeFcn(hObject, ~, ~)
% hObject handle to the selected object in uipanel5
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
global sex_flag;
switch get(hObject,'Tag')
case 'rb_man'
sex_flag=0;
case 'rb_women'
sex_flag=1;
% case 'rb_Neither'
% sex_flag=2;
end
% --------------------------------------------------------------------
function menu_intro_Callback(hObject, eventdata, handles)
% hObject handle to menu_intro (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
msg={'1. V1 实现基本功能'; ' 录音、播放、画时域图、基本频谱图等';'2. V2 改进算法,重新改变频域信号处理函数---20150522'...
;'3. V2.5加入start界面 尝试将讲解内容以图片形式导进---20150524 '...
;'4.V2.6加入采集频率可选下拉框---20150525'};
msgbox(msg ,'版本说明');
% --- Executes during object creation, after setting all properties.
function axes_fft_m_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes_fft_m (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 axes_fft_m
% --- Executes on button press in pushbutton_exit.
function pushbutton_exit_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
quest={'亲 你真的要离开人家吗?'};
response=questdlg(quest,'wennine ','Yeah','Oh No',2);
if(strcmp(response,'Yeah'))
close;
start_voice();
else
end
% if(response=='Yeah')
% close;
% else
% end
function et_savme_Callback(hObject, eventdata, handles)
% hObject handle to et_savme (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of et_savme as text
% str2double(get(hObject,'String')) returns contents of et_savme as a double
filename=get(hObject,'String');
handles.filename=filename;
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function et_savme_CreateFcn(hObject, eventdata, handles)
% hObject handle to et_savme (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit 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
function et_local_Callback(hObject, eventdata, handles)
% hObject handle to et_local (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of et_local as text
% str2double(get(hObject,'String')) returns contents of et_local as a double
% --- Executes during object creation, after setting all properties.
function et_local_CreateFcn(hObject, eventdata, handles)
% hObject handle to et_local (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit 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 pushbutton_local.
function pushbutton_local_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton_local (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global local_yn;
local_yn=1;
[FileName,PathName] = uigetfile('*.wav','亲 请选择您想要查看的音频文件');
handles.FileName=FileName;
guidata(hObject,handles);
set(handles.et_local,'String',FileName);
% data=audioread(FileName);
% axes(handles.axes_t_m);
% draw_fft_m=plot(data);
% ylabel('Amplitude');
% xlabel('Time ');
% title('时域图');
% %set(gca,'ylim',[0 2])
% % myRecording = getaudiodata(aa,'uint8');
% Fs=16000;
% %data=myRecording; %
% blockSize=length(data);
% xFFT = fft(data); %
% xfft = abs(xFFT); %
% % Avoid taking the log of 0.
% index = xfft == 0;
% xfft(index) = 1e-17;
% mag = 20*log10(xfft); %
% mag = mag(1:blockSize/2);
% f = (0:length(mag)-1)*Fs/blockSize;
% f = f(:);
% axes(handles.axes_fft_m);
% draw_fft_m=plot(f(:,1),mag(:,1));
% ylabel('Magnitude (dB)');
% xlabel('Frequency (Hz)');
% title('频谱图');
%
% --- Executes during object creation, after setting all properties.
function rb_man_CreateFcn(hObject, eventdata, handles)
% hObject handle to rb_man (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes during object creation, after setting all properties.
function uipanel5_CreateFcn(hObject, eventdata, handles)
% hObject handle to uipanel5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on selection change in ppm_caiyang.
function ppm_caiyang_Callback(hObject, eventdata, handles)
% hObject handle to ppm_caiyang (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns ppm_caiyang contents as cell array
% contents{get(hObject,'Value')} returns selected item from ppm_caiyang
canshu={'1000','2000','4000','8000','11025','22050','44100' };
val=get(handles.ppm_caiyang,'value');
switch val
case 1
fs=canshu{val};
case 2
fs=canshu{val};
case 3
fs=canshu{val};
case 4
fs=canshu{val};
case 5
fs=canshu{val};
case 6
fs=canshu{val};
case 7
fs=canshu{val};
end
handles.fs_chose=str2double(fs);
guidata(hObject,handles);
%keyboard;
% --- Executes during object creation, after setting all properties.
function ppm_caiyang_CreateFcn(hObject, eventdata, handles)
% hObject handle to ppm_caiyang (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
有兴趣者可到博客交流。
http://blog.sina.com.cn/s/blog_131ac03010102vna8.html
