## Functions diff and split

Another set of functions that's of use in signal detection is diff() and split(). The function diff(v), which was introduced in previous chapters, returns a vector composed of differences of elements in v. The function split(v, indices) splits a vector on indices.

>>> v = arange(io) »> split(v, [4, 8])

[array([0, 2, 3]), array([4, 5, 6, 7]), array([8, 9])]

Example: Simple Detection of Signal in Noise, Part 2

In the previous example, you saw how to perform simple detection using find(). We've displayed all points that were above a specific threshold. In many occasions, we're less interested with points above a threshold because the threshold is arbitrarily chosen; we're more interested with the highest points above a threshold.

Here we pick up from the previous example. This time, we'd like to spot the peak in each detection. Listing 8-4 presents the code to do that.

Listing 8-4. Peak Detections

# peak detections 3 = find(diff(I) > 1) for K in split(I, D+l): ytag = y[K]

peak = find(ytag == max(ytag)) plot(peak+K, ytag[peak], 'sg', ms=7)

The implementation is a bit tricky, so let's walk through it. The idea is this: we split the detections into separate groups, and in each group, we find the peak and plot it.

The first problem of splitting detections makes use of the indices of detected values. A group is considered one detection if the indices are consecutive. Whenever there's a jump in indices, it means a new group:

array([ 9, 10, 11, 12, 13, 14, 42, 43, 44, 45, 46, 47, 66, 67, 68, 69, 70, 71])

So the group [9, 10, 11, 12, 13, 14] is one group, the group [42, 43, 44, 45, 46, 47] is the second group, and the group [66, 67, 68, 69, 70, 71] is the last group.

The function diff(I) will return values other than 1 whenever there's a new group. Whenever the difference is greater than 1, it means the start of a new group:

array([ 1, 1, 1, 1, 1, 28, 1, 1, 1, 1, 1, 19, 1, 1, 1, 1, l]) »> 3 = find(diff(I) > 1)

So we'd like to split on the sixth element (denoted by 5) and the twelfth element (denoted by 11). This is done with the split() function:

[array([ 9, 10, 11, 12, 13, 14]), array([42, 43, 44, 45, 46, 47]), array([66, 67, 68, 69, 70, 71])]

All that's needed now is finding the peak, which is coded as find(ytag == max(ytag)). In Figure 8-10, peak detections are marked by squares.

Simple signal detection in noise

Simple signal detection in noise 50 20 40 60 80 100

Figure 8-10. Peak detections

50 20 40 60 80 100

Figure 8-10. Peak detections