Code covered by the BSD License  

Highlights from
Bit Packet Analyzer

image thumbnail

Bit Packet Analyzer

by

 

The Bit Packet Analyzer can be used to analyze bit streams for repeating patterns (e.g. preamble)

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

% Last Modified by GUIDE v2.5 12-Dec-2012 17:42:44

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @BitPacketAnalyzer_gui_OpeningFcn, ...
    'gui_OutputFcn',  @BitPacketAnalyzer_gui_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
end

% --- Executes just before BitPacketAnalyzer_gui is made visible.
function BitPacketAnalyzer_gui_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 BitPacketAnalyzer_gui (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

set(handles.PlotAnalyzis,'String','Set inputs')
set(handles.PlotAnalyzis,'Enable','off')

% set(handles.PackPlot,'Parent', ha);
set(handles.PackPlot,'XTick', []);
set(handles.PackPlot,'YTick', []);
% axis(handles.PackPlot, 'tight');

end


% --- Outputs from this function are returned to the command line.
function varargout = BitPacketAnalyzer_gui_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;
end


% --- Executes on button press in PlotAnalyzis.
function PlotAnalyzis_Callback(hObject, eventdata, handles)
% hObject    handle to PlotAnalyzis (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
maxNpacks = 100;
inVector = evalin('base',get(handles.BitStream_var,'String'));
lenPack = str2double(get(handles.PackLen_var,'String'));

%% Shape data
nElements = numel(inVector);

if (size(inVector,2) > 1) % if
    inVector = reshape(inVector,nElements,1);
end

numPackets = nElements/lenPack;
intNumPackets = ceil(numPackets);

if(numPackets > maxNpacks) % if there's more packets then nMax, truncate the input
    intNumPackets = maxNpacks;
    outVector = inVector(1:intNumPackets*lenPack);
elseif(numPackets ~= intNumPackets) %zero padding is needed
    nZeros = lenPack*intNumPackets - lenPack*numPackets;
    outVector = [inVector; zeros(nZeros,1)];
else
    outVector = inVector;
end

clear inVector numPackets;

% shape the data to show packets in lines
outVector = (reshape(outVector, lenPack, intNumPackets))';

%% Find vertices for patches
outVector = flipud(outVector);
[y,x]=find(outVector == 1);
x = x'; y = y';
xpatch = [x-1; x ; x; x-1];
ypatch = [y-1; y-1; y; y];

%% Plot patches

p = get(handles.PackPlot,'Children');
set(p,'xdata',[],'ydata',[],'zdata',[]);
handles.PackPlot = patch(xpatch,ypatch,'k');

% set(h,'Parent', ha);
% set(ha,'XTick', []);
% set(ha,'YTick', []);
% axis(ha, 'tight');
% set(ha,'visible','on');
% set(f,'visible','on');

handles.Ana_Title = title(['Bit Packet Analysis, Packet Length = ', ...
    num2str(lenPack), '# of Packets = ', num2str(intNumPackets)]);

clear outVector;
end


function PackLen_var_Callback(hObject, eventdata, handles)
% hObject    handle to PackLen_var (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

lenPack = str2double(get(hObject,'String')); %read packet length
if isnan(lenPack) || ~isreal(lenPack)
    % isdouble returns NaN for non-numbers and f1 cannot be complex
    % Disable the Plot button and change its string to say why
    set(handles.PlotAnalyzis,'String','Invalid packet length')
    set(handles.PlotAnalyzis,'Enable','off')
%     set(handles.PlotAnalyzis,'ForegroundColor','red')
    set(handles.PackLen_var,'ForegroundColor','red')
    return
    % Give the edit text box focus so user can correct the error
    uicontrol(hObject)
else
    % Enable the Plot button with its original name
    set(handles.PlotAnalyzis,'String','Plot')
    set(handles.PlotAnalyzis,'Enable','on')
    set(handles.PlotAnalyzis,'ForegroundColor','green')
    set(handles.PackLen_var,'ForegroundColor','black')
    return
end
end



function BitStream_var_Callback(hObject, eventdata, handles)
% hObject    handle to BitStream_var (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

varName = get(hObject,'String');
assignin('base','varName',varName);

if evalin('base','~exist(varName,''var'')')0
    % isdouble returns NaN for non-numbers and f1 cannot be complex
    % Disable the Plot button and change its string to say why
    set(handles.PlotAnalyzis,'String','invalid vector')
    set(handles.PlotAnalyzis,'Enable','off')
%     set(handles.PlotAnalyzis,'ForegroundColor','red')
    set(handles.BitStream_var,'ForegroundColor','red')
    return
else
    inVector = evalin('base',varName);
    if ~isnumeric(inVector)
        % t is not a number
        set(handles.PlotAnalyzis,'String','vector isnt numeric')
        set(handles.PlotAnalyzis,'Enable','off')
%         set(handles.PlotAnalyzis,'ForegroundColor','red')
        set(handles.BitStream_var,'ForegroundColor','red')
        return
    elseif length(inVector) < 2
        set(handles.PlotAnalyzis,'String','vector is too short')
        set(handles.PlotAnalyzis,'Enable','off')
%         set(handles.PlotAnalyzis,'ForegroundColor','red')
        set(handles.BitStream_var,'ForegroundColor','red')
        return
    else
        % All OK; Enable the Plot button with its original name
        set(handles.PlotAnalyzis,'String','Plot')
        set(handles.PlotAnalyzis,'Enable','on')
        set(handles.PlotAnalyzis,'ForegroundColor','green')
        set(handles.BitStream_var,'ForegroundColor','black')
        return
    end
end


end


% --- Executes during object creation, after setting all properties.
function PackLen_var_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PackLen_var (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
end


% --- Executes during object creation, after setting all properties.
function BitStream_var_CreateFcn(hObject, eventdata, handles)
% hObject    handle to BitStream_var (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
end


% --- Executes during object creation, after setting all properties.
function PackPlot_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PackPlot (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 PackPlot

end

Contact us