How to find start and end points of signal in data file?
Show older comments
I have several hundred data files containing electric field over time measurements from a real system. The data has ~20 seconds of lead and trail time where the signal is off, so the data at the beginning and end of the file is just noise. I want to isolate the portion of the file containing the actual signal.
However, that 20 seconds is not exact so I can't just use that to index into the data file. I need some way to detect the start and stop of the signal. Visually, it's incredibly obvious, but I'm struggling to do it reliably programmatically. Any help is appreciated. I do have the signal processing toolbox.
The file I attached is for an applied signal at 1500 Hz, with a 10 kHz sample rate. I have files with applied signals all the way down to milliHertz, so it's a wide range of frequencies to deal with.

2 Comments
Matt J
on 3 Sep 2026
Where would the signal start and end in your posted example?
Daniel Flisek
on 3 Sep 2026
Edited: Daniel Flisek
on 3 Sep 2026
Accepted Answer
More Answers (1)
Another way would be to calculate a suitable threshold, find the index or time values at the points where the signal crosses them, then go from there. (This finds the start and stop index values, then the time values from them, you can work with only the time values if you want. That will likely be faster.) The comparisons used in the find calls are of course inequalities, since it is highly unlikely that the signal will actally equal the threshold values used in that calculation, so the derived time values are necessarily approximate.
Try something like this --
tic
load('sample_data.mat')
toc
format long
% sample_data
Fs = 10E+3;
t = sample_data.(1);
e = sample_data.(2);
[emin,emax] = bounds(e);
emdn = median(e)
ethr_hi = (emax+emdn)/2;
ethr_lo = (emin+emdn)/2;
idx_hi = find(e >= ethr_hi);
[ehi_start,ehi_stop] = bounds(idx_hi);
t_hi_start = t(ehi_start);
t_hi_stop = t(ehi_stop);
idx_lo = find(e <= ethr_lo);
[elo_start,elo_stop] = bounds(idx_hi);
t_lo_start = t(elo_start);
t_lo_stop = t(elo_stop);
t_start = bounds([t_hi_start,t_lo_start])
t_stop = bounds([t_hi_stop,t_lo_stop])
pulse_duration = t_stop - t_start
toc
figure
plot(t, e, DisplayName='Signal')
yline(emdn, '--', DisplayName='Median', LineWidth=1.5, Color=[1 0 1])
yline(ethr_hi, '--', DisplayName='High Threshold', LineWidth=1.5, Color=[1 1 1]*0.5)
yline(ethr_lo, '--', DisplayName='Low Threshold', LineWidth=1.5, Color=[1 1 1]*0.5)
xline(t_start, '-g', DisplayName='Start', LineWidth=1.5)
xline(t_stop, '-r', DisplayName='Stop', LineWidth=1.5)
grid
xlabel('Time (s)')
ylabel('E (units)')
legend(Location='best')
title('Original Data')
toc
figure
plot(t, e, DisplayName='Signal')
yline(emdn, '--', DisplayName='Median', LineWidth=1.5, Color=[1 0 1])
yline(ethr_hi, '--', DisplayName='High Threshold', LineWidth=1.5, Color=[1 1 1]*0.5)
yline(ethr_lo, '--', DisplayName='Low Threshold', LineWidth=1.5, Color=[1 1 1]*0.5)
xline(t_start, '-g', DisplayName='Start', LineWidth=1.5)
xline(t_stop, '-r', DisplayName='Stop', LineWidth=1.5)
grid
xlim([0.99999*t_start 1.00001*t_stop])
xlabel('Time (s)')
ylabel('E (units)')
legend(Location='eastoutside')
title('Zoomed Version')
toc
This may work generally for all your data, however since I don't have them I cannot determine that. It should be relatively straightforward to adapt it.
.
2 Comments
Daniel Flisek
on 11 Sep 2026 at 20:01
Star Strider
on 11 Sep 2026 at 20:35
My pleasure!
Corrrect. I copied and pasted the previous line, however didn't change the argument.
My objective here was to create code that was as efficient as possible, since execution speed seemed to be important.
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






