From: Khalid Abdulla on
Hi Paul,

I needed to write a similar piece of code myself (a copy is included below). It does include 'for' loops but this is so it can check for an arbitrary number 'N' of consectutive data points; and so it can check any number of data columns for clipping. It works by rotating the time trace round by N-1, N-2, N-3,... 1 time-steps and seeing if resulting columns are equal along any row.

Currently looks for exact equality (not within a tolerance); but this feature could be added. It seems to run pretty quick but not sure it suitable for you.

Cheers,

- Khalid.

function ClipDetect(sgl_in)
%ClipDetect - detect if a raw signal has been clipped

% Seek out N consecutive values in a row which are identicle
% NB sgl must be a number of column vectors arranged into a matrix

num_same = 10; % number of consecutive same points to look for

[rows,cols] = size(sgl_in);

for i = 1:cols % Run check on a single column of data at a time
sgl = zeros(rows,num_same); % to store signal and shifted signals
sgl(:,1) = sgl_in(:,i); % store unshifted signal in 1st coloumn of 'sgl'

for i_same = 2:num_same
% Shift signal by 1, 2, 3, timesteps and save as additional columns of
% 'sgl'
k = i_same-1;
sgl(:,i_same) = sgl([end-k+1:end 1:end-k],1);
end

% detect cliping by checking if all shifted signals are equal; if they are
% max and min will be the same along a row of 'sgl' matrix

IsClip_ = max(sgl,[],2)==min(sgl,[],2);

% sum all 'clips' to give number of 'clip' events as single value

IsClip = sum(IsClip_);

% Check if clipping has occured and output an error message
if IsClip > 0
h = msgbox('WARNING: There appear to be clipping events in the raw data!','Clipping','warn');
uistack(h, 'top'); % put message box on top layer so its seen
end

end

end
From: Khalid Abdulla on
Hi Paul,

I needed to write a similar piece of code myself (a copy is included below). It does include 'for' loops but this is so it can check for an arbitrary number 'N' of consectutive data points; and so it can check any number of data columns for clipping. It works by rotating the time trace round by N-1, N-2, N-3,... 1 time-steps and seeing if resulting columns are equal along any row.

Currently looks for exact equality (not within a tolerance); but this feature could be added. It seems to run pretty quick but not sure it suitable for you.

Cheers,

- Khalid.

function ClipDetect(sgl_in)
%ClipDetect - detect if a raw signal has been clipped

% Seek out N consecutive values in a row which are identicle
% NB sgl must be a number of column vectors arranged into a matrix

num_same = 10; % number of consecutive same points to look for

[rows,cols] = size(sgl_in);

for i = 1:cols % Run check on a single column of data at a time
sgl = zeros(rows,num_same); % to store signal and shifted signals
sgl(:,1) = sgl_in(:,i); % store unshifted signal in 1st coloumn of 'sgl'

for i_same = 2:num_same
% Shift signal by 1, 2, 3, timesteps and save as additional columns of
% 'sgl'
k = i_same-1;
sgl(:,i_same) = sgl([end-k+1:end 1:end-k],1);
end

% detect cliping by checking if all shifted signals are equal; if they are
% max and min will be the same along a row of 'sgl' matrix

IsClip_ = max(sgl,[],2)==min(sgl,[],2);

% sum all 'clips' to give number of 'clip' events as single value

IsClip = sum(IsClip_);

% Check if clipping has occured and output an error message
if IsClip > 0
h = msgbox('WARNING: There appear to be clipping events in the raw data!','Clipping','warn');
uistack(h, 'top'); % put message box on top layer so its seen
end

end

end