Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Typing gif from Wikipedia

Spy Keypad

Part 2 for working with audio signals


Overview

A door is encoded with a number pad (0-9). We can’t see the door, but through nefarious means we have a recording of someone opening it. Quick! We need to decode this mystery signal and the order they appear to open the door!

We know that the door code is set up as:

  • A note: 0

  • B note: 1

  • C note: 2

  • D note: 3

  • E note: 4

  • F note: 5

Prerequisites

ConceptsImportanceNotes
Intro to MatplotlibNecessaryUsed to plot data
Intro to PandasNecessaryUsed to read in and organize data (in particular dataframes)
Intro to NumpyNecessaryUsed to work with large arrays
Intro to SciPyHelpfulUsed to work with .wav files and built-in Fast Fourier Transform
  • Time to learn: 30 minutes


Imports

Extract audio .wav file

As when working with the “Jingle Bells” song file, any .wav input file contains information about the amplitude at every point in the file. The frequency of the note will determine which chord each part of the piece represents.

Sample Rate: 10000
duration = 6.0 seconds
Total length in time steps = 60000

Let’s Give the Data a Look!

It is always good practice to view the data that we have collected. First, let’s organize the data into a pandas dataframe to organize the amplitude and time stamps.

Loading...

Plot a Small Sample of the .wav File

Plot a small subsample of the .wav to visualize the input data.

<Figure size 800x800 with 1 Axes>

Wavelet Analysis: Power Spectrum

The power spectrum plots the real component of the complex number returns from wavelet coefficients. This will return information about the frequency and time that we need to use to determine which notes are used in what order for the keypad.

For the purpose of this example, we will use the Morlet mother wavelet. Morlet is one type of mother wavelet useful for working with audio signals and is a good general wavelet to start with when analyzing frequencies of a signal.

However, choosing which wavelet to use is an important step as different wavelets will be sensitive to different features in time-series data.

To learn more!

size (39, 60000) with 39 scales and 60000 time steps
x-axis is: 60000
y-axis is: 39

Plot Scalogram

We will be plotting the wavelet as a scalogram, where the presence of a strong match to a specific frequency will be a darker color. This will create distinct bands of a dark color where a specific frequency is present.

<Figure size 800x800 with 2 Axes>

Behold! Distinct Bands of Frequencies!

Each distinct band represents a note. So, we can see that the data at the beginning and at the end is random noise, with no distinct frequency. But at 1 second, a distinct note that lasts for 1 second, followed by three additional distinct bands. We now know the code is four numbers long. But now we need to determine what the numbers are and what their order is. Let’s see where the possible note frequencies we have by overlaying the frequencies of each note onto the wavelet scalogram.

Important Note

To convert Hz frequency to a scale = hz *.0001 (where 0.01 is 100 Hz sampling) then apply frequency2scale PyWavelets function
<Figure size 800x800 with 1 Axes>

But Which Match Best?

We are looking for a note frequency that best lines up with the darkest part of each band. The first and the last band seem like the same note, but is it closer to an A note or a B note?

Let’s see if we can use Fourier Transform to get a smaller range of notes to chose from.

Fast Fourier Transform

<Figure size 800x800 with 1 Axes>

Perfect, There are Three Notes!

Three notes stand out, and one note is used about twice as much as the other two: A, B, F.

<Figure size 800x800 with 1 Axes>

Three Notes Played Over Six Seconds

We have the keypad solution! The three notes are played (sometimes repeated) over the course of the six seconds.

A, B, F, A

From our original problem, we know that the door code is set up as:

  • A note: 0

  • B note: 1

  • C note: 2

  • D note: 3

  • E note: 4

  • F note: 5

The solution to the door password is:

0, 1, 5, 0


Summary

Now we’ve had a chance to work with unknown input values, but within an expected range. Different time-series data will have different ranges of expected frequencies, and with Fourier Transform and wavelet analysis it is possible to pull out such relevant data.

What’s next?

Up next: apply wavelets transform and work with weather data!