AJDave
AJDave

Reputation: 3

How to break up a signal into equal blocks based on a threshold

I have a signal which is saved as a csv file. I'm trying to break up the signal into individual blocks of a fixed length (say 100 samples) for analysis (fft, wavelet, amplitude etc) when a threshold is crossed. If the threshold was to be placed at 5, I still need a bit before the threshold included in the block (say 30 of the 100 samples before the major peak, 70 after). How would I go about this, only getting 1 block output per signal pulse? Many thanks in advance!

(https://i.sstatic.net/BicFJ.jpg)

I've tried looping through the array to find data points where they're above the threshold but I'm struggling to reduce it to one point per pulse.

Upvotes: 0

Views: 59

Answers (1)

rinkert
rinkert

Reputation: 6863

assuming your signal is in x, the following would find the index of the first peak that crosses the threshold.

threshold = 5;
n_pre = 30;
n_post = 70;

ind_first_peak = find(x > threshold, 1, 'first');
ind_pre = ind_first_peak - n_pre
ind_end = ind_first_peak + n_post;

ind_selection = ind_start:ind_end;
x_selection = x(ind_selection);

Now we can remove the first peak from the original signal, and repeat:

x_new = x;
x_new(x_selection) = []; % remove the first peak, so x_new can be used by find again.

This is not very robust, but depending on your signal and threshold could work. If you have access to the Signal Processing Toolbox, you could check findpeaks.

Upvotes: 1

Related Questions