一.代码运行结果
二.代码
function varargout = tianqi(varargin)
% TIANQI MATLAB code for tianqi.fig
% TIANQI, by itself, creates a new TIANQI or raises the existing
% singleton*.
%
% H = TIANQI returns the handle to a new TIANQI or the handle to
% the existing singleton*.
%
% TIANQI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TIANQI.M with the given input arguments.
%
% TIANQI('Property','Value',...) creates a new TIANQI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before tianqi_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to tianqi_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 tianqi% Last Modified by GUIDE v2.5 18-Oct-2020 21:54:40% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @tianqi_OpeningFcn, ...'gui_OutputFcn', @tianqi_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);
if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});
endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT% --- Executes just before tianqi is made visible.
function tianqi_OpeningFcn(hObject, eventdata, 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 tianqi (see VARARGIN)% Choose default command line output for tianqi
handles.output = hObject;% Update handles structure
guidata(hObject, handles);% UIWAIT makes tianqi wait for user response (see UIRESUME)
% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.
function varargout = tianqi_OutputFcn(hObject, eventdata, 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 pushbutton1.
% 绘制x图形
function pushbutton1_Callback(hObject, eventdata, handles)
res1=get(handles.edit1,'String'); %获取参数a(string)
res2=get(handles.edit2,'String'); %获取参数b(string)
if isempty(res1)msgbox('input a first','warn','warn');return
elsea=str2double(res1); %判断a是否为空值
end
if isempty(res2)msgbox('input b first','warn','warn');return
elseb=str2double(res2); %判断b是否为空值
end
choose_x=get(handles.popupmenu1,'value');
%获取函数x类型
switch choose_xcase 1 %门函数if b<=0msgbox('b should be positive','warn','warn');returnend
t_start=a-b/2;
t_end=a+b/2; %门函数起点与终点
x_start=t_start-3; %限定x坐标轴范围
x_end=t_end+3;
t=x_start:0.001:x_end;%定义时间向量
f=@(t) (t.*0+1).*(t>=t_start&t<=t_end);%使用函数句柄定义门函数,门高为1
yt=f(t); %创建门函数
y_end=max(yt)*1.1;%限定y坐标轴范围
axes(handles.axes1); %画图像在坐标轴1
plot(t,yt); %绘图
axis([x_start,x_end,0,y_end]); %限定坐标轴范围
grid on; %打开网格线case 2 %三角函数if b<=0msgbox('b should be positive','warn','warn');returnendt_start=a-b/2;
t_end=a+b/2; %三角函数起点与终点
x_start=t_start-3; %限定x坐标轴范围
x_end=t_end+3;
t=x_start:0.001:x_end;%定义时间向量
t_center=(t_start+t_end)/2;
t_width=t_end-t_start; %三角函数中心与宽度
a10=t_center;
b10=t_width;
f=@(t) ((2/b10).*t-(2*a10/b10)+1).*(t>=t_start&t<=t_center)+((-2/b10).*t+(2*a10/b10)+1).*(t>t_center&t<=t_end);
%使用函数句柄定义三角函数
yt=f(t); %创建三角函数
y_end=max(yt)*1.1;%限定y坐标轴范围
axes(handles.axes1); %画图像在坐标轴1
plot(t,yt);
axis([x_start,x_end,0,y_end]);
grid on;case 3 %阶跃函数if a==0msgbox('a can not be zero','warn','warn');returnendb10=b;
a10=a;
x_start=b10/a10-3; %限定x坐标轴范围
x_end=b10/a10+3;
t=x_start:0.001:x_end;%定义时间向量
f=@(t) (t.*0+1).*((a10.*t-b10)>=0); %使用函数句柄定义阶跃函数
yt=f(t);
y_end=max(yt)*1.1;%限定y坐标轴范围
axes(handles.axes1);
plot(t,yt);
axis([x_start,x_end,0,y_end]);case 4 %冲激函数if a==0msgbox('a can not be zero','warn','warn');returnendsyms f t; b10=b;
a10=a;
f=dirac(a10*t-b10); %定义冲激函数
x_start=b10/a10-3; %限定x坐标轴范围
x_end=b10/a10+3;
tv=x_start:0.001:x_end;%定义时间向量
ft=double(subs(f,t,tv)); %用时间向量tv替代t
ft(find(ft==inf))=1; %将冲激函数中无穷大量改为1
axes(handles.axes1);
plot(tv,ft);
axis([x_start x_end 0 1 ]);case 5 %单边指数if a==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a;
b10=b;
x_start=-3; %限定x坐标轴范围
x_end=3;
t=x_start:0.001:x_end;%定义时间向量
f=@(t) (a10*exp(-abs(b10).*t)).*(t>=0); %使用函数句柄定义单边指数信号
yt=f(t);
y_start=min(yt)*1.1;
y_end=max(yt)*1.1;%限定y坐标轴范围
axes(handles.axes1);
plot(t,yt);
axis([x_start,x_end,y_start,y_end]);
grid on;case 6 %单边正弦波if a==0msgbox('a can not be zero,due to some reasons that exist on sources codes','warn','warn');returnenda10=a;
b10=b;
x_start=-pi/abs(a10) ; %限定x坐标轴范围
x_end=3*pi/abs(a) ;
t=x_start:0.001:x_end;%定义时间向量
f=@(t) sin(a10.*t+b10).*(t>=0);
yt=f(t);
y_end=max(yt)*1.1;%限定y坐标轴范围
y_start=min(yt)*1.1;
axes(handles.axes1);
plot(t,yt);
axis([x_start,x_end,y_start,y_end]);
grid on;
end
% 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)% --- Executes on button press in pushbutton2.
%绘制y图象
function pushbutton2_Callback(hObject, eventdata, handles)
res1=get(handles.edit3,'String');
res2=get(handles.edit4,'String');
if isempty(res1)msgbox('input a first','warn','warn');return
elsea=str2double(res1);
end
if isempty(res2)msgbox('input b first','warn','warn');return
elseb=str2double(res2);
end
choose_y=get(handles.popupmenu2,'value');
switch choose_ycase 1 %门函数if b<=0msgbox('b should be positive','warn','warn');returnend
t_start=a-b/2;
t_end=a+b/2;
x_start=t_start-3; %限定x坐标轴范围
x_end=t_end+3;
t=x_start:0.001:x_end;%定义时间向量
f=@(t) (t.*0+1).*(t>=t_start&t<=t_end);%使用函数句柄定义门函数,门高为1
yt=f(t); %创建门函数
y_end=max(yt)*1.1;%限定y坐标轴范围
axes(handles.axes2);
plot(t,yt,'r');
axis([x_start,x_end,0,y_end]);
grid on;case 2 %三角函数if b<=0msgbox('b should be positive','warn','warn');returnendt_start=a-b/2;
t_end=a+b/2;
x_start=t_start-3; %限定x坐标轴范围
x_end=t_end+3;
t=x_start:0.001:x_end;%定义时间向量
t_center=(t_start+t_end)/2;
t_width=t_end-t_start;
a10=t_center;
b10=t_width;
f=@(t) ((2/b10).*t-(2*a10/b10)+1).*(t>=t_start&t<=t_center)+((-2/b10).*t+(2*a10/b10)+1).*(t>t_center&t<=t_end);
%使用函数句柄定义三角函数
yt=f(t); %创建三角函数
y_end=max(yt)*1.1;%限定y坐标轴范围
axes(handles.axes2);
plot(t,yt,'r');
axis([x_start,x_end,0,y_end]);
grid on;case 3 %阶跃函数if a==0msgbox('a can not be zero','warn','warn');returnendb10=b;
a10=a;
x_start=b10/a10-3; %限定x坐标轴范围
x_end=b10/a10+3;
t=x_start:0.001:x_end;%定义时间向量
f=@(t) (t.*0+1).*((a10.*t-b10)>=0);
yt=f(t);
y_end=max(yt)*1.1;%限定y坐标轴范围
axes(handles.axes2);
plot(t,yt,'r');
axis([x_start,x_end,0,y_end]);case 4 %冲激函数if a==0msgbox('a can not be zero','warn','warn');returnendsyms f t;b10=b;
a10=a;
f=dirac(a10*t-b10); %定义冲激函数
x_start=b10/a10-3; %限定x坐标轴范围
x_end=b10/a10+3;
tv=x_start:0.001:x_end;%定义时间向量
ft=double(subs(f,t,tv)); %用时间向量tv替代t
ft(find(ft==inf))=1;
axes(handles.axes2);
plot(tv,ft,'r');
axis([x_start x_end 0 1 ]);
grid on;case 5 %单边指数if a==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a;
b10=b;
x_start=-3; %限定x坐标轴范围
x_end=3;
t=x_start:0.001:x_end;%定义时间向量
f=@(t) (a10*exp(-abs(b10).*t)).*(t>=0);
yt=f(t);
y_start=min(yt)*1.1;
y_end=max(yt)*1.1;%限定y坐标轴范围
axes(handles.axes2);
plot(t,yt,'r');
axis([x_start,x_end,y_start,y_end]);
grid on;case 6 %单边正弦波if a==0msgbox('a can not be zero,due to some reasons that exist on sources codes','warn','warn');returnenda10=a;
b10=b;
x_start=-pi/abs(a10) ; %限定x坐标轴范围
x_end=3*pi/abs(a) ;
t=x_start:0.001:x_end;%定义时间向量
f=@(t) sin(a10.*t+b10).*(t>=0);
yt=f(t);
y_end=max(yt)*1.1;%限定y坐标轴范围
y_start=min(yt)*1.1;
axes(handles.axes2);
plot(t,yt,'r');
axis([x_start,x_end,y_start,y_end]);
grid on;end
% 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)% --- Executes on button press in pushbutton3.
%卷积运算
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global flag;
flag=1;
choose1=get(handles.popupmenu1,'value');
choose2=get(handles.popupmenu2,'value');
res1=get(handles.edit1,'String');
res2=get(handles.edit2,'String');
res3=get(handles.edit3,'String');
res4=get(handles.edit4,'String');
if isempty(res1)msgbox('input a first','warn','warn');return
elsea1=str2double(res1);
end
if isempty(res2)msgbox('input b first','warn','warn');return
elseb1=str2double(res2);
end
if isempty(res3)msgbox('input b first','warn','warn');return
elsea2=str2double(res3);
end
if isempty(res4)msgbox('input b first','warn','warn');return
elseb2=str2double(res4);
end
switch choose2 %函数y图形(g表示)case 1 %门函数if b2<=0msgbox('b should be positive','warn','warn');returnendsyms x g2;g2=heaviside(x-(a2-b2/2))-heaviside(x-(a2+b2/2));%利用阶跃函数构建门函数
t2_start=a2-b2/2; %门函数左边界
t2_end=a2+b2/2; %门函数右边界
g=@(t) (t.*0+1).*(t>=t2_start&t<=t2_end)+0;%使用函数句柄定义门函数,门高为1
switch choose1 %函数x图形(f表示)case 1 %门函数if b1<=0msgbox('b should be positive','warn','warn');returnend
t1_start=a1-b1/2; %门函数左边界
t1_end=a1+b1/2; %门函数右边界
x_start=t1_start+t2_start-3; %坐标轴3左边界
x_end=t1_end+t2_end+3; %坐标轴3右边界
y_start =-0.5;
if b1>b2y_end=b2*1.1;
elsey_end=b1*1.1;
end
f=@(t) (t.*0+1).*(t>=t1_start&t<=t1_end)+0;%使用函数句柄定义门函数,门高为1case 2 %三角函数if b1<=0msgbox('b should be positive','warn','warn');returnendt1_start=a1-b1/2; %三角函数左边界
t1_end=a1+b1/2; %三角函数右边界
x_start=t1_start+t2_start-3; %坐标轴3左边界
x_end=t1_end+t2_end+3; %坐标轴3右边界
y_start =-0.5;
if (b1/2)>b2y_end=b2*1.1;
elsey_end=(b1/2)*1.1;
end
t1_center=a1; %三角函数中心
t1_width=b1; %三角函数宽度
a=t1_center;
b=t1_width;
f=@(t) ((2/b).*t-(2*a/b)+1).*(t>=t1_start&t<=t1_center)+((-2/b).*t+(2*a/b)+1).*(t>t1_center&t<=t1_end);case 3 %阶跃函数if a1==0msgbox('a can not be zero','warn','warn');returnend
x1_start=b1/a1-3;
x1_end=b1/a1+3; %阶跃函数左右边界
a=a1;
b=b1;
f=@(t) (t.*0+1).*((a.*t-b)>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-0.5;
y_end=b2*1.1;case 4 %冲激函数if a1==0msgbox('a can not be zero','warn','warn');returnendsyms f;syms tao;x1_start=b1/a1-3;
x1_end=b1/a1+3; %冲激函数左右边界
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-0.5;
if abs(a1)>1y_end=1.1;
else
y_end=round(1/abs(a1),1)*1.1;
endx_of_f=x_start:0.01:x_end;%定义时间向量f=dirac(a1*tao-b1); %定义冲激函数y_of_f=double(subs(f,tao,x_of_f));% 将tao替换为x_of_fy_of_f(find( y_of_f==inf))=1;%将冲激函数中的无穷大改为1xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程case 5 %单边指数信号if a1==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a1;
b10=b1;
x1_start=-3;
x1_end=3;
f=@(t) (a10*exp(-abs(b10).*t)).*(t>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=min((a1<0)*a1*b2,a1*b2*(a1>0));
y_end=max((a1<0)*a1*b2,a1*b2*(a1>0));case 6 %单边正弦波if a2==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a1;
b10=b1;
x1_start=-pi/abs(a10) ;
x1_end=3*pi/abs(a10) ;
f=@(t) sin(a10.*t+b10).*(t>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-3;
y_end=3;
end
case 2 %函数y图形三角函数syms x g2;t2_start=a2-b2/2;
t2_end=a2+b2/2;
t2_center=a2;
t2_width=b2;
a=t2_center;
b=t2_width;
g=@(t) ((2/b).*t-(2*a/b)+1).*(t>=t2_start&t<=t2_center)+((-2/b).*t+(2*a/b)+1).*(t>t2_center&t<=t2_end);g2=(heaviside(x-(a2-b2/2))-heaviside(x-a2))*(x-(a2-b2/2))*2/b2+(heaviside(x-a2)-heaviside(x-(a2+b2/2)))*(-x+(a2+b2/2))*2/b2;
switch choose1 %函数x图形(f表示)case 1 %门函数if b1<=0msgbox('b should be positive','warn','warn');returnend
t1_start=a1-b1/2;
t1_end=a1+b1/2;
x_start=t1_start+t2_start-3;
x_end=t1_end+t2_end+3;
f=@(t) (t.*0+1).*(t>=t1_start&t<=t1_end)+0;%使用函数句柄定义门函数,门高为1
y_start=-0.5;
if (b2/2)>b1y_end=b1*1.1;
elsey_end=(b2/2)*1.1;
end
case 2 %三角函数if b1<=0msgbox('b should be positive','warn','warn');returnendt1_start=a1-b1/2;
t1_end=a1+b1/2;
x_start=t1_start+t2_start-3;
x_end=t1_end+t2_end+3;
y_start=-0.5;
if (b2/2)>(b1/2)y_end=(b1/2)*1.1;
elsey_end=(b2/2)*1.1;
end
t1_center=a1;
t1_width=b1;
a=t1_center;
b=t1_width;
f=@(t) ((2/b).*t-(2*a/b)+1).*(t>=t1_start&t<=t1_center)+((-2/b).*t+(2*a/b)+1).*(t>t1_center&t<=t1_end);case 3 %阶跃函数if a1==0msgbox('a can not be zero','warn','warn');returnend
x1_start=b1/a1-3;
x1_end=b1/a1+3; %阶跃函数左右边界
a=a1;
b=b1;
f=@(t) (t.*0+1).*((a.*t-b)>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-0.5;
y_end=(b2/2)*1.1;
case 4 %冲激函数if a1==0msgbox('a can not be zero','warn','warn');returnendsyms f;syms tao;f=dirac(a1*tao-b1); %定义冲激函数x1_start=b1/a1-3;
x1_end=b1/a1+3; %冲激函数左右边界
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-0.5;
if abs(a1)>1y_end=1.1;
else
y_end=round(1/abs(a1),1)*1.1;
endx_of_f=x_start:0.01:x_end;%定义时间向量y_of_f=double(subs(f,tao,x_of_f));% 将tao替换为x_of_fy_of_f(find( y_of_f==inf))=1;%将冲激函数中的无穷大改为1xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程case 5 %单边指数信号if a1==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a1;
b10=b1;
x1_start=-3;
x1_end=3;
f=@(t) (a10*exp(-abs(b10).*t)).*(t>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=min((a1<0)*a1*(b2/2),a1*(b2/2)*(a1>0));
y_end=max((a1<0)*a1*(b2/2),a1*(b2/2)*(a1>0));case 6 %单边正弦波if a2==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a1;
b10=b1;
x1_start=-pi/abs(a10) ;
x1_end=3*pi/abs(a10) ;
f=@(t) sin(a10.*t+b10).*(t>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-3;
y_end=3;
end
case 3 %函数y图形阶跃函数syms x g2;x2_start=b2/a2-3;
x2_end=b2/a2+3;
a=a2;
b=b2;
g=@(t) (t.*0+1).*((a.*t-b)>=0);
g2=heaviside(a2*x-b2);
switch choose1 %函数x图形(f表示)case 1 %门函数if b1<=0msgbox('b should be positive','warn','warn');returnend
t1_start=a1-b1/2;
t1_end=a1+b1/2;
x_start=t1_start+x2_start-3;
x_end=t1_end+x2_end+3;
y_start=-0.5;
y_end=b1*1.1;
f=@(t) (t.*0+1).*(t>=t1_start&t<=t1_end)+0;%使用函数句柄定义门函数,门高为1case 2 %三角函数if b1<=0msgbox('b should be positive','warn','warn');returnendt1_start=a1-b1/2;
t1_end=a1+b1/2;
x_start=t1_start+x2_start-3;
x_end=t1_end+x2_end+3;
y_start=-0.5;
y_end=(b1/2)*1.1;
t1_center=a1;
t1_width=b1;
a=t1_center;
b=t1_width;
f=@(t) ((2/b).*t-(2*a/b)+1).*(t>=t1_start&t<=t1_center)+((-2/b).*t+(2*a/b)+1).*(t>t1_center&t<=t1_end);case 3 %阶跃函数if a1==0msgbox('a can not be zero','warn','warn');returnend
x1_start=b1/a1-3;
x1_end=b1/a1+3; %阶跃函数左右边界
a=a1;
b=b1;
f=@(t) (t.*0+1).*((a.*t-b)>=0);
x_start=x1_start+x2_start-3;
x_end=x1_end+x2_end+3;
y_start=-0.5;
y_end=10;case 4 %冲激函数if a1==0msgbox('a can not be zero','warn','warn');returnendsyms f;syms tao;f=dirac(a1*tao-b1); %定义冲激函数x1_start=b1/a1-3;
x1_end=b1/a1+3; %冲激函数左右边界
x_start=x1_start+x2_start-3;
x_end=x1_end+x2_end+3;
y_start=-0.5;
if abs(a1)>1y_end=1.1;
else
y_end=round(1/abs(a1),1)*1.1;
end
x_of_f=x_start:0.01:x_end;%定义时间向量y_of_f=double(subs(f,tao,x_of_f));% 将tao替换为x_of_fy_of_f(find( y_of_f==inf))=1;%将冲激函数中的无穷大改为1xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程case 5 %单边指数信号if a1==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a1;
b10=b1;
x1_start=-3;
x1_end=3;
f=@(t) (a10*exp(-abs(b10).*t)).*(t>=0);
x_start=x1_start+x2_start-3;
x_end=x1_end+x2_end+3;
y_start=min((a1<0)*a1*2,a1*2*(a1>0));
y_end=max((a1<0)*a1*2,a1*2*(a1>0));case 6 %单边正弦波if a2==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a1;
b10=b1;
x1_start=-pi/abs(a10) ;
x1_end=3*pi/abs(a10) ;
f=@(t) sin(a10.*t+b10).*(t>=0);
x_start=x1_start+x2_start-3;
x_end=x1_end+x2_end+3;
y_start=-3;
y_end=3;
endcase 4 %函数y图形冲激函数syms t f tao g2 x;t2_start=b2/a2-3;t2_end=b2/a2+3;g2=dirac(a2*x-b2);switch choose1 %函数x图形(f表示)case 1 if b1<=0msgbox('b should be positive','warn','warn');returnend
t1_start=a1-b1/2;
t1_end=a1+b1/2;
x_start=t1_start+t2_start;
x_end=t1_end+t2_end;
y_start=-0.5;
if abs(a2)>1y_end=1.1;
else
y_end=round(1/abs(a2),1)*1.1;
endf=heaviside(tao-(a1-b1/2))-heaviside(tao-(a1+b1/2));%利用阶跃函数构建门函数xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程case 2if b1<=0msgbox('b should be positive','warn','warn');returnendt1_start=a1-b1/2;
t1_end=a1+b1/2;
x_start=t1_start+t2_start;
x_end=t1_end+t2_end;
y_start=-0.5;
if abs(a2)>1y_end=1.1;
else
y_end=round(1/abs(a2),1)*1.1;
endf=(heaviside(tao-(a1-b1/2))-heaviside(tao-a1))*(tao-(a1-b1/2))*2/b1+(heaviside(tao-a1)-heaviside(tao-(a1+b1/2)))*(-tao+(a1+b1/2))*2/b1;%利用阶跃函数构建三角函数xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程case 3if a1==0msgbox('a can not be zero','warn','warn');returnend
x1_start=b1/a1-3;
x1_end=b1/a1-3; %阶跃函数左右边界
if abs(a2)>1y_end=1.1;
else
y_end=round(1/abs(a2),1)*1.1;end
y_start=-0.5;f=heaviside(a1*tao-b1);%阶跃函数表达式x_start=x1_start+t2_start;
x_end=x1_end+t2_end;
xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程case 4if a1==0msgbox('a can not be zero','warn','warn');returnendx1_start=b1/a1-3;
x1_end=b1/a1+3; %冲激函数左右边界
x_start=x1_start+t2_start;
x_end=x1_end+t2_end;
if abs(a2)>1y_end=1.1;
else
y_end=round(1/abs(a2),1)*1.1;
end
y_start=-0.5;f=dirac(a1*tao-b1);%冲激函数表达式xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程case 5 %单边指数信号if a1==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnendx1_start=-3;
x1_end=3;
x_start=x1_start+t2_start;
x_end=x1_end+t2_end;
if abs(a2)>1y_start=-abs(a1);y_end=abs(a1);
elsey_start=-abs(a1)/abs(a2);y_end=abs(a1)/abs(a2);
endf= a1*exp(-abs(b1)*tao)*heaviside(tao);xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程case 6 %单边正弦波if a2==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnendx1_start=round(-pi/abs(a1) ,2) ;
x1_end=round(3*pi/abs(a1),2) ;
x_start=x1_start+t2_start;
x_end=x1_end+t2_end;
if abs(a2)>1y_start=-3;y_end=3;
elsey_start=-(1/abs(a2))*1.1;y_end=(1/abs(a2))*1.1;
endf=sin(a1*tao+b1)*heaviside(tao);xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程endcase 5 %函数y图形单边指数函数t2_start=-3; %限定x坐标轴范围
t2_end=3;
a=a2;
b=b2;
g=@(t) (a*exp(-abs(b).*t)).*(t>=0);
syms g2 x;g2=a2*exp(-abs(b2)*x)*heaviside(x);switch choose1 %函数x图形(f表示)case 1 %门函数if b1<=0msgbox('b should be positive','warn','warn');returnend
t1_start=a1-b1/2;
t1_end=a1+b1/2;
x_start=t1_start+t2_start-3;
x_end=t1_end+t2_end+3;
y_start=min((a2<0)*a2*b1,a2*b1*(a2>0));
y_end=max((a2<0)*a2*b1,a2*b1*(a2>0));
f=@(t) (t.*0+1).*(t>=t1_start&t<=t1_end)+0;%使用函数句柄定义门函数,门高为1case 2 %三角函数if b1<=0msgbox('b should be positive','warn','warn');returnendt1_start=a1-b1/2;
t1_end=a1+b1/2;
x_start=t1_start+t2_start-3;
x_end=t1_end+t2_end+3;
y_start=min((a2<0)*a2*(b1/2),a2*(b1/2)*(a2>0));
y_end=max((a2<0)*a2*(b1/2),a2*(b1/2)*(a2>0));
t1_center=a1;
t1_width=b1;
a=t1_center;
b=t1_width;
f=@(t) ((2/b).*t-(2*a/b)+1).*(t>=t1_start&t<=t1_center)+((-2/b).*t+(2*a/b)+1).*(t>t1_center&t<=t1_end);case 3 %阶跃函数if a1==0msgbox('a can not be zero','warn','warn');returnend
x1_start=b1/a1-3;
x1_end=b1/a1+3; %阶跃函数左右边界
a=a1;
b=b1;
f=@(t) (t.*0+1).*((a.*t-b)>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-abs(a2);
y_end=abs(a2);case 4 %冲激函数if a1==0msgbox('a can not be zero','warn','warn');returnendsyms f;syms tao;f=dirac(a1*tao-b1); %定义冲激函数x1_start=b1/a1-3;
x1_end=b1/a1+3; %冲激函数左右边界
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
if abs(a1)>1y_start=-abs(a2);y_end=abs(a2);
elsey_start=-abs(a2)/abs(a1);y_end=abs(a2)/abs(a1);
end
x_of_f=x_start:0.01:x_end;%定义时间向量y_of_f=double(subs(f,tao,x_of_f));% 将tao替换为x_of_fy_of_f(find( y_of_f==inf))=1;%将冲激函数中的无穷大改为1xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程case 5 %单边指数信号if a1==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a1;
b10=b1;
x1_start=-3;
x1_end=3;
f=@(t) (a10*exp(-abs(b10).*t)).*(t>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
top=abs(a1)+abs(a2);
y_start=-top;
y_end=top;case 6 %单边正弦波if a2==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a1;
b10=b1;
x1_start=-pi/abs(a10) ;
x1_end=3*pi/abs(a10) ;
f=@(t) sin(a10.*t+b10).*(t>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-3;
y_end=3;
end
case 6 %函数y图形单边正弦波syms g2 x;g2=sin(a2*x+b2)*heaviside(x);
t2_start=round(-pi/abs(a2),2) ;
t2_end=round(3*pi/abs(a2),2) ;
g=@(t) sin(a2.*t+b2).*(t>=0);switch choose1 %函数x图形(f表示)case 1 %门函数if b1<=0msgbox('b should be positive','warn','warn');returnend
t1_start=a1-b1/2;
t1_end=a1+b1/2;
x_start=t1_start+t2_start-3;
x_end=t1_end+t2_end+3;
y_start=-3;
y_end=3;
f=@(t) (t.*0+1).*(t>=t1_start&t<=t1_end)+0;%使用函数句柄定义门函数,门高为1case 2 %三角函数if b1<=0msgbox('b should be positive','warn','warn');returnendt1_start=a1-b1/2;
t1_end=a1+b1/2;
x_start=t1_start+t2_start-3;
x_end=t1_end+t2_end+3;
y_start=-3;
y_end=3;
t1_center=a1;
t1_width=b1;
a=t1_center;
b=t1_width;
f=@(t) ((2/b).*t-(2*a/b)+1).*(t>=t1_start&t<=t1_center)+((-2/b).*t+(2*a/b)+1).*(t>t1_center&t<=t1_end);case 3 %阶跃函数if a1==0msgbox('a can not be zero','warn','warn');returnend
x1_start=b1/a1-3;
x1_end=b1/a1+3; %阶跃函数左右边界
a=a1;
b=b1;
f=@(t) (t.*0+1).*((a.*t-b)>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-3;
y_end=3;case 4 %冲激函数if a1==0msgbox('a can not be zero','warn','warn');returnendsyms f;syms tao;f=dirac(a1*tao-b1); %定义冲激函数x1_start=b1/a1-3;
x1_end=b1/a1+3; %冲激函数左右边界
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-3;
y_end=3;
x_of_f=x_start:0.01:x_end;%定义时间向量y_of_f=double(subs(f,tao,x_of_f));% 将tao替换为x_of_fy_of_f(find( y_of_f==inf))=1;%将冲激函数中的无穷大改为1xy_tao=f*subs(g2,x,x-tao);%将y中的x用x-tao替换xy=simplify(simplify(int(xy_tao,tao,-100,100)));%卷积过程case 5 %单边指数信号if a1==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a1;
b10=b1;
x1_start=-3;
x1_end=3;
f=@(t) (a10*exp(-abs(b10).*t)).*(t>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-3;
y_end=3;case 6 %单边正弦波if a2==0msgbox('a can not be zero,due to some reasons that exsit on sources codes','warn','warn');returnenda10=a1;
b10=b1;
x1_start=-pi/abs(a10) ;
x1_end=3*pi/abs(a10) ;
f=@(t) sin(a10.*t+b10).*(t>=0);
x_start=x1_start+t2_start-3;
x_end=x1_end+t2_end+3;
y_start=-3;
y_end=3;
end
end
if choose2 ==4syms g g3 tao x;x_of_g=x_start:0.01:x_end;axes(handles.axes3);cla reset;grid on;hold on;%坐标轴范围axis([x_start x_end y_start y_end]);fplot(f,[x_start x_end ]);H=round(b2/a2,2);g=dirac(x-tao-H); %定义冲激函数g3=subs(g,tao,x_of_g);for t=x_start:0.01:x_end
y_of_g=subs(g3,x,t);y_of_g(find(y_of_g==inf))=1;y_of_g_plot=plot(x_of_g,y_of_g,'r');res=double(subs(xy,x,t));plot(t,res,'.');pause(0.1);if flag ==0uiwait(handles.figure1);end
delete(y_of_g_plot);endreturn
end
if choose1 == 4 %冲激函数另作讨论x_of_g=x_start:0.01:x_end; %定义函数y时间向量axes(handles.axes3);cla reset; %清空坐标系hold on; %将多张图绘到一个坐标系上grid on;plot(x_of_f,y_of_f,'b'); %绘制x(tao)%坐标轴范围
axis([x_start x_end y_start y_end]);
for t=x_start:0.01:x_endy_of_g=g(t-x_of_g);y_of_g_plot=plot(x_of_g,y_of_g,'r'); %绘制y(t-tao)pause(0.1); %暂定目的看清绘制过程res=double(subs(xy,x,t)); %卷积xy中x用数值t替代plot(t,res,'.'); %绘制卷积结果,注意是一个又一个点if flag ==0uiwait(handles.figure1);enddelete(y_of_g_plot);
endreturn
end
%一般情况
x_of_f=x_start:0.01:x_end; %定义函数x时间向量
y_of_f=f(x_of_f); %得到x的纵坐标
x_of_g=x_start:0.01:x_end; %定义函数y时间向量axes(handles.axes3);cla reset; %清空坐标系hold on; %将多张图绘到一个坐标系上grid on;plot(x_of_f,y_of_f,'b');%绘制x(tao)%坐标轴范围
axis([x_start x_end y_start y_end]);
for t=x_start:0.01:x_endy_of_g=g(t-x_of_g); y_of_g_plot=plot(x_of_g,y_of_g,'r'); %绘制y(t-tao)pause(0.1); %暂定目的看清绘制过程sum=0; %求卷积积分for tao=x_start:0.01:x_endsum=sum+0.01*f(tao)*g(t-tao); %乘以0.01目的,归一化endplot(t,sum,'.'); %绘制卷积结果,注意是一个又一个点if flag ==0uiwait(handles.figure1);enddelete(y_of_g_plot);
end% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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 popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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');
endfunction edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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 selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (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 popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (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
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (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 edit4_Callback(hObject, eventdata, handles)
% hObject handle to edit4 (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 edit4 as text
% str2double(get(hObject,'String')) returns contents of edit4 as a double
% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit4 (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 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)
global flag;
str=get(handles.pushbutton5,'String');
if isequal(str,'pause')flag=0;set(handles.pushbutton5,'String','continue');
elseflag=1;uiresume(handles.figure1);set(handles.pushbutton5,'String','pause');
end
三.界面布局
三个按钮从上到下依次为:
函数x图形:pushbutton1
函数y图形:pushbutton2
运算:pushbutton3
pause:pushbutton5
可编辑文本框从上到下依次为
edit1
edit2
edit3
edit4
下拉菜单从上到下依次为
popupmenu1
popupmenu2
其余为静态文本框
下载代码链接:
点击下载代码