📄 bicoherence.m
字号:
set(findobj(hndf,'Tag','edtInitCond'),'String',num2str(y0,2));
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');
%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'));
y0=str2num(get(findobj(hndf,'Tag','edtInitCond'),'String'));
hndaxes(1)=findobj(hndf,'Tag','aPowSpec');
hndaxes(2)=findobj(hndf,'Tag','aBicoher');
%clear axes and display status
axes(hndaxes(1));cla
axes(hndaxes(2));cla
set(findobj(hndf,'Tag','txtVal'),'String','Computing...');
drawnow
vTimeRange=[vTimeRange(1):1/rFsamp:vTimeRange(2)];
[T,Y]=ode45(fcname,vTimeRange,y0,[],rParam);
[waxis,Pyy,bic]=m_bicoher(Y(:,nSelVar));
axes(hndaxes(1));
bidonstr=get(hndaxes(1),'Tag');
%select the nonredundant region
vIndf1=find(waxis>=0);
%plot commands
plot(waxis(vIndf1),Pyy(vIndf1));
set(hndaxes(1),'XLim',[0 0.5],'XTick',0:0.05:0.5,'Tag',bidonstr);
grid on
title('Power spectrum');xlabel('f_1/f_s');ylabel('Power');
drawnow
axes(hndaxes(2));
clear bidonstr
bidonstr=get(hndaxes(2),'Tag');
%select the nonredundant region
vIndf2=find(waxis>=0 & waxis<=0.25);
%plot commmands
contour(waxis(vIndf1),waxis(vIndf2),bic(vIndf2,vIndf1),8);
axis([0 0.5 0 0.25]);
patch([0 0 0.25],[0 0.25 0.25],[1 1 1],'EdgeColor','none','EraseMode','none');
patch([0.5 0.5 0.25],[0 0.25 0.25],[1 1 1],'EdgeColor','none','EraseMode','none');
set(hndaxes(2),'Layer','top','XTick',0:0.05:0.5,'Tag','aBicoher');
grid on
title('Bicoherence');xlabel('f_1/f_s');ylabel('f_2/f_s');
%store data for new plot
matData=[bic(vIndf2,vIndf1);waxis(vIndf1)';Pyy(vIndf1)'];
matData=[matData [waxis(vIndf2);0;0]];
set(hndf,'UserData',matData);
%display FFT length
set(findobj(hndf,'Tag','txtVal'),'String',['FFT length : ' num2str(length(Pyy))]);
%enable buttons
set(findobj(hndf,'Tag','btnNewPlot'), 'Enable', 'on');
set(findobj(hndf,'Tag','btnExit'), 'Enable', 'on');
set(findobj(hndf,'Tag','btnBrowse'), 'Enable', 'on');
elseif strcmp(action,'NewPlot')
hndf=gcf;
matData=get(hndf,'UserData');
vf1=matData(end-1,1:end-1);
vf2=matData(1:end-2,end);
vPyy=matData(end,1:end-1);
matBic=matData(1:end-2,1:end-1);
fcname=get(findobj(hndf,'Tag','edtTitle'),'String');
h0 = figure('Units','normalized','Color',[1 1 1],'Name','Bicoherence - new plot', ...
'ToolBar','none','Numbertitle','off','Position',[0.175 0.07 0.65 0.85]);
subplot('Position',[0.13 0.615 0.775 0.28]);
plot(vf1,vPyy);
set(gca,'XLim',[0 0.5],'XTick',0:0.05:0.5);
grid on
title('Power spectrum');xlabel('f/f_s');ylabel('Power');
drawnow
subplot('Position',[0.13 0.11 0.775 0.3839]);
contour(vf1,vf2,matBic,8);
axis([0 0.5 0 0.25]);
patch([0 0 0.25],[0 0.25 0.25],[1 1 1],'EdgeColor','none','EraseMode','none');
patch([0.5 0.5 0.25],[0 0.25 0.25],[1 1 1],'EdgeColor','none','EraseMode','none');
set(gca,'Layer','top','XTick',0:0.05:0.5);
grid on
title('Bicoherence');xlabel('f_1/f_s');ylabel('f_2/f_s');
%Text : axes title
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 0],'Position',[0.27 0.93 0.5 0.05],'Style','text', ...
'String',fcname,'FontSize',15);
elseif strcmp(action,'Browse')
d=dir('Cont');
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=BicInit(fcname);
rParam=structData.ParamVal;
rFsamp=structData.Fsamp;
nSelVar=structData.SelVar;
vTimeRange=structData.TimeRange;
y0=structData.InitCond;
if ~isempty(rParam) | ~isempty(rFsamp) | ~isempty(nSelVar) | ~isempty(vTimeRange) | ~isempty(y0)
%display default values
hndaxes(1)=findobj(hndf,'Tag','aPowSpec');
hndaxes(2)=findobj(hndf,'Tag','aBicoher');
axes(hndaxes(1));cla
axes(hndaxes(2));cla
set(findobj(hndf,'Tag','edtParVal'),'String',num2str(rParam,4));
set(findobj(hndf,'Tag','edtSampFreq'),'String',num2str(rFsamp,2));
set(findobj(hndf,'Tag','edtSelVar'),'String',num2str(nSelVar));
set(findobj(hndf,'Tag','edtTimeRange'),'String',num2str(vTimeRange,5));
set(findobj(hndf,'Tag','edtInitCond'),'String',num2str(y0,2));
set(findobj(hndf,'Tag','txtVal'),'String',[])
end
set(findobj(hndf,'Tag','btnStart'), 'Enable', 'on')
set(findobj(hndf,'Tag','btnNewPlot'), 'Enable', 'off')
set(findobj(hndf,'Tag','btnExit'), 'Enable', 'on')
end
elseif strcmp(action,'Exit')
%clear global variables
clear global
%clear paths
rmpath([pwd '\Cont']);
rmpath([pwd '\Prvt']);
%close figure
close(gcf)
end
%---------------- subroutine -----------------
function srtBicoherenceGUI
%creates a GUI for power spectrum and bicoherence drawing
%Callback strings
cbstrStart='bicoherence(''Start'')';
cbstrNewPlot='bicoherence(''NewPlot'')';
cbstrHelp='helpwin(''bicoherence'','''',''Bicoherence Help Window'')';
cbstrBrowse='bicoherence(''Browse'')';
cbstrExit='bicoherence(''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','Bicoherence','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.72 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.69 BtnWidth BtnHeight],'Style','edit', ...
'Tag','edtParVal');
%Text : sampling frequency
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[xPos 0.6 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.57 BtnWidth BtnHeight],'Style','edit', ...
'Tag','edtSampFreq');
%Text : select variable
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[xPos 0.48 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.45 BtnWidth BtnHeight],'Style','edit', ...
'Tag','edtSelVar');
%Text : time range
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[xPos 0.36 BtnWidth BtnHeight],'String','Time range','Style','text');
%Edit : time range
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 1],'Position',[xPos 0.33 BtnWidth BtnHeight],'Style','edit', ...
'Tag','edtTimeRange');
%Text : initial conditions
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[0.513725 0.6 0.694118], ...
'Position',[xPos 0.24 BtnWidth BtnHeight],'String','Initial conditions','Style','text');
%Edit : time range
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 1],'Position',[xPos 0.21 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 : power spectrum
axes('Parent',h0,'Position',[0.1 0.64 0.68 0.255],'Tag','aPowSpec');
box
title('Power spectrum');xlabel('f_1/f_s');ylabel('Power');
%Axes : bicoherence
axes('Parent',h0,'Position',[0.1 0.15 0.68 0.37],'Tag','aBicoher');
box
title('Bicoherence');xlabel('f_1/f_s');ylabel('f_2/f_s');
%Edit : axes title
uicontrol('Parent',h0,'Units','normalized','BackgroundColor',[1 1 1], ...
'ForegroundColor',[0 0 0],'Position',[0.291 0.94 0.3 0.05],'Style','edit', ...
'Tag','edtTitle');
%Button : browse
uicontrol('Parent',h0,'Units','normalized','Position',[0.593 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 + -