A form of data that is often encountered in primary scientific research is various binary streams. There are specific codecs for video and audio transmission and storage, and often, we are looking for higher-level tools to deal with each specific format. There are various signal sources we might be considering such as from a radio telescopes, sensor on a camera, or the electrical impulses from a microphone. Signals all share the same underlying principles based on wave mechanics and harmonic motion.
Signals are generally studied using time frequency analysis. The central concept here is that a continuous signal in time and space can be decomposed into frequency components. We use what is known as a Fourier Transform to move between the time and frequency domains. This utilizes the interesting fact that states that any given function, including non periodic functions, can be represented by a series of sine and cosine functions. This is illustrated by the following:
To make this useful, we need to find the values for an and bn. We do this by multiplying both sides of the equation cosine, mx, and integrating. Here m is an integer.
This is called an orthogonal function, in a similar notion to how we consider x, y, and z to be orthogonal in a vector space. Now, if you can remember all your trigonometric functions, you will know that sine times cosine with integer coefficients is always zero between negative pi and pi. If we do the calculation, it turns out that the middle term on the left-hand side is zero, except when n equals m. In this case, the term equals pi. Knowing this, we can write the following:
So, in the first step, if we multiply by sin mx instead of cosine mx, then we can derive the value of bn.
We can see that we have decomposed a signal into a series of sine values and cosine values. This enables us to separate the frequency components of a signal.
One of the most common and easy to study signals is audio. We are going to use the soundfile
module. You can install it via pip
if you do not have it. The soundfile
module has a wavfile.read
class that returns the .wav
file data as a NumPy array. To try the following code, you will need a short 16 bit wave file called audioSamp.wav
. This can be downloaded from davejulian.net/mlbook. Save it in your data directory, in your working directory:
import soundfile as sf import matplotlib.pyplot as plt import numpy as np sig, samplerate = sf.read('data/audioSamp.wav') sig.shape
We see that the sound file is represented by a number of samples, each with two values. This is effectively the function as a vector, which describes the .wav
file. We can, of course, create slices of our sound file:
slice=sig[0:500,:]
Here, we slice the first 500 samples. Let's calculate the Fourier transform of the slice and plot it:
ft=np.abs(np.fft.fft(slice)) Finally lets plot the result plt.plot(ft) plt.plot(slice)
The output of the preceding commands is as follows: