FRAC2BIN Convert an column vector to binary S=(D,N,M) Inputs: D scalar or column vector to convert N minimum number of integer bits to output [default 1] M number of places after binary point [default 0] Outputs: S String matrix with one value per row. A binary point is included if M>0. The values in D are rounded to the number of displayed bits. If N is negative then leading zeros will be output as spaces if they are to the left of the |N|'th integer column (i.e. N digits will always be output) If M is negative, then values will be truncated rather than rounded. Bug: doesn't yet cope with negative numbers

0001 function s=frac2bin(d,n,m) 0002 %FRAC2BIN Convert an column vector to binary S=(D,N,M) 0003 % Inputs: D scalar or column vector to convert 0004 % N minimum number of integer bits to output [default 1] 0005 % M number of places after binary point [default 0] 0006 % 0007 % Outputs: S String matrix with one value per row. A binary point is included 0008 % if M>0. The values in D are rounded to the number of displayed bits. 0009 % If N is negative then leading zeros will be output as spaces if they are to the 0010 % left of the |N|'th integer column (i.e. N digits will always be output) 0011 % If M is negative, then values will be truncated rather than rounded. 0012 % 0013 % Bug: doesn't yet cope with negative numbers 0014 0015 % Copyright (C) Mike Brookes 2005 0016 % Version: $Id: frac2bin.m 713 2011-10-16 14:45:43Z dmb $ 0017 % 0018 % VOICEBOX is a MATLAB toolbox for speech processing. 0019 % Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html 0020 % 0021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0022 % This program is free software; you can redistribute it and/or modify 0023 % it under the terms of the GNU General Public License as published by 0024 % the Free Software Foundation; either version 2 of the License, or 0025 % (at your option) any later version. 0026 % 0027 % This program is distributed in the hope that it will be useful, 0028 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0029 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0030 % GNU General Public License for more details. 0031 % 0032 % You can obtain a copy of the GNU General Public License from 0033 % http://www.gnu.org/copyleft/gpl.html or by writing to 0034 % Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA. 0035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0036 0037 if nargin<3 0038 m=0; 0039 if nargin<2 0040 n=1; 0041 end 0042 end 0043 l=abs(n); 0044 r=abs(m); 0045 [f,e]=log2(max(d)); 0046 if m<0 0047 v=floor(pow2(d(:),r)); 0048 else 0049 v=round(pow2(d(:),r)); 0050 end 0051 s=setstr(rem(floor(v*pow2(1-max(l,e)-r:0)),2)+'0'); 0052 c=size(s,2)+1; % size including binary point (even if not present) 0053 b=c-r; % position of binary point 0054 if r>0 0055 s(1,c)='0'; % make s bigger 0056 s(:,b+1:c)=s(:,b:c-1); % shift binary places to the right 0057 s(:,b)='.'; 0058 end 0059 q=cumsum(s~='0',2); 0060 if n<0 0061 t=s(:,1:b-l-1); 0062 t(~q(:,1:b-l-1))=' '; 0063 s(:,1:b-l-1)=t; 0064 end 0065

Generated on Mon 06-Aug-2018 14:48:32 by