源码在这里:记得给我点个星星⭐️https://gitee.com/poemwar/matlab
实现有限长序列的基本运算(包括:加法、乘法、累加、移位、翻褶、抽取、插值、卷
积和),并以 GUI 的形式将这些运算整合起来,使用者可通过向 GUI 输入任意有限长序列得
到对应的运算结果。
题目分析:
该题是对序列的各种数据操作,可根据“需要两个序列”还是“需要一个序列”可分为两类操作,第一类操作为“加法、乘法、卷积和”,需要接收两个序列,第二类操作为“累加、移位、翻褶、抽取、插值”。基本定义如下:
加法:z(n)=x(n)+y(n)
乘法:w(n)=x(n)y(n)
当y(n)为常数c时,称为标度运算:w(n)=cx(n)
累加:y(n)=
∑x(k)
移位:y(n)=x(n-m)
翻褶:y(n)=x(-n)
抽取:xd(n)=x(Dn),D为整数
插值:x’(n)= x(n/l),n=ml,l为整数,m=0,±1,±2……
程序截图:
通过下拉框可实现功能间的切换
设计思路:
第一类:通过两个可编辑文本框获取两个序列,运用axes()与stem()将A和B两个序列以针状图的形式画在轴上,接下来在按钮中写回调函数,按下后判断文本框中序列元素个数是否相等,不相等则弹出警告,相等则继续进行操作。
加法和乘法直接用A+B和A.*B即可得到,卷积和运用conv()即可操作完毕;
第二类:累加,运用for循环或者cumsum函数即可完成功能。移位,运用randn生成一个移位的随机数,通过if判断随机数的正负来决定左移还是右移,然后通过矩阵合并一个零矩阵来实现矩阵的移位。翻褶,通过以y轴为对称轴左右翻转得到,可运用fliplr函数来进行该操作。抽取,接收抽取的n值后,运用for循环找符合要求的矩阵索引来合成一个新矩阵,即完成抽取的功能。插值,接收插入的n值后,新建一个空矩阵,运用一个for循环不断地将零向量、所求矩阵索引与该矩阵合并,最终得到符合要求的新矩阵。
程序源码:
首先使用guide创建一个matlab的gui界面,按上图添加控件
接着编写 回调函数:
开始按钮1:
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)
eval("A="+(get(handles.edit1,'String')));
eval("B="+(get(handles.edit2,'String')));
x=0:length(A)-1;
axes(handles.axes1);
stem(x,A);
axes(handles.axes2);
stem(x,B);
V=get(handles.popupmenu1,'Value');
if numel(A)==numel(B)
switch V
case 1
C1=A+B;
axes(handles.axes3);
set(handles.text20,'String',num2str(C1));
stem(x,C1)
case 2
axes(handles.axes3);
C2=A.*B;
stem(C2)
set(handles.text20,'String',num2str(C2));
case 3
axes(handles.axes3);
C3=conv(A,B);
stem(C3);
set(handles.text20,'String',num2str(C3))
end
else
msgbox('二者元素个数不同','error','modal');
end
开始按钮2:
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)
A2=str2num(get(handles.edit3,'String'));
x=0:1:length(A2)-1;
axes(handles.axes4);
stem(x,A2);
V=get(handles.popupmenu2,'Value');
switch V
case 1
C1=cumsum(A2);
axes(handles.axes6);
stem(x,C1);
set(handles.text17,'String',num2str(C1))
case 2
axes(handles.axes6);
r=round(randn()*10)
if r>0
x=zeros(1,r);
C2=[x,A2];
else
r=-r;
x=zeros(1,r);
C2=[A2,x];
end
z=0:1:length(C2)-1;
stem(z,C2);
set(handles.text17,'String',num2str(A2))
case 3
axes(handles.axes6);
x=0:1:length(A2)-1;
x=-x;
stem(x,A2);
set(handles.text17,'String',num2str(fliplr(A2)))
case 4
ninput=inputdlg('请输入抽取的间隔数');
n=str2double(ninput);
cqu=[];
for i=1:n:length(A2)
cqu=[cqu,A2(i)];
end
axes(handles.axes6);
x=0:1:length(cqu)-1;
stem(x,cqu);
set(handles.text17,'String',num2str(cqu))
case 5
axes(handles.axes6)
ninput=inputdlg('每两点间插入多少个值')
n=str2double(ninput);
C5=A2(1);
for i=2:1:length(A2)
z=zeros(1,n);
C5=[C5,z,A2(i)]
end
x=0:1:length(C5)-1;
stem(x,C5);
set(handles.text17,'String',num2str(C5));
end
下拉型文本框:
双击打开该文本框的属性编辑器,在string属性中添加功能
同样的,在另一个下拉文本框中输入功能
程序即可运行