题目:
用GUI方式编程实现一个频率在100-500Hz,幅值在0-2000,相位0-180度可变的正弦波信号,信号采样频率为5120Hz.
1.创建GUI项目
2.布局Panel
3.布局Static Text
同理,做出另外两个Text
4.布局slider和EditText
设置幅值Slider的Max值为2000,当前值为0;初始相位Slider的Min值为0,Max值为180,当前值为0;频率Slider的Min值为100,Max值为500,当前值为100。
下面是当前值的设置
设置EidtText的值
分别设置三个EditText的显示值为0,0,100
5.布局显示波形控件
6.设置Slider1的CallBack
slider1的CallBack
function slider1_Callback(hObject, eventdata, handles)
fuzhi=get(handles.slider1,'Value'); % 获取slider1代表的幅值
caiyangpinglv=5120;% 采样频率设置为5120
dt=1.0/caiyangpinglv;
T=1;
N=T/dt;
t=[0:N-1]/N;
chushixiangwei=get(handles.slider2,'Value');% 获取slider2代表的初始相位值
pinlv=get(handles.slider3,'Value');% 获取slider3代表的频率值
x=fuzhi * sin(2*pi* pinlv *t + chushixiangwei);%计算波形
plot(t, x, 'r','LineWidth', 3);%画波形
axis([0,0.1,-2000,2000]);%axis( [xmin xmax ymin ymax] ) 设置当前坐标轴 x轴 和 y轴的限制范围
set(gca,'color',[0, 0.95, 0.95]); %利用set(gca,'propertyname','propertyvalue'......)命令可以调整图形的坐标属性
s1=sprintf('%f',fuzhi);
set(handles.edit1,'String',s1);
wavplay(x,caiyangpinglv);
7.设置slider2的CallBack:
function slider2_Callback(hObject, eventdata, handles)
fuzhi=get(handles.slider1,'Value');
caiyangpinglv=5120;
dt=1.0/caiyangpinglv;
T=1;
N=T/dt;
t=[0:N-1]/N;
chushixiangwei=get(handles.slider2,'Value');
pinlv=get(handles.slider3,'Value');
x=fuzhi * sin(2*pi* pinlv *t + chushixiangwei);
plot(t, x, 'r','LineWidth', 3);
axis([0,0.1,-2000,2000]);%axis( [xmin xmax ymin ymax] ) 设置当前坐标轴 x轴 和 y轴的限制范围
set(gca,'color',[0, 0.95, 0.95]); %利用set(gca,'propertyname','propertyvalue'......)命令可以调整图形的坐标属性
s1=sprintf('%f',chushixiangwei);
set(handles.edit2,'String',s1);
wavplay(x,caiyangpinglv);
8.设置slider3的CallBack:
function slider3_Callback(hObject, eventdata, handles)
fuzhi=get(handles.slider1,'Value');
caiyangpinglv=5120;
dt=1.0/caiyangpinglv;
T=1;
N=T/dt;
t=[0:N-1]/N;
chushixiangwei=get(handles.slider2,'Value');
pinlv=get(handles.slider3,'Value');
x=fuzhi * sin(2*pi* pinlv *t + chushixiangwei);
plot(t, x, 'r','LineWidth', 3);
axis([0,0.1,-2000,2000]);%axis( [xmin xmax ymin ymax] ) 设置当前坐标轴 x轴 和 y轴的限制范围
set(gca,'color',[0, 0.95, 0.95]); %利用set(gca,'propertyname','propertyvalue'......)命令可以调整图形的坐标属性
s1=sprintf('%f',pinlv);
set(handles.edit2,'String',s1);
wavplay(x,caiyangpinglv);
运行结果: