function varargout = tong(varargin)
% TONG MATLAB code for tong.fig
% TONG, by itself, creates a new TONG or raises the existing
% singleton*.
%
% H = TONG returns the handle to a new TONG or the handle to
% the existing singleton*.
%
% TONG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TONG.M with the given input arguments.
%
% TONG('Property','Value',...) creates a new TONG or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before tong_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to tong_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 tong
% Last Modified by GUIDE v2.5 29-Apr-2013 11:36:07
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @tong_OpeningFcn, ...
'gui_OutputFcn', @tong_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before tong is made visible.
function tong_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 tong (see VARARGIN)
% Choose default command line output for tong
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes tong wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = tong_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 during object creation, after setting all properties.
function ps1_axes_CreateFcn(hObject, eventdata, handles)
% hObject handle to ps1_axes (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate ps1_axes
% 对P(s1)的输入进行判断,提高程序的鲁棒性% 对P(s1)的输入进行判断,提高程序的鲁棒性
function ps1_input_Callback(hObject, eventdata, handles)
set(handles.plot_button,'Enable','off')
try
ps1 = eval(get(handles.ps1_input,'String'));
if ~isnumeric(ps1)
% ps1 is not a number
set(handles.plot_button,'String','ps1 is not numeric')
elseif (ps1 > 1) || (ps1 < 0)
% ps1 is too large or too small
set(handles.plot_button,'String','ps1 is not available')
else
% All OK; Enable the Plot button with its original name
set(handles.plot_button,'String','Plot')
set(handles.plot_button,'Enable','on')
return
end
% Found an input error other than a bad expression
% Give the edit text box focus so user can correct the error
uicontrol(hObject)
catch EM
% Cannot evaluate expression user typed
set(handles.plot_button,'String','Cannot plot t')
% Give the edit text box focus so user can correct the error
uicontrol(hObject)
end
% hObject handle to ps1_input (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 ps1_input as text
% str2double(get(hObject,'String')) returns contents of ps1_input as a double
% --- Executes during object creation, after setting all properties.
function ps1_input_CreateFcn(hObject, eventdata, handles)
% hObject handle to ps1_input (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 px = midterm_class1_liklihood(x)
%定义一个以0为中心,(-1,1)的三角分布
px = zeros(size(x));
idx = (x <= double(0))&(x >= double(-1));
px(idx) = x(idx)+1;
idx = (x <= double(1))&(x > double(0));
px(idx) = -1*x(idx)+1;
function px = midterm_class2_liklihood(x)
%定义(0,1)间的均匀分布
px = zeros(size(x));
idx = (x <= double(1))&(x > double(0));
px(idx) = 1;
function total = integrate(x,fx,low_lim,high_lim)
%用矩形方法进行积分
delta = x(2) - x(1);
idx = (x >= low_lim)&(x <= high_lim);
fx = fx(idx);
total = sum(fx*delta);
% --- Executes on button press in plot_button.
function plot_button_Callback(hObject, eventdata, handles)
x = linspace(-1,1,10001);
%可能性:
% pxs1 = p(x|s1)
% pxs2 = p(x|s2)
pxs1 = midterm_class1_liklihood(x);
pxs2 = midterm_class2_liklihood(x);
% Get user input form GUI
ps1 = str2double(get(handles.ps1_input,'String'));
ps2 = 1 - ps1;
set(handles.ps2_output,'String',num2str(ps2));
threshold = x;
Perrors = zeros(size(threshold));
%积分
%类w1为域的左边,w2为域的右边,分别对错误域进行积分
for i=1:length(Perrors),
Perrors(i) = ps2*integrate(x,pxs2,-1,threshold(i))+ ps1*integrate(x,pxs1,threshold(i),1);
end
%求出最小误码率
minpe = min(Perrors);
set(handles.perror_output,'String',num2str(minpe));
threshFin = x(Perrors == minpe);
set(handles.thresh_output,'String',num2str(threshFin));
thresh = zeros(size(x));
thresh(x >= threshFin) = 1;
% Create perror probablity plot in proper axes
plot(handles.perror_axes,x,Perrors,'-m')
set(handles.perror_axes,'XMinorTick','on')
ylabel('P(error)')
% Create s1 probablity plot in proper axes
plot(handles.ps1_axes,x,pxs1,'-b')
set(handles.ps1_axes,'XMinorTick','on')
% Create s2 probablity plot in proper axes
plot(handles.ps2_axes,x,pxs2,'-r')
set(handles.ps2_axes,'XMinorTick','on')
% Create thresh probablity plot in proper axes
plot(handles.x_axes,x,thresh,'-g')
set(handles.x_axes,'XMinorTick','on')
grid off
hold off
% hObject handle to plot_button (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 exit_pushbutton.
function exit_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to exit_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get the current position of the GUI from the handles structure
% to pass to the modal dialog
% pos_size = get(handles.figure1,'Position');
% Call modaldlg with the argument 'Position'
user_response = modaldlg('Title','exit');
switch user_response
case 'No'
% take no action
case 'Yes'
display Goobye
close(handles.figure1);
end
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over text6.
function text6_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to text6 (see GCBO)
% eventdata r