www.gusucode.com > matlab编程pi4-DQPSK的仿真程序,GUI界面,可以计算误码率源码程序 > gusucode/com_process.m

    function varargout = com_process(varargin)
% COM_PROCESS M-file for com_process.fig
%      COM_PROCESS, by itself, creates a new COM_PROCESS or raises the existing
%      singleton*.
%
%      H = COM_PROCESS returns the handle to a new COM_PROCESS or the handle to
%      the existing singleton*.
%
%      COM_PROCESS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in COM_PROCESS.M with the given input arguments.
%
%      COM_PROCESS('Property','Value',...) creates a new COM_PROCESS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before com_process_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to com_process_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

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help com_process

% Last Modified by GUIDE v2.5 12-Sep-2005 10:17:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @com_process_OpeningFcn, ...
                   'gui_OutputFcn',  @com_process_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 com_process is made visible.
function com_process_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 com_process (see VARARGIN)

% Choose default command line output for com_process
handles.output = hObject;

handles.pathDefault=pwd;
% Update handles structure
guidata(hObject, handles);

% add current path to the matlab search path.
addpath(handles.pathDefault);

%
%--------------------------------------------------------------------------
handle=waitbar(0,'Tetra system simulation...','Name','Starting Program...');
pause(0.1);
for i=1:100
    waitbar(i/100,handle,['Percentage:',num2str(i),'% '],handle);
end
close(handle);
%-------------------------------------------------------------------------

set(handles.txtTime,'string',date);

axes(handles.axes1);
grid on;

%
%--------------------------------------------
set(hObject,'Toolbar','none');
%-----------------------------------------------


load BtnCData;
set(handles.btnexit,'CData',X_close);

%
param.NumSig=100;
param.srState=12345;
param.dpt=3;
param.SNR=10;
param.State=71;
param.Td=0.001;
param.FD=40;
param.DelayVector='[0 2e-6]';
param.GainVector='[0 -3]';
assignin('base','param',param);
% end of Reference Initialized.

