# How to know when a sound in a given frequency starts and ends?

I need to detect two sounds (913 Hz and 1370 Hz), knowing the time in which each starts and ends.

At this time, I am using a band pass filter (butter), and this is the result:

Here is the first time this sound occurs:

The red line is where the sound is supposed to end (276 ms).

So how can I detect that the first time the tone occurs it starts at 0 ms and ends at 276 ms?

• You need to define very carefully and precisely what you mean by start time and end time. Otherwise the answer could theoretically be never (an IIR bandpass filter rings for infinity). – hotpaw2 Feb 23 '16 at 18:19
• @hotpaw2, in the middle of a recording I want to detect those two sounds, in this specific order and specific lengths. By the start and end, I mean when this tone occurs. Let's say, I want to know if someone pressed the key '7' on the phone for 276 milliseconds and the 9 key for 1 second. – Dayvid Oliveira Feb 23 '16 at 18:22
• You've missed the point of what @hotpaw2 wrote. – Carl Witthoft Feb 23 '16 at 20:58

## 2 Answers

I would recommend reading about onset detection. It's about music signals but nevertheless a few ideas might be extracted from it.

From a more general point of view, what I would do is a frame-based analysis of the signal and define some kind of energy threshold/noise floor with which I could determine if there is activity or not.

A matched filter trained on the onset and release keying times for your particular keyboard (perhaps unified, or perhaps divided into the two frequency bands) might work. The length of the matched filter might depend on the S/N ratio and your desired statistical error rate.