function clipdata = cclip(xin,cliptype,clpercent) % This function applies various form of clipping to the input signal % based a percentage of its peak values. % % [clipdata] = cclip(xin,cliptype,clpercent) % % inputs: % xin => Vector of input signal values % cliptype => a string indicating which cliptype to apply % 'clc' = Soft Center Clip: % C[x(n)]=x(n)-Cl for (x(n)>=Cl), 0 for (|x(n)|=Cl), and 0 for (|x(n)|=Cl), 0 for (|x(n)| percentage of minimum of maximum positive or negative going peak values % clipping level must be between 0 < cl < 1 % % Written by Tim Black, updated by Kevin D. Donohue (donohue@engr.uky.edu) Jan 2006 % The Clipping level, cl, is set to the percentage of the smaller magnitude of the % positive and negative peaks if clpercent > 1 | clpercent < 0 error('Precent of signal for clip threshold should be between 0 and 1') end effmag=min([abs(max(xin)),abs(min(xin))]); cl=clpercent*effmag(1); % Center clip xwin (the current window of xin) according to argument cliptype: clipdata=zeros(1,length(xin)); % Initialize output to zeros ind1=find(xin>=cl); % find all positive points greater than clip threshold ind2=find(xin<=-cl); % fild all negative points with magnitudes greater than clip threshold if cliptype=='clc' % Center soft clip clipdata(ind1)=xin(ind1)-cl; % Reduce amplitude by clip threshold (for positive values) clipdata(ind2)=xin(ind2)+cl; % Reduce amplitude by clip threshold (for negative values) elseif cliptype=='clp' % Center hard clip clipdata(ind1)=xin(ind1); % Pass positive values greater than threshold clipdata(ind2)=xin(ind2); % Pass negative values with magnitudes greather than threshold elseif cliptype == 'sig' % Hard limit center clip clipdata(ind1)=1; % Set postive values to 1; clipdata(ind2)=-1; % Set negative values to -1; else error('Clip Type not specified! Use strings clc, clp, sig, for soft, hard, or hard limit center clip.') end