1. 程式人生 > 其它 >【定位問題】基於matlab GUI RSSI無線定位【含Matlab原始碼 1054期】

【定位問題】基於matlab GUI RSSI無線定位【含Matlab原始碼 1054期】

一、簡介













二、原始碼

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

% Last Modified by GUIDE v2.5 26-Jun-2021 09:51:44

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

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = test_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.
function pushbutton1_Callback(hObject, eventdata, handles)
for t = 1:5
    A = [0,0];
    B = [5*t,5*t*sqrt(3)];
    C = [10*t,0];
    nums = [A(1),A(2),B(1),B(2),C(1),C(2)];
    p = min(nums);
    q = max(nums);
    L = sqrt((A(1)-C(1))^2+(A(2)-C(2))^2);
    m = 10;
    syms b c;
    %生成在[p,q]上滿足均勻分佈的隨機數矩陣
    %即生成一組m行2列的有可能落在等邊三角形區域內的座標
    numbox = p+(q-p)*rand(m,2);%rand函式產生由在(0, 1)之間均勻分佈的隨機陣列成的陣列
    
    %計數初值,最終根據計算將隨機生成的點中落在等邊三角形區域內的座標存放於新的矩陣
    n = 1;
    for i = 1:m
        dA(i) = sqrt((numbox(i,1)-A(1))^2+(numbox(i,2)-A(2))^2);
        dB(i) = sqrt((numbox(i,1)-B(1))^2+(numbox(i,2)-B(2))^2);
        dC(i) = sqrt((numbox(i,1)-C(1))^2+(numbox(i,2)-C(2))^2);
        %將確實在等邊三角形區域內的座標存入P_position矩陣
        if (dA(i)<=L) & (dB(i)<=L) & (dC(i)<=L)
            P_position(n,1) = numbox(i,1);
            P_position(n,2) = numbox(i,2);
     
             b=P_position(n,1);
            c=P_position(n,2);
            n = n+1;
        end
    end
    %N為隨機生成的點中落在等邊三角形區域內的點(測試點)的個數
    N = n-1
    if N == 0
        disp('所取的隨機座標無一落在等邊三角形內,請增大m值重新執行程式.')
        return
    end

    %計算測試點離三個頂點的實際距離
    %dis為N行3列的矩陣,用於存放N個測試點分別到等邊三角形三個頂點A,B,C的實際距離
    for i = 1:N
        dis(i,1) = sqrt((P_position(i,1)-A(1))^2+(P_position(i,2)-A(2))^2);
        dis(i,2) = sqrt((P_position(i,1)-B(1))^2+(P_position(i,2)-B(2))^2);
        dis(i,3) = sqrt((P_position(i,1)-C(1))^2+(P_position(i,2)-C(2))^2);
    end
     a = 7; %由RSSI計算T-R距離時使用的引數
    for i = 1:N
        dis_test(i,1) = Distance(dis(i,1),a);
        dis_test(i,2) = Distance(dis(i,2),a);
        dis_test(i,3) = Distance(dis(i,3),a);
    end
    
    %根據函式Triangle及求得的測試距離進行定位
    %P_calculate為N行2列的矩陣,用於存放定位後的N個座標
    for i = 1:N
        P_temp = Triangle(A,B,C,dis_test(i,1),dis_test(i,2),dis_test(i,3));
        P_calculate(i,1) = P_temp(1);
        P_calculate(i,2) = P_temp(2);
    end

    %由於測試距離相比真實距離有誤差,三角計算中的兩圓有可能無交點,導致方程無實根.
    %於是P_calculate中會出現虛數.在測試中虛數無實際意義,因此取其實部存放於另一矩陣
    for i = 1:N
        P_calculate_real(i,1) = real(P_calculate(i,1));
        P_calculate_real(i,2) = real(P_calculate(i,2));
    end

    %對比測試點的定位座標與實際座標之間的誤差
    P_position;
    P_calculate;
    P_calculate_real;
    %計算定位結果與真實座標之間的距離誤差平均值e_average(測試點等概率)
    e_sum = 0;
    for i = 1:N
        e = sqrt((P_calculate_real(i,1)-P_position(i,1))^2+(P_calculate_real(i,2)-P_position(i,2))^2);
        e_sum = e_sum+e;
    end
    e_average = e_sum/N;
    e_average_percent = e_average/L;
    
    e_average_box(t) = e_average
    e_average_percent_box(t) = e_average_percent
    e_average_box(t) = e_average;
    
   x=e_average_percent_box(t);
   y = e_average_box(t);
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)



function 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)
set(edit1,'string',2);
% 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)
set(edit2,'string',y);
% 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 button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)

for t = 1:5
    A = [0,0];
    B = [5*t,5*t*sqrt(3)];
    C = [10*t,0];
    nums = [A(1),A(2),B(1),B(2),C(1),C(2)];
    p = min(nums);
    q = max(nums);
    L = sqrt((A(1)-C(1))^2+(A(2)-C(2))^2);
    m = 10;
    syms b c;
    %生成在[p,q]上滿足均勻分佈的隨機數矩陣
    %即生成一組m行2列的有可能落在等邊三角形區域內的座標
    numbox = p+(q-p)*rand(m,2);%rand函式產生由在(0, 1)之間均勻分佈的隨機陣列成的陣列
    
    %計數初值,最終根據計算將隨機生成的點中落在等邊三角形區域內的座標存放於新的矩陣
    n = 1;
    for i = 1:m
        dA(i) = sqrt((numbox(i,1)-A(1))^2+(numbox(i,2)-A(2))^2);
        dB(i) = sqrt((numbox(i,1)-B(1))^2+(numbox(i,2)-B(2))^2);
        dC(i) = sqrt((numbox(i,1)-C(1))^2+(numbox(i,2)-C(2))^2);
        %將確實在等邊三角形區域內的座標存入P_position矩陣
        if (dA(i)<=L) & (dB(i)<=L) & (dC(i)<=L)
            P_position(n,1) = numbox(i,1);
            P_position(n,2) = numbox(i,2);
             b=P_position(n,1);
            c=P_position(n,2);
            n = n+1;
        end
    end
    %N為隨機生成的點中落在等邊三角形區域內的點(測試點)的個數
    N = n-1
    if N == 0
        disp('所取的隨機座標無一落在等邊三角形內,請增大m值重新執行程式.')
        return
    end

三、執行結果

四、備註

版本:2014a
完整程式碼或代寫加1564658423