How to calculate/plot a tolerance band in two dimensions?

26 views (last 30 days)
I have a time signal and want to add a value-tolerance-band as well as a time-tolerance band. So to say a sepcific y offset in both plus and minus y directions and a time offset in both negative and positive time.
Y-Offset and X-Offset are independent from another.
As seen on the picture: The black line is my input and i want two arrays containing the datapoints to plot the two dashed lines.
Simply adding and subtracting in both y- and x-direction will give me just the most outer curves, for example with a sinus wave:
Which results in 3 peaks instead of a smooth "plateau" at the maxima, but the area between the three peaks are "allowed" datapoints and should be under the curve as well.
Is there an easy way to do this other than to just shift the curve and comparing maxima at every timestep?

Accepted Answer

belowaveragestudent
belowaveragestudent on 10 Aug 2022
Hi, so for future people who might run into the same problem, I found a solution:
I offset the original curve down/up with the given y-offset. After that, calculate how many timesteps/times the curve has to be shifted to the right/left to get the given x-(time-)offset.
Now shift the upper/lower bound to the left/right by that exact number of elemetns in the array. Compare every new value at those timesteps and if they are greater than the original upper bound (or lower for the lower bound respectively), exchange the value in the upper/lower bound. See my code, I hope it clears up what words couldn't explain:
% Just some input
X = 0:0.1:10;
Y = sin(X);
ValTolerance = 2;
TimeTolerance = 1;
%Generate upper/lower bound without shift to right/left
UpperBound = Y + ValTolerance;
LowerBound = Y - ValTolerance;
%Generate arrays, which are for right/left shifting
UpperRL = UpperBound;
LowerRL = LowerBound;
%Needed times of array indice shifting to get wanted time-delay-tolerance
Dist = find(X >= TimeTolerance, 1)-1;
%Generate matrix in such way that:
% 1st row: X-Values from 0 to 10
% 2nd row: Y Values of sin function
% 3rd row: UpperBound without shifting left/right
% 4th row: Same UpperBound array, to be shiftet left/right
% 5th row: LowerBound without shifting left/right
% 6th row: Same LowerBound array, to be shiftet left/right
M = [X; Y; UpperBound; UpperRL; LowerBound; LowerRL];
%Right direction
for z1 = 1:Dist %shift as often as needed for wanted time-delay-tolerance
M(4,z1+1:end) = M(4,z1:end-1); %shift UpperRL array one element to the right
M(4,1) = 0; %replace first array element with 0, as there is no known value for it
M(6,z1+1:end) = M(6,z1:end-1); %do the same for the LowerRL array
M(6,1) = 0;
for z2 = 1:length(M(1,:)) %compare the whole dataset
if M(4, z2) > M(3, z2) %replace values of the UpperBound array, if the UpperRL values are greater
M(3,z2) = M(4,z2);
end
if M(6, z2) < M(5, z2) %replace values of the LowerBound array, if the LowerRL values are smaller
M(5,z2) = M(6,z2);
end
end
end
%reset UpperRL and LowerRL arrays to do the same thing for the left direction
M(4,:) = UpperBound;
M(6,:) = LowerBound;
%Left direction
for z1 = 1:Dist
M(4,1:end-z1) = M(4,2:(end-z1)+1);
M(4,end) = 0;
M(6,1:end-z1) = M(6,2:(end-z1)+1);
M(6,end) = 0;
for z2 = 1:length(M(1,:))
if M(4, z2) > M(3, z2)
M(3,z2) = M(4,z2);
end
if M(6, z2) < M(5, z2)
M(5,z2) = M(6,z2);
end
end
end
%final arrays of the upper and lower bound
UpperBound = M(3,:);
LowerBound = M(5,:);
It most certainly is not the most potent code but it does its job:
One thing is also the flat ends at the start and at the end, which appear because there is no information about the signal outside the given window.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!