15 Create a function that checks if there are two claps
Edit the function foundClaps.m
that checks if there are exactly two strong peaks separated by approximately one-half of a second. See the help documentation for a description of the function inputs and outputs.
To implement the function you can perform the following steps in order:
- use
find
to find the indices of all values inpks
greater than0.3
- check if the number of indices from the previous step is equal to
2
; if not then the return value of the function isfalse
, else go to the next step - reaching this step means that there are exactly two indices returned by
find
in Step 1. Goto the next step. - supose that the two indices from Step 1 are
[5 22]
. These indices are used to index intolocs
.locs(5)
is the index of a strong peak in the upper envelope. Similarly,locs(22)
is the index of a strong peak in the upper envelope. Use the values of the two indices that you find in Step 1, compute the two corresponding indices of the strong peaks and store them in two separate variables. - convert the indices of the strong peaks that you found in Step 4 to times in seconds using the function
index2time
. Store the two times in two separate variables. - compute the difference in the two times from Step 5. If this difference is between 0.4 seconds and 0.6 seconds then the return value of the function is true (there are two claps separated by approximately 0.5 seconds), otherwise the return value is false (the two claps are not separated by approximately 0.5 seconds).
15.1 Test your function
Test your function using the vector pks and locs from the previous question:
yFiltered = bandpass(y, [2200 2800], 16000);
[yupper, ylower] = envelope(yFiltered, 800, 'peak');
[pks, locs] = findpeaks(yupper);
foundClaps(pks, locs)
Your function should return true if the claps in your audio vector are separated by about one-half of a second.
Next test your function using the audio data where the claps are too quick and too fast. You can process those signals like so:
tooFastFiltered = bandpass(yTooFast, [2200 2800], 16000);
[yupper, ylower] = envelope(tooFastFiltered, 800, 'peak');
[pks, locs] = findpeaks(yupper);
foundClaps(pks, locs)
and
tooSlowFiltered = bandpass(yTooSlow, [2200 2800], 16000);
[yupper, ylower] = envelope(tooSlowFiltered, 800, 'peak');
[pks, locs] = findpeaks(yupper);
foundClaps(pks, locs)
Your function should return false for both of these test inputs.