17 Implementing the Clapper

Our MATLAB implementation of the Clapper is simply the script shown below:

disp('Start recording.')
recordblocking(rec, 3);
disp('End of Recording.');

A = getaudiodata(rec);

% bandpass filter
yF = bandpass(A, [2200 2800], 16000);

% envelope
[yupper,ylower] = envelope(yF, 800, 'peak');

% peaks
[pks, locs] = findpeaks(yupper);

% are there 2 peaks about 0.5s apart?
tf = foundClaps(pks, locs);
if tf
    toggleLED()
end

You should already have this script in the files that you downloaded at the beginning of the lab.

Test your implementation of the Clapper by typing the following into the Command Window:

clapper

When you see the ‘Start recording’ message wait about one second and then clap twice (with about a half-second pause between claps). The LED should turn on or off if your timing between claps is accurate.

Getting the timing between claps is tricky. I found it useful to print out the time between peaks in the function foundClaps (just leave the semi-colon off of the end of the line where you compute the difference between the times of the two peaks). Alternatively, you could modify foundClaps so that it prints out Too fast or Too slow when two claps are detected that are not 0.5 seconds apart.