1.命令输入guide
2.建立GUI
3.添加控件
- Static Text 静态文本框
- Edit Text 编辑文本框
- Pushbutton 按钮
- Radiobutton 单选
- Axes 坐标轴
4.双击设置控件参数并保存
5.书写GUI回调函数(callback)代码
% --- 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)
close(gcf);
run('sigsample');
单选:
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (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 radiobutton1
set(handles.radiobutton1,'value',1);
set(handles.radiobutton2,'value',0);
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton2 (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 radiobutton2
set(handles.radiobutton2,'value',1);
set(handles.radiobutton1,'value',0);
打开音频文件:
% --- 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)
[filename, pathname] = uigetfile('*.wav', '*.*','请选择一个音频文件');
if isequal(filename,0) | isequal(pathname,0)
return;
end
newstr=[pathname filename];
set(handles.edit6,'String',newstr);
[x,fs,nbits]=wavread([pathname filename]);
set(handles.edit9,'String',num2str(fs));
axes(handles.axes1);
plot(x);
xlabel('时间');
ylabel('幅度');
N=length(x);
fx=fftshift(fft(x));
w=linspace(-fs/2,fs/2,N);
axes(handles.axes2);
plot(w,abs(fx));
xlabel('f');
ylabel('幅度');
handles.x=x;
handles.fs=fs;
% Choose default command line output for sigsample
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
set(handles.pushbutton7,'enable','on');
set(handles.pushbutton8,'enable','on');
频率转换:
% --- 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)
%频率转换
x=handles.x;
fs=handles.fs;
fsn=str2double(get(handles.edit7,'string'));
t1=(fs/fsn:fs/fsn:length(x))/fs;
c=resample(x,fsn,fs) ;
wavwrite(c,fsn,'after.wav')
axes(handles.axes1);
plot(t1,c);
xlabel('时间');
ylabel('幅度');
N=length(c);
fx=fftshift(fft(c));
w=linspace(-fsn/2,fsn/2,N);
axes(handles.axes2);
plot(w,abs(fx));
xlabel('f');
ylabel('幅度');
播放:
% --- 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)
x=handles.x;
fs=handles.fs;
sound(x,fs);
录音:
% --- 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)
%录音
fs=str2double(get(handles.edit17,'string')); %取样频率
duration=str2double(get(handles.edit18,'string')); %录音时间
% fprintf('Press any key to start %g seconds of recording...\n',duration);
% pause;
fprintf('Recording...\n');
x=wavrecord(duration*fs,fs); %duration*fs 是总的采样点数
msgbox('录制已经完成','录音','edit19');
wavwrite(x,fs,'recording.wav');%写入音频文件.wav
返回菜单界面:
% --- 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)
close(gcf);
menu;