% UIWAIT makes com_process wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = com_process_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 btnSource.
function btnSource_Callback(hObject, eventdata, handles)
% hObject    handle to btnSource (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 btnCode.
function btnCode_Callback(hObject, eventdata, handles)
% hObject    handle to btnCode (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 btnMod.
function btnMod_Callback(hObject, eventdata, handles)
% hObject    handle to btnMod (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 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 button press in btnImportData.
function btnImportData_Callback(hObject, eventdata, handles)
% hObject    handle to btnImportData (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

Check_matFile=0;
path_pwd=handles.pathDefault;
path=strcat(handles.pathDefault,'\parameters\');
cd(path);
set(handles.txt_simState,'String','Importing configuration data...');
[FileName,PathName,FilterIndx]=uigetfile({'*.mat','Mat-files(*.mat)';'*.*','All files(*.*)'},'Import Data');
if FilterIndx
    load(FileName);
    if ~Check_matFile
        msgbox('Invalid data file! Please ensure that you make the data file correctly.','Error','warn');
        return;
    else
        assignin('base','param',param);
        set(handles.txt_simState,'String','Import configuration data successful');
    end
else
    disp('User cancel! Import configuration data not succeed!');
    set(handles.txt_simState,'String','Fail to import configuration data');
end
cd(path_pwd);

% --- Executes on button press in btnSaveData.
function btnSaveData_Callback(hObject, eventdata, handles)
% hObject    handle to btnSaveData (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.txt_simState,'String','Saving configuration data...');
path_pwd=handles.pathDefault;
path=strcat(handles.pathDefault,'\parameters\');
cd(path);
[FileName,PathName,FilterIndx]=uiputfile({'*.mat','Mat-files(*.mat)';'*.*','All files(*.*)'},'Save Parameters');
if FilterIndx
    Check_matFile=1;
    param=evalin('base','param');
    save(FileName,'param','Check_matFile');
    set(handles.txt_simState,'String','Save configuration data successful');
else
    disp('User canceled ! Configuration data not save!');
    set(handles.txt_simState,'String','Fail to save configuration data');
end
cd(path_pwd);

% --- Executes on button press in btnstart.
function btnstart_Callback(hObject, eventdata, handles)
% hObject    handle to btnstart (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.txt_simState,'String','Starting Simulation...');
param=evalin('base','param');

Data.x=randint(1,param.NumSig,[0 1],param.srState);

Data.xCoded_Block=[];
Data.xCoded_Cyclic=[];
Data.xCoded_Interleaver=[];

tt=mod(param.NumSig,4);
if tt
    Data.x=[Data.x zeros(1,4-tt)];
end
% Linear coding.
xCode=zeros(1,4);
for k=1:4:param.NumSig
    xCode(1)=Data.x(k);
    xCode(2)=Data.x(k+1);
    xCode(3)=Data.x(k+2);
    xCode(4)=Data.x(k+3);
    
    BlockGen=[1 0 0 0 1 1 1;0 1 0 0 1 1 0;0 0 1 0 1 0 1; 0 0 0 1 0 1 1];
    Data.xCoded_Block=[Data.xCoded_Block rem(xCode*BlockGen,2)];
end

% xCoded_Block must be the mutiple of 4
tt=mod(length(Data.xCoded_Block),4);
if tt
    Data.xCoded_Block=[Data.xCoded_Block zeros(1,4-tt)];
end

% Cyclic coding
i=1;
for k=1:4:length(Data.xCoded_Block)
    xCode(1)=Data.xCoded_Block(k);
    xCode(2)=Data.xCoded_Block(k+1);
    xCode(3)=Data.xCoded_Block(k+2);
    xCode(4)=Data.xCoded_Block(k+3);
    
    %CyclicPoly=[1 0 1 1];
    CyclicGen=[1 0 1 1 0 0 0;1 1 1 0 1 0 0;1 1 0 0 0 1 0;0 1 1 0 0 0 1];
    %xCoded_Cyclic=[xCoded_Cyclic rem(tx*CyclicGen,2)];
    Data.xCoded_Cyclic(i,:)=rem(xCode*CyclicGen,2);
    i=i+1;
end


tt=mod(i-1,param.dpt);
if tt
    Data.xCoded_Cyclic=[Data.xCoded_Cyclic;zeros(param.dpt-tt,7)];
end

% Interleaver coding
[n,m]=size(Data.xCoded_Cyclic);
for k=1:param.dpt:n
    for cln=1:7
       for j=0:param.dpt-1
           Data.xCoded_Interleaver=[Data.xCoded_Interleaver Data.xCoded_Cyclic(k+j,cln)];
       end
    end
end

% convert to 1-D column vector
Data.xCoded_Interleaver=Data.xCoded_Interleaver(:);

tt=mod(length(Data.xCoded_Interleaver),2);
odd_check=0;
if tt
    Data.xCoded_Interleaver=[Data.xCoded_Interleaver;0];
    odd_check=1;
end

% convert to  2-D column vector
for k=1:2:length(Data.xCoded_Interleaver)
    Data.bit_txSig((k+1)/2,1)=Data.xCoded_Interleaver(k);
    Data.bit_txSig((k+1)/2,2)=Data.xCoded_Interleaver(k+1);
end


[n,m]=size(Data.bit_txSig);
for i=1:n
    Data.int_txSig(i,1)=Data.bit_txSig(i,1)*2+Data.bit_txSig(i,2);
end

% pi/4DQPSK modulation
Data.txSigMod=dpskmod(Data.int_txSig,4,pi/4);

% Add noise
% rayleighchan(Ts,FD,TAU,PDB)
% awgn()
chan=rayleighchan(param.Td,param.FD,[0 2e-6],[0 -3]);
Data.FadeSig=filter(chan,Data.txSigMod);
Data.FadeSig=awgn(Data.FadeSig,param.SNR,'measured');

% demodulated
Data.rxSigdemod=dpskdemod(Data.FadeSig,4,pi/4);

% symbol error ratio
[symNum,symRatio]=symerr(Data.int_txSig,Data.rxSigdemod);

[n,m]=size(Data.rxSigdemod);
% convert to n x 2 dimension bit vector
for i=1:n
    switch Data.rxSigdemod(i)
        case 0
            Data.rxSigBit(i,1)=0;
            Data.rxSigBit(i,2)=0;
        case 1
            Data.rxSigBit(i,1)=0;
            Data.rxSigBit(i,2)=1;
        case 2
            Data.rxSigBit(i,1)=1;
            Data.rxSigBit(i,2)=0;
        case 3
            Data.rxSigBit(i,1)=1;
            Data.rxSigBit(i,2)=1;
        otherwise
    end
end

% convert to N x 1 bit vector
for i=1:n
    Data.rxSig_cln(2*i-1,1)=Data.rxSigBit(i,1);
    Data.rxSig_cln(2*i,1)=Data.rxSigBit(i,2);
end

[n,m]=size(Data.rxSig_cln);
if odd_check
    Data.rxSig_cln=Data.rxSig_cln(1:n-1);
end

[n,m]=size(Data.rxSig_cln);
if mod(n,param.dpt)
    error('find some error ');
end

%
k=1;
for i=1:param.dpt:n
    for j=0:param.dpt-1
        Data.rxSig_dpt(k,j+1)=Data.rxSig_cln(i+j);
    end
     k=k+1;
end

[n,m]=size(Data.rxSig_dpt);
Data.rxdecoded_Interleaver=[];
for i=1:7:n
    for j=1:m
        for k=0:6
            Data.rxdecoded_Interleaver=[Data.rxdecoded_Interleaver Data.rxSig_dpt(i+k,j)];
        end
    end
end


Data.rxdecoded_Cyclic=decode(Data.rxdecoded_Interleaver,7,4,'cyclic',[1 0 1 1]);

[n,m]=size(Data.rxdecoded_Cyclic);
tt=mod(n,7);
if tt
    Data.rxdecoded_Cyclic=[Data.rxdecoded_Cyclic;zeros(7-tt,1)];
end

%assignin('base','sddd',rxdecoded_Cyclic);

% 
Data.rxdecoded_Linear=decode(Data.rxdecoded_Cyclic,7,4,'linear',BlockGen);

xx=Data.x(1:param.NumSig).';

%
[bitNum,bitRatio]=biterr(xx,Data.rxdecoded_Linear(1:param.NumSig));

assignin('base','Data',Data);

axes(handles.axes1);
set(handles.radiobutton1,'Value',1);
if param.NumSig>40
    stem((1:40),xx(1:40),'rx');
    axis([0 40 -1 2]);
else
    stem(Data.x(1:param.NumSig),'rx');
    axis([0 param.NumSig+2 -1 2]);
end
grid on;

set(handles.txt_simState,'String','Simulation completed!');
set(handles.txt_NumSymbol,'String',length(Data.int_txSig));
set(handles.txt_SymErr,'String',symNum);
set(handles.txt_SymErrRatio,'String',symRatio);
set(handles.txt_NumBit,'String',param.NumSig);
set(handles.txt_BitErr,'String',bitNum);
set(handles.txt_BitErrRatio,'String',bitRatio);

% --- Executes on button press in btnexit.
function btnexit_Callback(hObject, eventdata, handles)
% hObject    handle to btnexit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close;

% --- Executes on button press in radiobutton5.
function radiobutton5_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton5


% --- Executes on button press in radiobutton6.
function radiobutton6_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton6


% --- Executes on button press in btnSetParam.
function btnSetParam_Callback(hObject, eventdata, handles)
% hObject    handle to btnSetParam (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ParamSet




function edit_symbolNum_Callback(hObject, eventdata, handles)
% hObject    handle to edit_symbolNum (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 edit_symbolNum as text
%        str2double(get(hObject,'String')) returns contents of edit_symbolNum as a double


% --- Executes during object creation, after setting all properties.
function edit_symbolNum_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_symbolNum (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1
param=evalin('base','param');
Data=evalin('base','Data');
if param.NumSig>40
    stem((1:40),Data.x(1:40),'rx');
    axis([0 40 -1 2]);
else
    stem(Data.x(1:param.NumSig),'rx');
    axis([0 param.NumSig+2 -1 2]);
end

grid on;


% --- Executes on button press in btn_importData.
function btn_importData_Callback(hObject, eventdata, handles)
% hObject    handle to btn_importData (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

Check_matFile=0;
path_pwd=handles.pathDefault;
path=strcat(handles.pathDefault,'\Data\');
cd(path);
set(handles.txt_simState,'String','Importing data...');
[FileName,PathName,FilterIndx]=uigetfile({'*.mat','Mat-files(*.mat)';'*.*','All files(*.*)'},'Import Data');
if FilterIndx
    load(FileName);
    if ~Check_matFile
        msgbox('Invalid data file! Please ensure that you make the data file correctly.','Error','warn');
        return;
    else
        assignin('base','Data',Data);
        set(handles.txt_simState,'String','Import simulation data successful');
    end
else
    disp('User cancel! Import simulation data not succeed!');
    set(handles.txt_simState,'String','Fail to import simulation data');
end
cd(path_pwd);


% --- Executes on button press in btn_saveData.
function btn_saveData_Callback(hObject, eventdata, handles)
% hObject    handle to btn_saveData (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.txt_simState,'String','Saving configuration data...');
path_pwd=handles.pathDefault;
path=strcat(handles.pathDefault,'\Data\');
cd(path);
[FileName,PathName,FilterIndx]=uiputfile({'*.mat','Mat-files(*.mat)';'*.*','All files(*.*)'},'Save Data');
if FilterIndx
    Check_matFile=1;
    Data=evalin('base','Data');
    save(FileName,'Data','Check_matFile');
    set(handles.txt_simState,'String','Save simulation data successful');
else
    disp('User canceled ! Simulation data not save!');
    set(handles.txt_simState,'String','Fail to save simulation data');
end
cd(path_pwd);



% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton2

Data=evalin('base','Data');
axes(handles.axes1);
plot(Data.txSigMod,'r-');
axis([-1.8 1.8 -1 1]);
grid on;


% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton3
Data=evalin('base','Data');
axes(handles.axes1);
plot(Data.FadeSig,'r-');
axis([-2.8 2.8 -1.6 1.6]);
grid on;



% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton4
param=evalin('base','param');
Data=evalin('base','Data');
if param.NumSig>40
    stem((1:40),Data.rxdecoded_Linear(1:40),'rx');
    axis([0 40 -1 2]);
else
    stem(Data.rxdecoded_Linear(1:param.NumSig),'rx');
    axis([0 param.NumSig+2 -1 2]);
end

grid on;