How to make threshold lines and mark the 1st intersection line?

Hi, I would like to ask a few questions:
  1. How to set 20% upper and lower threshold from the signal maximum amplitude and make a line of the thresholds.
  2. From the upper threshold line, how to find and mark the 1st line intersection between upper threshold and signal.
  3. How to expand the signal that only focus on the line intersection automatically.
I really appreciate if anyone can help me with matlab coding.
load energy_bigcircle_placement1 %load file
[p,s,mu]=polyfit((1:numel(bigcircle_ant2))',bigcircle_ant2,20);
f_y=polyval(p,(1:numel(bigcircle_ant2))',[],mu);
bigcircle_ant2_data=bigcircle_ant2 - f_y;
tms=(0:numel(bigcircle_ant2_data)-1)/Fs;
figure (1)
plot(tms,bigcircle_ant2_data)
axis tight
title('Signal and Scalogram')
xlabel('Time(s)')
ylabel('Amplitude')
grid on
Regards.

 Accepted Answer

What do you mean by upper and lower 20%? Do you mean like percent of the range from the min to the max value?
theRange = max(bigcircle_ant2_data) - min(bigcircle_ant2_data)
v1 = min(bigcircle_ant2_data) + 0.2 * theRange
v2 = min(bigcircle_ant2_data) + 0.8 * theRange
% Draw horizontal lines across the plot at these values.
line(xlim, [v1, v1], 'Color', 'r')
line(xlim, [v2, v2], 'Color', 'r')
v1 =
-0.185422184752362
v2 =
0.176083685039577
Or do you mean by looking at how often the values occur, like taking the histogram?
sortedValues = sort(bigcircle_ant2_data, 'ascend')
n = numel(sortedValues)
index1 = round(0.2 * n)
index2 = round(0.8 * n)
v1 = sortedValues(index1)
v2 = sortedValues(index2)
line(xlim, [v1, v1], 'Color', 'r')
line(xlim, [v2, v2], 'Color', 'r')
v1 =
-0.00543053385884211
v2 =
0.0115623903477877
load energy_bigcircle_placement1 %load file
[p,s,mu]=polyfit((1:numel(bigcircle_ant2))',bigcircle_ant2,20);
f_y=polyval(p,(1:numel(bigcircle_ant2))',[],mu);
bigcircle_ant2_data=bigcircle_ant2 - f_y;
tms=(0:numel(bigcircle_ant2_data)-1)/Fs;
figure (1)
plot(tms,bigcircle_ant2_data)
axis tight
title('Signal and Scalogram')
xlabel('Time(s)')
ylabel('Amplitude')
grid on
0000 Screenshot.png
To find where the signal first crosses some value, use find(). Use plot() to place a marker there.
index = find(bigcircle_ant2_data > v1, 1, 'first')
x1 = tms(index)
% Plot a circle there
hold on;
plot(x1, bigcircle_ant2_data(index), 'ro', 'MarkerSize', 10, 'LineWidth', 2)
To zoom in to +/1 0.05 around x1, change xlim()
xlim([x1 - 0.05, x1 + 0.05]);

5 Comments

Tqsm for your help,
I already adjusted the coding given based on my requirement but the problem is the circle not showing the intersection between the first blue color line signal and red line. Can u help me on that?
theRange = max(bigcircle_ant2_data) - min(bigcircle_ant2_data)
v1 = 0.2 * theRange
v2 = -0.2 * theRange
% Draw horizontal lines across the plot at these values.
line(xlim, [v1, v1], 'Color', 'r')
line(xlim, [v2, v2], 'Color', 'r')
index = find(bigcircle_ant2_data > v1, 1, 'first')
x1 = tms(index)
% Plot a circle there
hold on;
plot(x1, bigcircle_ant2_data(index), 'ro', 'MarkerSize', 10, 'LineWidth', 2)
Screenshot 2019-12-31 at 1.24.00 PM.png
Try this:
load energy_bigcircle_placement1 %load file
[p,s,mu]=polyfit((1:numel(bigcircle_ant2))',bigcircle_ant2,20);
f_y=polyval(p,(1:numel(bigcircle_ant2))',[],mu);
bigcircle_ant2_data=bigcircle_ant2 - f_y;
tms=(0:numel(bigcircle_ant2_data)-1)/Fs;
figure (1)
plot(tms,bigcircle_ant2_data)
axis tight
title('Signal and Scalogram')
xlabel('Time(s)')
ylabel('Amplitude')
grid on
% Find the 20% and 80% parts of the total range.
theRange = max(bigcircle_ant2_data) - min(bigcircle_ant2_data)
v1 = min(bigcircle_ant2_data) + 0.2 * theRange
v2 = min(bigcircle_ant2_data) + 0.8 * theRange
% Plot horizontal lines across.
line(xlim, [v1, v1], 'Color', 'r', 'LineWidth', 2)
line(xlim, [v2, v2], 'Color', 'r', 'LineWidth', 2)
% Find out where it first goes lower than v1 (the negative one).
index1 = find(bigcircle_ant2_data < v1, 1, 'first')
x1 = tms(index1)
% Plot a circle there
hold on;
plot(x1, bigcircle_ant2_data(index1), 'ro', 'MarkerSize', 10, 'LineWidth', 2)
% Find out where it first goes higher than v2 (the positive one).
index2 = find(bigcircle_ant2_data > v2, 1, 'first')
x2 = tms(index2)
% Plot a circle there
hold on;
plot(x2, bigcircle_ant2_data(index2), 'ro', 'MarkerSize', 10, 'LineWidth', 2)
0000 Screenshot.png
It doesn't lie EXACTLY on the line because there is no point in your data that is exactly on the line.
So the circle is the first point past where the line intersects the red line, and that circle will be around an actual data point.
If you need it exactly on the red line, then you can use bilinear interpolation to find a time where the Amplitude will be exactly the value of the red line.
Hi Image Analyst,
Sorry for contacting you from this previous post. I try to find out how to communicate with you, but I can't find any message button in your profile. I really appreciate if you can help me answering my question here:
Thank you in advance.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!