⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 write_numerichex.m

📁 这是一个关于MATLAB的函数
💻 M
字号:
function dummy = write_numerichex(nn,index,data)
%WRITE_NUMERIC Writes an array of numeric values into DSP memory.
%  WRITE_NUMERIC(MM,DATA)
%  WRITE_NUMERIC(MM,DATA,INDEX)
%  WRITE_NUMERIC(MM,DATA,INDEX,TIMEOUT)
%  WRITE_NUMERIC(MM,DATA,[],TIMEOUT)
%
%   See also READ, WRITE, CAST, INT32.
%
%   Copyright 2002 The MathWorks, Inc.
%   $Revision: 1.5 $ $Date: 2002/05/16 14:13:46 $

error(nargchk(2,3,nargin));
if ~ishandle(nn),
    error('First Parameter must be a NUMERIC Handle.');
end
if nargin==2,   
    data = index;
end

% Is data a valid input
if ischar(data)
    try 
        data = hex2dec(data); %cellstr(data);
    catch
        error('DATA must contain valid hexadecimal characters ');
    end
elseif iscellstr(data)
    nsize = size(data);
    try 
        if length(nsize)==1
            data = reshape([ hex2dec(data) ],1,nsize);
        else
            data = reshape([ hex2dec(data) ],nsize);
        end
    catch
        error('DATA must contain valid hexadecimal characters ')
    end
else 
    % Internal use: do not error on 'else' - use for binary
    % Write_numerichex is called by writebin - this is easier than putting
    % numeric data (bin2dec of data) back into a hexadecimal array of strings 
    % Will generate no error because write(hexdata) does the input
    % checking, not write_numerichex(hexdata)
end

% Process address offset from index
if nargin>=3
    [addroffset,linearoffset] = ComputeOffsetFromIndex(nn,nargin,index);
end

% Reshape value according to ARRAYORDER property
if strcmp(nn.arrayorder,'col-major')
    data = data(1:numel(data));  % linear - write as it is referenced by Matlab
else
    data = reshape( ArrangeDataInCFormat(nn,data),1,[] );
end

% Do we have sufficent values? 
nvalues = numel(data);
nvaldef = prod(nn.size);

if nargin==2 | (nargin>2 & isempty(index))
	if nvalues < nvaldef,
		warning('DATA has less elements than the specified numeric array size, DATA will only be applied to beginning of memory area');
	elseif nvalues > nvaldef,
        warning('DATA has more elements than the specified numeric array size, DATA will be be limited to defined memory area');
        data = data(1:nvaldef);  % Truncate!
    end
else
    if length(linearoffset)==1 & linearoffset+nvalues > nvaldef,
        warning('DATA has extra elements, DATA will be truncated ');
        data = data(1:nvaldef-linearoffset);  % Truncate!
    end
end

% Convert numeric values into array of AUs 
%  where each column is the the AUs necessary for one numeric value
%  The data will be little-endian (swapped later if necessary)
%  thus cc.write([1 256]), where storageunitspervalue = 3, represent = 'unsigned', bitsperstorageunit = 8
%  fdata = 
%    1  0
%    0  1
%    0  0
data = round(double(data));
maxval = 2^nn.wordsize-1;
minval = 0;
if any( maxval < data),
    warning('Overflow: Saturation was required to fit into the specified numeric range');
    [maxdat,inx] = max(data);
    while maxdat > maxval,
        data(inx) = maxval;
        [maxdat,inx] = max(data);
    end 
end
if any(minval > data),
    warning('Undeflow: Saturation was required to fit into the specified numeric range');
    [mindat,inx] = min(data);
    while mindat < minval,
        data(inx) = minval;
        [mindat,inx] = min(data);
    end 
end
data = data*2^nn.prepad;  % scale data by prepend to shift bits within word    
bscaler = nn.bitsperstorageunit*(0:nn.storageunitspervalue-1);
uidata = [];
for val = data,  % Do conversion
    uidata = horzcat(uidata,mod(fix(val./2.^bscaler),2^nn.bitsperstorageunit)');
end

% Endianness swap (if necessary), Little endian only
if strcmp( nn.endianness,'big') & (nn.storageunitspervalue > 1),
    uidata = flipud(uidata);
end    

if nn.bitsperstorageunit <= 8, % (we've already applied rounding)
    uidata = uint8(uidata);
elseif nn.bitsperstorageunit <= 16,
    uidata = uint16(uidata);
elseif nn.bitsperstorageunit <= 32,
    uidata = uint32(uidata);
else
    warning('Host computer does not natively support integer values > 32, data truncation likely')
    uidata = uint32(uidata);
end

% Write raw values to memory
if nargin == 2,
    write(nn.link,nn.address,uidata,nn.timeout);
elseif nargin == 3, % timeout% index version - one value only!
    write(nn.link,nn.suoffset(addroffset-1),uidata,nn.timeout);
elseif nargin == 4 & isempty(index), % timeout
    write(nn.link,nn.address,uidata,timeout);
else
    write(nn.link,nn.suoffset(addroffset-1),uidata,timeout);
end    

%-----------------------------------------
function [addroffset,linearoffset] = ComputeOffsetFromIndex(nn,numargs,index);
if isempty(index),      
    addroffset  = 1;
    linearoffset = 1;
else
    if ~isequal(length(index),length(nn.size)) & length(index)
        error(['Index must be a (1 x '  num2str(length(nn.size)) ') array ' ]);
    end
    if length(nn.size)>1,
        index = p_sub2ind(nn,nn.size,index,nn.arrayorder);
    end
    addroffset = (index-1).*nn.storageunitspervalue + 1;
    linearoffset = index-1;
end

%-----------------------------------
function fdata = ArrangeDataInCFormat(nn,data)
a = [];
siz = size(data);
totalnumel = prod(siz);
if totalnumel<prod(nn.size)
	a   = p_ind2sub(nn,siz,[1:totalnumel]',nn.arrayorder);
	ndx = p_sub2ind(nn,siz,a,'col-major');
elseif totalnumel==prod(nn.size)
	a   = p_ind2sub(nn,nn.size,[1:totalnumel]',nn.arrayorder);
	ndx = p_sub2ind(nn,nn.size,a,'col-major');
else
	a   = p_ind2sub(nn,siz,[1:totalnumel]',nn.arrayorder);
	ndx = p_sub2ind(nn,siz,a,'col-major');
end
fdata = data(ndx);

% [EOF] write_numerichex.m

⌨️ 快捷键说明

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