mbytepack.m

来自「OFDMA 物理层开发的matlab 源码.飞思卡尔提供.对物理层开发的工程师有」· M 代码 · 共 71 行

M
71
字号
function [output] = mBytePack(input,otype)
% Auxiliary function to pack eight bits into one byte, MSB first.
%
%     output = mBytePack(input,<otype>)
%
% input : input vector or matrix (bits, values 0 or 1)
% otype : output type (optional), default is 'uint8' 
% output: output vector or matrix (bytes, values 0 to 255)
%
% The size of the output is eight times lower than the size of the input.
% If the input size is not a multiple of 8 the input is padded with zeros.
%
% If the input is a matrix, columns are treated as a vector. In this case,
% if the number of rows is not a multiple of 8, rows of zeros are padded.
%
% See also mByteUnpack
%
% Matlab 7 Release 14 SP2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Property of Freescale
%  Freescale Confidential Proprietary
%  Freescale Copyright (C) 2005 All rights reserved
%  ----------------------------------------------------------------------------
%  $RCSfile: mBytePack.m.rca $
%  $Revision: 1.1 $
%  $Date: Mon Dec 11 15:21:52 2006 $
%  Target: Matlab
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%  Written by:  Karsten Mohr
%        Date:  06-Aug-2005

%check input dimension
if ndims(input)~=2
    error('Error: Input must be a vector or 2-dim matrix.');
end

%set default type
if ~exist('otype','var')
  otype='uint8';
end

%get input size
[m,n]=size(input);

%make input binary
if m==1
  input=logical(input.');
else
  input=logical(input);
end
[InputLen,blocks]=size(input);

%zero padding
if mod(InputLen,8)
    input=[input;logical(zeros(8-mod(InputLen,8),blocks))];
    InputLen=InputLen+8-mod(InputLen,8);
end

output=zeros(InputLen/8,blocks,otype);
for k=1:blocks,
 output(:,k)=([128 64 32 16 8 4 2 1]*reshape(input(:,k),8,InputLen/8))';
end

%if input is a row vector then make output a row vector as well
if m==1
  output=output.';
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?