%% Peaks of Saturated Signal
% Sensors can return clipped readings if the data are larger than a given saturation point. You can choose to disregard these peaks as meaningless or incorporate them to your analysis.
% Generate a signal that consists of a product of trigonometric functions of frequencies 5 Hz and 3 Hz embedded in white Gaussian noise of variance 0.1². The signal is sampled for one second at a rate of 100 Hz. Reset the random number generator for reproducible results.

rng default

fs = 1e2;
t = 0:1/fs:1-1/fs;

s = sin(2*pi*5*t).*sin(2*pi*3*t)+randn(size(t))/10;

%
% Simulate a saturated measurement by truncating every reading that is greater than a specified bound of 0.32. Plot the saturated signal.

bnd = 0.32;
s(s>bnd) = bnd;

plot(t,s)
xlabel('Time (s)')

%
% Locate the peaks of the signal. findpeaks reports only the rising edge of each flat peak.

[pk,lc] = findpeaks(s,t);

hold on
plot(lc,pk,'x')

%
% Use the 'Threshold' name-value pair to exclude the flat peaks. Require a minimum amplitude difference of 10^{-4} between a peak and its neighbors.

[pkt,lct] = findpeaks(s,t,'Threshold',1e-4);

plot(lct,pkt,'o','MarkerSize',12)

% Copyright 2012 The MathWorks, Inc.