推荐先看理论知识:matlab 生成正弦波
1.布局页面的控件
2.添加Button的点击回调函数
Fs=44100;
dt=1.0/Fs;
T=1;
N=T/dt;
t=linspace(0,T,N);
x=0.3*sin(2*pi*600*t);
plot(t,x,'r','LineWidth',3);
axis([0,0.01,-0.5,0.5]);%axis( [xmin xmax ymin ymax] ) 设置当前坐标轴 x轴 和 y轴的限制范围
set(gca,'color',[0, 0.95, 0.95]); %利用set(gca,'propertyname','propertyvalue'......)命令可以调整图形的坐标属性
wavplay(x,Fs);
运行结果:先播放一段声音,然后显示出波形
3.改进
3.1添加EditText
3.2修改Button的点击回调函数
F=str2double(get(handles.edit1, 'String'));
Fs=44100;
dt=1.0/Fs;
T=1;
N=T/dt;
%t=linspace(0,T,N);
t=[0:N-1]/N;
%x=0.3*sin(2*pi*600*t);
x=0.3*sin(2*pi* F *t);
plot(t,x,'r','LineWidth',3);
axis([0,0.01,-0.5,0.5]);%axis( [xmin xmax ymin ymax] ) 设置当前坐标轴 x轴 和 y轴的限制范围
set(gca,'color',[0, 0.95, 0.95]); %利用set(gca,'propertyname','propertyvalue'......)命令可以调整图形的坐标属性
wavplay(x,Fs);
点击运行,这样就能动态生成不一样的波形
4.再次改进
4.1添加Slider控件
将鼠标放到slider的右下角后变成这种样子
然后点击左键往右拖
这样控件就变成横向的了
修改滚动条的最小值和最大值
4.2 添加滚动条的回调函数
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
F=get(handles.slider1,'Value');
Fs=44100;
dt=1.0/Fs;
T=1;
N=T/dt;
%t=linspace(0,T,N);
t=[0:N-1]/N;
%x=0.3*sin(2*pi*600*t);
x=0.3*sin(2*pi* F *t);
plot(t,x,'r','LineWidth',3);
axis([0,0.01,-0.5,0.5]);%axis( [xmin xmax ymin ymax] ) 设置当前坐标轴 x轴 和 y轴的限制范围
set(gca,'color',[0, 0.95, 0.95]); %利用set(gca,'propertyname','propertyvalue'......)命令可以调整图形的坐标属性
s1=sprintf('%f',F);
set(handles.edit1,'String',s1);
wavplay(x,Fs);
运行,拖动滚动条就会先发出声音,然后更新EditText和生成波形