📄 timefreq.m
字号:
hndf=gcf;
set(findobj(hndf,'Tag','popupGT'), 'Value', 1);
fcname = 'RC_Colpitts';
set(findobj(hndf,'Tag','edtTitle'),'FontSize',10,'String',fcname)
%set default values
structData=TFInit(fcname);
rParam=structData.ParamVal;
rFsamp=structData.fsamp;
nSelVar=structData.SelVar;
vTimeRange=structData.TimeRange;
vIntRange=structData.IntRange;
y0=structData.InitCond;
if ~isempty(rParam) | ~isempty(rFsamp) | ~isempty(nSelVar) | ~isempty(vTimeRange) ...
| ~isempty(vIntRange) | ~isempty(y0)
%display default values
hndaxes(1)=findobj(hndf,'Tag','aTimeDom');
hndaxes(2)=findobj(hndf,'Tag','aFreqDom');
axes(hndaxes(1));cla
axes(hndaxes(2));cla;set(gca,'xlim',[0 0.5])
set(findobj(hndf,'Tag','edtParVal'),'String',num2str(rParam))
set(findobj(hndf,'Tag','edtSampFreq'),'String',num2str(rFsamp))
set(findobj(hndf,'Tag','edtSelVar'),'String',num2str(nSelVar))
set(findobj(hndf,'Tag','edtTimeRange'),'String',num2str(vTimeRange))
set(findobj(hndf,'Tag','edtIntRange'),'String',num2str(vIntRange))
set(findobj(hndf,'Tag','edtInitCond'),'String',num2str(y0))
set(findobj(hndf,'Tag','txtVal'),'String',[])
end
elseif strcmp(action,'Start')
hndf=gcf;
%disable buttons
set(findobj(hndf,'Tag','btnNewPlot'), 'Enable', 'off');
set(findobj(hndf,'Tag','btnExit'), 'Enable', 'off');
set(findobj(hndf,'Tag','btnBrowse'), 'Enable', 'off');
set(findobj(hndf,'Tag','popupGT'), 'Enable', 'off');
%get current values
fcname=get(findobj(hndf,'Tag','edtTitle'),'String');
rParam=str2num(get(findobj(hndf,'Tag','edtParVal'),'String'));
rFsamp=str2num(get(findobj(hndf,'Tag','edtSampFreq'),'String'));
nSelVar=str2num(get(findobj(hndf,'Tag','edtSelVar'),'String'));
vTimeRange=str2num(get(findobj(hndf,'Tag','edtTimeRange'),'String'));
vIntRange=str2num(get(findobj(hndf,'Tag','edtIntRange'),'String'));
y0=str2num(get(findobj(hndf,'Tag','edtInitCond'),'String'));
%clear axes and display status
hndaxes(1)=findobj(hndf,'Tag','aTimeDom');
hndaxes(2)=findobj(hndf,'Tag','aFreqDom');
axes(hndaxes(1));cla
axes(hndaxes(2));cla;set(gca,'xlim',[0 0.5])
set(findobj(hndf,'Tag','txtVal'),'String','Computing...');
drawnow
%select solver and plot commands
nVal=get(findobj(hndf,'Tag','popupGT'),'Value');%get generator type {Cont, Discr}
axes(hndaxes(1));
bidonstr=get(gca,'Tag');
if nVal==1 %Cont
if ~isempty(rFsamp)
vIntRange=[vIntRange(1):1/rFsamp:vIntRange(2)];
end
[T_time,Y_time]=ode45(fcname,vTimeRange,y0,[],rParam);%time domain
plot(T_time,Y_time(:,nSelVar))
[T_freq,Y_freq]=ode45(fcname,vIntRange,y0,[],rParam);%frequency domain
elseif nVal==2 %Discr
[T_time,Y_time]=m_dreTF(fcname,vTimeRange,y0,rParam);%time domain
stem(T_time,Y_time(:,nSelVar))
[T_freq,Y_freq]=m_dreTF(fcname,vIntRange,y0,rParam);%frequency domain
end
set(gca,'Tag',bidonstr)
title('Time domain');xlabel('Time');ylabel('Magnitude');
grid on
[waxis,Pyy]=m_powerspec(Y_freq(:,nSelVar));
vInd=find(waxis>=0);%select the nonredundant region
axes(hndaxes(2));
clear bidonstr
bidonstr=get(gca,'Tag');
plot(waxis(vInd),10*log10(Pyy(vInd)))
set(gca,'Tag',bidonstr)
title('Frequency domain');xlabel('Normalized frequency f/f_s');ylabel('Power [dB]');
grid on
set(hndaxes(2),'XTick',0:0.05:0.5);
%estimate a sampling frequency
fsamp=1/mean(diff(T_freq));
%display sampling frequency used to compute FT
set(findobj(hndf,'Tag','txtVal'),'String',['Sampling frequency: ' num2str(fsamp)]);
%store data for new plot
ud.T_time=T_time;
ud.Y_time=Y_time(:,nSelVar);
ud.fsamp=fsamp;
ud.waxis=waxis;
ud.Pyy=Pyy;
set(hndf,'UserData',ud);
%enable buttons
set(findobj(hndf,'Tag','btnNewPlot'), 'Enable', 'on');
set(findobj(hndf,'Tag','btnExit'), 'Enable', 'on');
set(findobj(hndf,'Tag','btnBrowse'), 'Enable', 'on');
set(findobj(hndf,'Tag','popupGT'), 'Enable', 'on');
elseif strcmp(action,'NewPlot')
%get data
hndf=gcf;
ud=get(hndf,'UserData');
fcname=get(findobj(hndf,'Tag','edtTitle'),'String');
nVal=get(findobj(hndf,'Tag','popupGT'),'Value');%get generator type {Cont, Discr}
%Time domain
figure('Color',[1 1 1],'Name','Time domain - new plot','NumberTitle','off');
if nVal==1
plot(ud.T_time,ud.Y_time);
elseif nVal==2
stem(ud.T_time,ud.Y_time)
end
grid on
title(fcname);xlabel(['Time']);ylabel('Magnitude');
%Frequency domain
figure('Color',[1 1 1],'Name','Frequency domain - new plot','NumberTitle','off');
vInd=find(ud.waxis>=0);
plot(ud.waxis(vInd),10*log10(ud.Pyy(vInd)));
grid on
set(gca,'XTick',0:0.05:0.5);
title(fcname);
xlabel(['Normalized frequency f/f_s(f_s=' num2str(ud.fsamp,'%3g') ')']);
ylabel('Power [dB]');
elseif strcmp(action,'GT')
hndf=gcf;
nVal=get(findobj(hndf,'Tag','popupGT'),'Value');%get generator type {Cont, Discr}
%modify GUI
hndaxes(1)=findobj(hndf,'Tag','aTimeDom');
hndaxes(2)=findobj(hndf,'Tag','aFreqDom');
axes(hndaxes(1));cla
axes(hndaxes(2));cla;set(gca,'xlim',[0 0.5])
set(findobj(hndf,'Tag','edtTitle'),'String',[])
set(findobj(hndf,'Tag','edtParVal'),'String',[])
set(findobj(hndf,'Tag','edtSampFreq'),'String',[])
set(findobj(hndf,'Tag','edtSelVar'),'String',[])
set(findobj(hndf,'Tag','edtTimeRange'),'String',[])
set(findobj(hndf,'Tag','edtIntRange'),'String',[])
set(findobj(hndf,'Tag','edtInitCond'),'String',[])
set(findobj(hndf,'Tag','txtVal'),'String',[])
if nVal==1 %Cont
set(findobj(hndf,'Tag','edtSampFreq'),'Enable','on');
elseif nVal==2 %Discr
set(findobj(hndf,'Tag','edtSampFreq'),'Enable','off');
end
set(findobj(hndf,'Tag','btnBrowse'),'Enable','on');
set(findobj(hndf,'Tag','btnStart'),'Enable','off');
set(findobj(hndf,'Tag','btnNewPlot'),'Enable','off');
elseif strcmp(action,'Browse')
nVal=get(findobj(gcf,'Tag','popupGT'),'Value');%get generator type {Cont, Discr}
if nVal==1
d=dir('Cont');
elseif nVal==2
d=dir('Discr');
end
str = {d.name};
[Selection,ok] = listdlg('PromptString','Select a file:',...
'SelectionMode','single',...
'ListString',str(3:end));
if ok
s=cell2struct(str(2+Selection),{'str'},1);
fcname=s.str(1:end-2);
hndf=gcf;
set(findobj(hndf,'Tag','edtTitle'),'FontSize',10,'String',fcname)
%set default values
structData=TFInit(fcname);
rParam=structData.ParamVal;
rFsamp=structData.fsamp;
nSelVar=structData.SelVar;
vTimeRange=structData.TimeRange;
vIntRange=structData.IntRange;
y0=structData.InitCond;
if ~isempty(rParam) | ~isempty(rFsamp) | ~isempty(nSelVar) | ~isempty(vTimeRange) ...
| ~isempty(vIntRange) | ~isempty(y0)
%display default values
hndaxes(1)=findobj(hndf,'Tag','aTimeDom');
hndaxes(2)=findobj(hndf,'Tag','aFreqDom');
axes(hndaxes(1));cla
axes(hndaxes(2));cla;set(gca,'xlim',[0 0.5])
set(findobj(hndf,'Tag','edtParVal'),'String',num2str(rParam))
set(findobj(hndf,'Tag','edtSampFreq'),'String',num2str(rFsamp))
set(findobj(hndf,'Tag','edtSelVar'),'String',num2str(nSelVar))
set(findobj(hndf,'Tag','edtTimeRange'),'String',num2str(vTimeRange))
set(findobj(hndf,'Tag','edtIntRange'),'String',num2str(vIntRange))
set(findobj(hndf,'Tag','edtInitCond'),'String',num2str(y0))
set(findobj(hndf,'Tag','txtVal'),'String',[])
end
set(findobj(hndf,'Tag','btnStart'), 'Enable', 'on')
set(findobj(hndf,'Tag','btnNewPlot'), 'Enable', 'off')
end
elseif strcmp(action,'Exit')
%clear global variables
clear global
%clear paths
rmpath([pwd '\Cont']);
rmpath([pwd '\Discr']);
rmpath([pwd '\Prvt']);
%close figure
close(gcf)
end
%---------------- subroutine -----------------
function srtTimeFreqGUI
%creates a GUI for time evolution and power spectrum drawing
%Callback strings
cbstrStart='timefreq(''Start'')';
cbstrNewPlot='timefreq(''NewPlot'')';
cbstrHelp='helpwin(''timefreq'','''',''Time and Frequency Domain Help Window'')';
cbstrGT='timefreq(''GT'')';
cbstrBrowse='timefreq(''Browse'')';
cbstrExit='timefreq(''Exit'')';
WinWidth=0.75; %Window width
WinHeight=0.85; %Window height
BtnWidth=0.15; %Button width
BtnHeight=0.05; %Button height
xPos=0.835; %X-position of buttons
h0 = figure('Units','normalized','Color',[1 1 1],'Name','Time/Frequency Domain','NumberTitle','off', ...
'Position',[0.125 0.05 WinWidth WinHeight],'MenuBar','none','ToolBar','none', ...
'Resize','off','Visible','off');
%Frame : left
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[0.82 0 0.2 1],'Style','frame');
%Frame : bottom
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[0 0 0.82 0.0676],'Style','frame');
%Button : start
uicontrol('Parent',h0,'Units','normalized','Position',[xPos 0.92 BtnWidth BtnHeight], ...
'String','Start','Tag','btnStart','Callback',cbstrStart,'Enable','on');
%Button : new plot
uicontrol('Parent',h0,'Units','normalized','Position',[xPos 0.85 BtnWidth BtnHeight], ...
'String','New plot','Tag','btnNewPlot','Callback',cbstrNewPlot,'Enable','off');
%Text : parameter value
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[xPos 0.77 BtnWidth BtnHeight],'String','Parameter value','Style','text');
%Edit : parameter value
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 1],'Position',[xPos 0.74 BtnWidth BtnHeight],'Style','edit', ...
'Tag','edtParVal');
%Text : sampling frequency
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[xPos 0.66 BtnWidth BtnHeight],'String','Sampling freq.(fs)','Style','text');
%Edit : sampling frequency
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 1],'Position',[xPos 0.63 BtnWidth BtnHeight],'Style','edit', ...
'Tag','edtSampFreq');
%Text : select variable
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[xPos 0.55 BtnWidth BtnHeight],'String','Select variable','Style','text');
%Edit : select variable
edtInitTime = uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 1],'Position',[xPos 0.52 BtnWidth BtnHeight],'Style','edit', ...
'Tag','edtSelVar');
%Text : time range
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[xPos 0.44 BtnWidth BtnHeight],'String','Time range','Style','text', ...
'Tag','txtTimeRange');
%Edit : time range
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 1],'Position',[xPos 0.41 BtnWidth BtnHeight],'Style','edit', ...
'Tag','edtTimeRange');
%Text : integration range for FFT
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[xPos 0.33 BtnWidth BtnHeight],'String','Int. range for FFT','Style','text', ...
'Tag','txtIntRange');
%Edit : integration range for FFT
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 1],'Position',[xPos 0.3 BtnWidth BtnHeight],'Style','edit', ...
'Tag','edtIntRange');
%Text : initial conditions
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[xPos 0.22 BtnWidth BtnHeight],'String','Initial conditions','Style','text');
%Edit : initial conditions
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 1],'Position',[xPos 0.19 BtnWidth BtnHeight],'Style','edit', ...
'Tag','edtInitCond');
%Button : help
uicontrol('Parent',h0,'Units','normalized','Position',[xPos 0.1 BtnWidth BtnHeight], ...
'String','Help','Tag','btnHelp','Callback',cbstrHelp,'Enable','on');
%Button : exit
uicontrol('Parent',h0,'Units','normalized','Position',[xPos 0.03 BtnWidth BtnHeight], ...
'String','Exit','Tag','btnExit','Callback',cbstrExit,'Enable','on');
%Axes : time domain
axes('Parent',h0,'Position',[0.1 0.64 0.68 0.255],'Tag','aTimeDom');
box
title('Time domain');xlabel('Time');ylabel('Magnitude');
%Axes : power spectrum
axes('Parent',h0,'Position',[0.1 0.15 0.68 0.37],'Tag','aFreqDom');
box
title('Frequency domain');xlabel('Normalized frequency f/f_s');ylabel('Power [dB]');
%Popup : generator type
uicontrol('Parent',h0,'Units','normalized','Position',[0.125 0.935 0.165 0.05], ...
'BackgroundColor',[1 1 1],'ForegroundColor',[0 0 0],'Style','popup', ...
'String','Time continuous|Time discrete','Value',1,'Tag','popupGT','Callback',cbstrGT,'Enable','on');
%Edit : axes title
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 0],'Position',[0.29 0.94 0.3 0.05],'Style','edit', ...
'Tag','edtTitle');
%Button : browse
uicontrol('Parent',h0,'Units','normalized','Position',[0.59 0.946 0.03 0.04], ...
'Style','pushbutton','String','...','Callback',cbstrBrowse, ...
'Tag', 'btnBrowse','Enable','on');
%Text : current values and time
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 1],'Position',[0.0057 0.01 0.81 0.045],'Style','text', ...
'Tag','txtVal');
set(h0,'Visible','on');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -