How to set threshold to trigger for start and stop finding max value?

Hi all,
I am measuring flowing particles with a system. The particle will start to induce a pulse signal once it just enters the sensing zone, and the max amplitude can be obtained around the center of the zone.
What I have considered method by used for and if function is :
  1. Set a threshold for switch on and switch off record the data
  2. Once the amplitude (y>threshold) which means the cell just enter the sensing zone, start the search
  3. When the amplitude (y<threshold) which means the cell almost leave the zone, stop searching
  4. Find the max value from this recording time interval
  5. Also get the time interval
However, I am quite fresh in MATLAB so don't know how to write down them...
Thanks if someone can help me design this tool!

 Accepted Answer

Can you post a diagram, plot, image, data or anything to help illustrate your situation?
Otherwise, just try find:
indexEnters = find(y > threshold, 1, 'first'); % Index when particle enters and signa becomes big enough.
indexLeaves = find(y > threshold, 1, 'last'); % Index when particle leaves and signa becomes small again.

7 Comments

Thanks,
Here is the diagram of my simulation signal, each wave sets means a cell flowing under the detecting zone.
In order to find each maximum value from each sets, I guess I can use "threshold" function to segment them and get not only maximum but also "width" of each wave sets
%Simulation signal generated
fs = 1e9;
tc = gauspuls('cutoff',10e3,0.5,[],-40);
t = -tc:1/fs:tc;
x = gauspuls(t,10e3,0.5);
ts = 0:1/50e3:0.025;
d = [0:1/1e3:0.025;sin(2*pi*0.1*(0:25))]';
y = pulstran(ts,d,x,fs);
figure
plot(ts,y)
xlim([0 0.01])
Try this:
clc; % Clear the command window.
fprintf('Beginning to run %s.m.\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
clear global;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%Simulation signal generated
fs = 1e9;
tc = gauspuls('cutoff',10e3,0.5,[],-40);
t = -tc:1/fs:tc;
x = gauspuls(t,10e3,0.5);
ts = 0:1/50e3:0.025;
d = [0:1/1e3:0.025;sin(2*pi*0.1*(0:25))]';
y = pulstran(ts,d,x,fs);
hFig = figure
subplot(3, 1, 1);
plot(ts,y, 'b-', 'LineWidth', 2)
grid on;
xlim([0 0.01])
hFig.WindowState = 'maximized';
binarySignal = y < 0.0001;
% Plot signal
subplot(3, 1, 2);
plot(ts,binarySignal, 'b-', 'LineWidth', 2)
grid on;
xlim([0 0.01])
title('Signal where y < 0.0001', 'FontSize', 18);
% Find the length of the small inter-wave gaps.
props = regionprops(binarySignal, 'Area');
allLengths = sort([props.Area], 'descend')
% Get rid of pulses less than 15 elements
binarySignal = ~bwareaopen(binarySignal, 15);
% Plot signal
subplot(3, 1, 3);
plot(ts,binarySignal, 'b-', 'LineWidth', 2)
grid on;
xlim([0 0.01])
ylim([0, 1.2]);
title('Signal where there is a pulse', 'FontSize', 18);
subplot(3, 1, 1);
% Now get the start and stop of each pulse
props = regionprops(binarySignal, 'PixelIdxList');
% Print them out
for k = 1 : length(props)
starts(k) = props(k).PixelIdxList(1);
stops(k) = props(k).PixelIdxList(end);
fprintf('Pulse #%d starts at element %d (ts = %f) and stops at element %d (ts = %f).\n', k, starts(k), ts(starts(k)), stops(k), ts(stops(k)));
xline(ts(starts(k)), 'Color', 'g', 'LineWidth', 2);
xline(ts(stops(k)), 'Color', 'r', 'LineWidth', 2);
end
title('Signal with starts and stops indicated', 'FontSize', 18);
In the command window, you'll see:
Pulse #1 starts at element 52 (ts = 0.001020) and stops at element 73 (ts = 0.001440).
Pulse #2 starts at element 102 (ts = 0.002020) and stops at element 123 (ts = 0.002440).
Pulse #3 starts at element 152 (ts = 0.003020) and stops at element 173 (ts = 0.003440).
Pulse #4 starts at element 202 (ts = 0.004020) and stops at element 223 (ts = 0.004440).
Pulse #5 starts at element 301 (ts = 0.006000) and stops at element 321 (ts = 0.006400).
Pulse #6 starts at element 351 (ts = 0.007000) and stops at element 371 (ts = 0.007400).
Pulse #7 starts at element 401 (ts = 0.008000) and stops at element 421 (ts = 0.008400).
Pulse #8 starts at element 451 (ts = 0.009000) and stops at element 471 (ts = 0.009400).
Pulse #9 starts at element 552 (ts = 0.011020) and stops at element 573 (ts = 0.011440).
Pulse #10 starts at element 602 (ts = 0.012020) and stops at element 623 (ts = 0.012440).
Pulse #11 starts at element 652 (ts = 0.013020) and stops at element 673 (ts = 0.013440).
Pulse #12 starts at element 702 (ts = 0.014020) and stops at element 723 (ts = 0.014440).
Pulse #13 starts at element 801 (ts = 0.016000) and stops at element 821 (ts = 0.016400).
Pulse #14 starts at element 851 (ts = 0.017000) and stops at element 871 (ts = 0.017400).
Pulse #15 starts at element 901 (ts = 0.018000) and stops at element 921 (ts = 0.018400).
Pulse #16 starts at element 951 (ts = 0.019000) and stops at element 971 (ts = 0.019400).
Pulse #17 starts at element 1052 (ts = 0.021020) and stops at element 1073 (ts = 0.021440).
Pulse #18 starts at element 1102 (ts = 0.022020) and stops at element 1123 (ts = 0.022440).
Pulse #19 starts at element 1152 (ts = 0.023020) and stops at element 1173 (ts = 0.023440).
Pulse #20 starts at element 1202 (ts = 0.024020) and stops at element 1223 (ts = 0.024440).
Thanks!
But when I copy and run them, here is the error appear:
Unrecognized property 'WindowState' for class 'matlab.ui.Figure'.
Error in test4 (line 23)
hFig.WindowState = 'maximized';
You must have a superold version of MATLAB (pre r2014b I think -- for some reason, you chose not to tell us when it asked you when you made your original posting). Simply comment out that line and it should run, but I'm guessing you figured that out by now.
My current version is r2017b Student for MAC.
Ok, I deleted "line 23" and it works, but still a little error occured:
Undefined function or variable 'xline'.
Error in test4 (line 50)
xline(ts(starts(k)), 'Color', 'g', 'LineWidth', 2);
xline() was introduced in r2018b. You can replace it with line():
line([ts(starts(k)), ts(starts(k))], ylim, 'Color', 'g', 'LineWidth', 2);
If anymore happen, just delete the line that threw an error. Most of them are just for making fancy graphics anyway.
I'll edit your post to add your Product on the right hand side of this page.
Thanks! Closer now, but how about get the max amplitude at each wave sets?

Sign in to comment.

More Answers (0)

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!