how to remove this error in converting the filter coefficients into csd format???Warning: Some precision has been lost. Returned value is truncated! > Error using findstr Input strings must have one row.

2 views (last 30 days)
clc; clear all; close all; N = 13; % filter length Kp = 1; % pass-band weight Ks = 2; % stop-band weight wp = 0.4*pi; % pass-band edge ws = 0.5*pi; % stop-band edge wo = (wp+ws)/2; % cut-off freq. L = 1000; % grid size w = [1:L]'*pi/L; % frequency W = Kp*(w<=wp) + Ks*(w>=ws); % weight function D = (w<=wo); % desired function SN=1e-8; M = (N-1)/2; R = M + 2; % R = size of reference set k = [51 101 341 361 531 671 701 851]; m = 0:M; s = (-1).^(1:R)'; while 1 x = [cos(w(k)*m), s./W(k)] \ D(k); a = x(1:M+1); del = x(M+2); h = [a(M+1:-1:2); 2*a(1); a(2:M+1)]/2; A = freqz(h,1,L)'; A=A(:) err= (A-D).*W; newk = sort([locmax(err); locmax(-err)]); errk = (A(newk)-D(newk)).*W(newk); v = abs(errk) >= (abs(del)-SN); newk = newk(v); errk = errk(v); v = etap(errk); newk = newk(v); errk = errk(v); % if newk is too large, remove points until size is correct while length(newk) > R if abs(errk(1)) < abs(errk(length(newk))) newk(1) = []; else newk(length(newk)) = []; end end % --------------- Check Convergence ------------------------------- if (max(errk)-abs(del))/abs(del) < SN disp('I have converged.') break end k = newk; end del = abs(del); h = [a(M+1:-1:2); 2*a(1); a(2:M+1)]/2; y=abs(A); display(y);
[digits,pos,neg,err]=csdigit(y,20,20); %csd function [digits,pos,neg,err]=csdigit(num,range,resolution) % Returns the canonical signed digit representation of the input number. % Useful for simple DSP implementations of multipliers. % [digits,pos,neg,err]=csdigit(num,range,resolution) % % example csdigit(23) returns +0-00-. % Where +0-00-. is a representation of +1 in 2^5, -1 in 2^3 and -1 in 2^0 positions. % i.e. 23 = 32 - 8 -1 % % example [a,p,n]=csdigit(23.5,6,2) returns % a = +0-000.-0 % p=32 % n=8.5 % 23.5 = 32 - 8 -.5 % % num = input number (decimal) % range = maximum digits to the left of the decimal point % resolution = digits to the right of decimal point % for example % csdigit(.25,2,2) represents xx.xx binary % csdigit(.25,0,4) represents .xxxx binary % Note: An error is generated if num does not fit inside the representation. % is truncated to the specified resolution. % Note: A warning message is generated if num doesn't fit in the precision % specified. The returned value is truncated. % Note: The range is not used except for a check for overflow and to add % leading zeros. % % Patrick J. Moran % AirSprite Technologies Inc.
% Copyright 2006, Patrick J. Moran for AirSprite Technologies Inc. % Redistribution and use in source and binary forms, with or without modification, % are permitted provided that the following conditions are met: % % 1. Redistributions of source code must retain the above copyright notice. % 2. Any derivative of this work must credit the author, and must be made available to everyone. % 3. The name of the author may not be used to endorse or promote products derived % from this software without specific prior written permission. % % THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, % INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A % PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, % INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT % LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; % OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY % WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. if nargin<1 help csdigit; return end if nargin<2 range=ceil(log(abs(num))/log(2))+1; end if nargin<3 resolution=0; end targetNum=num; num=abs(num)/(2^(-resolution)); if num~=floor(num) if floor(num)==0 error('Insufficient precision to represent this number!') else warning('Some precision has been lost. Returned value is truncated!') end end num=floor(num); if num>(2^(range+resolution)) error('Input number is too large for the requested bit representation.'); end binNum=dec2bin(num,range+resolution); digits=('0'+0)*ones(size(binNum)); %allZeros=allZeros; onesLoc=findstr(binNum,'1'); if ~isempty(onesLoc) onesRun=find(diff(onesLoc)==1); while ~isempty(onesRun) onesPointer=find(diff(onesLoc)==1)+1; addIn=onesLoc(onesPointer(end)); adder=('0'+0)*ones(size(binNum)); adder(addIn)=('1'+0); %keep track of the additional digits digits(addIn)=('-'+0); binAdder=char(adder); binNum=dec2bin(bin2dec(binNum)+bin2dec(binAdder),range+resolution); onesLoc=findstr(binNum,'1'); onesRun=find(diff(onesLoc)==1); if length(binNum)>length(digits) digits=['0'+0,digits]; % add any additional leading zero end end end pos=bin2dec(binNum)*2^(-resolution); negLoc=find(digits==('-'+0)); neg=('0'+0)*ones(size(binNum)); neg(negLoc)='1'+0; neg=bin2dec(char(neg))*2^(-resolution); digits(onesLoc)='+'+0; if (length(digits)-resolution)>range warning('Number overflowed for this CSD representation') end if length(digits)<resolution digits=[digits,('0'+0)*ones(1,resolution-length(digits))]; end digits=char([digits(1:length(digits)-resolution),'.'+0,digits(length(digits)-resolution+1:end)]); if targetNum<0 % flip representation if negative ntemp=neg; neg=pos; pos=ntemp; digits=strrep(digits,'+','p'); digits=strrep(digits,'-','+'); digits=strrep(digits,'p','-'); end %digits=char(digits); err=targetNum-(pos-neg);
  2 Comments
Geoff Hayes
Geoff Hayes on 12 Oct 2014
Rather than pasting a 100+ line function (csdigit.m), you should have just attached the m file to your question by using the paperclip button. The same is true for the lines of code that invoke it.
As for the error, type the following in the Command Window before executing your code
dbstop if error
Then run your code. When the error occurs, the debugger will pause at the line that generated the error. Examine the inputs to findstr and hopefully that will give you a clue as to what to do next.
Image Analyst
Image Analyst on 12 Oct 2014
I got a different error:
Undefined function 'locmax' for input arguments of type 'double'.
Error in test3 (line 29)
newk = sort([locmax(err); locmax(-err)]);
What is locmax? You never define it.

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!