Digital Signals Part II
Sampling
In the previous module we talked about physical properties of sound and how laws of acoustics are realized through vibrations of strings or air columns in different musical instruments, in human voice and more. The first step of getting a physical sound into the computer is translating air waves into electrical signals. In order to use a computer to synthesize or edit sound, we need to digitize sounds, or in other words to represent continuous waveforms as as series of numbers. This is called sampling and it has specific rule to it - for instance, how often or at what rate should we sample the signal? And how many bits are needed to represent effectively the sound?
The first step in creating a digital audio signal is to convert or capture the numerical representation from a continuous signal, such as an electric signal that comes from a microphone. We call this continuous signal analog, to distinguish it from numeric or digital. So the first step in digital audio system is a converter commonly called A/D or Analog to Digital converter (ADC). In Pd these are the adc~ and dac~ patches. The simplest way to think about it is so called sample-and-hold method. The sample-and-hold circuit captures, or samples, the instantaneous voltage of an analog audio signal and holds its value until the A/D converts it into a binary number. This process is repeated periodically at a specified sampling rate.
- AUDACITY
- Import an existing music file into Audacity (File -> Import -> Audio)
- Identify the sampling rate
- Use Tracks -> Resample to 8000 Hz and play sound
- Do you hear any difference?
- PYTHON
- Use numpy to create tones at 440Hz and 446Hz
- Create wav files sampled at 8000Hz and 16000Hz
- Combine the tones by summing their samples and use scipy to save it to a mono wav file
- Combine the tones by placing one tone on the left and the other tone on the right channel of a stereo wav file
- Listen to the files in Audacity
Quantization
When we digitize sounds we represent continuous waveforms as as series of numbers. How many bits are needed to effectively represent the sound?
Binary number are just like any number - a way to count. The only difference from our usual numeric system is that we are used to think in the base of ten, i.e we started counting with our fingers and when we run out of fingers on both hands we remember that we reached 10, or 20, or 30 and so on, and keep counting. Computer do the same but they have only two fingers labeled 0 and 1. So whenever they run out of these two fingers they move on to the next place in the count list. For example, with 10 fingers we express numbers from 0 to 9, the so called decimal representation, and with three decimal digits (digitus actually mean finger in Latin), we can count from 0 to 999. Similarly, with two bits we express four numbers 00, 01, 10, and 11, and with three bits we express eight numbers from 000 to 111.
Applying this to sound, if we use eight bits to represent the level of a sound waveform at each sample, we have a choice of <math>2^8 = 256</math> numbers to represent the continuous signal. With sixteen bits we can express <math>2^{16} = 65536</math> different levels. The number of bits per sample, also know as bit depth, has an immediate effect on the precision with which the waveform can be captured in a digital format, regardless of the rate of sampling of the waveform.
- AUDACITY
- Export the music file you imported as 8-bit-unsigned. File -> Export Audio and select 'other uncompressed files' in the format field and hit the options button. You will be guided to a menu. Choose 'WAV (microsoft)' as the file type, and then 8-bit-unsigned
- Import the file back into Audacity
- Do you hear any difference?
- PYTHON
- Use numpy to create a tone at 440Hz sampled at 8000Hz
- Quantize the signal using 2 bits then 16 bits
- Use matplotlib to visualize the waveforms
- PYTHON
- Use scipy to import a music file into a numpy array
- Quantize the song using 4 bits and save the result as a wav file
- Use Audacity to listen to the result
- Can you hear the effect of quantization on the orchestration? Can you hear the effect of quantization on the human voice?
Aliasing
A seen before, sampling a continuous (analog) signal into a sequence of digital numbers involves sampling periodically at the sampling rate. The more often the samples are taken, the more accurately the signal can be represented. More specifically, the higher the frequency of the sound that can be captured. The law that governs the relation between the sampling rate and the frequency content of the signal that can be reliably captured in the digital (sampled) version of the signal is called Sampling Theorem, also commonly known as Nyquist law in honor of the engineer who discovered it. Even though the mathematical details of the sampling theory are beyond the scope of this lecture, the intuition behind it is rather simple.
In order to reliably capture the oscillatory behavior of a sinusoidal waveform requires at least two samples for every period. Otherwise the resulting samples would not be oscillatory. The sampling frequency that produces exactly two samples of the input signal is the critical sampling frequency which in this simple case is equal to twice the sinusoidal frequency. We have seen before that it is possible to decompose a complex signal as the sums of its sinusoidal components using Fourier analysis. Any input signal in which all sinusoidal components have frequencies lower than half the sampling frequency will have its components sampled twice or more per period. A remarkable mathematical result is that such input signal can be reconstructed exactly from its samples! Half the sampling frequency is known as the Nyquist frequency.
On the other hand, if an input signal contains a sinusoidal component with frequency higher than the sampling frequency then it cannot be unambiguously reconstructed from its samples. The reason for this is that the samples of a sinusoidal waveform with frequency higher than the sampling frequency can not be distinguished from the samples of another sinusoidal waveform with frequency below the Nyquist frequency. If one attempts to reconstruct this signal the high frequency component will appear to have the frequency of its alias below the Nyquist frequency. For this reason this phenomenon is known as aliasing. You will show in the activities that any frequency <math>f</math> below the Nyquist frequency has aliases at <math>f + k f_s</math> where <math>k</math> is any positive integer. For instance, if the sampling rate is <math>f_s = 4</math>Hz then <math>f = 1</math>Hz has as aliases the frequencies 5Hz, 9Hz, 13Hz, etc. Also, because <math>\cos(2 \pi f t + \phi) = \cos(2 \pi (-f) t - \phi)</math> the frequencies -1+4=3Hz, 7Hz, 11Hz, etc, are also aliases.
The practical implications of the rules of sampling are as follows:
- In order to avoid aliasing the input frequency has to be less then Nyquist frequency, or in other words, the sampling frequency should be twice higher then the highest frequency present in the input signal. If this rule is violated then aliasing occurs. This effectively causes reconstruction of a frequency that is lower then the input frequency at a frequency that is an alias of the input frequency below the Nyquist frequency.
Aliasing occurs whenever sampling is involved, not only in audio signals. For instance, in movies, aliasing is responsible to the infamous wagon wheel illusion in which the wheel appears to rotate in the same direction as the direction of movement of the wagon or not rotate at all. Check out this video. In what frequency will it appear not to rotate?
- AUDACITY
- Open up Audacity and set the project sampling rate to <math>f_s = 8000</math>Hz. The highest frequency that can be digitally represented is the Nyquist frequency <math>f_N = 4000</math>Hz.
- Generate a chirp (Generate->Chirp...) that is a sine tone. Set the start frequency at 0Hz and the end frequency at 4000Hz.
- Now generate the same chirp, but using a sawtooth wave.
- Explain why the sinusoidal chirp sounds like a sine tone sweeping up from 0Hz to 4000Hz but the sawtooth chirp does not sound as smooth (Hint: remember that the sawtooth is rich in overtones and think about the Nyquist theorem)
- Depending on your hardware, you may have also noticed some thing unusual when the frequency gets close to 4000Hz. Can you explain it?
- PYTHON
- Use numpy to create tones at 1Hz and 5Hz sampled at 1000Hz and 4Hz
- Compare the samples in the two cases
- Repeat for a sawtooth waveform at 2Hz
- AUDACITY
- Open up Audacity and set the project sampling rate to <math>f_s = 8000</math>Hz.
- Generate a 1000Hz sawtooth tone (Generate->Tone...) of amplitude 1.
- Generate a 1000Hz sinusoidal tone of amplitude 0.64.
- Generate a 2000Hz sinusoidal tone of amplitude 0.32.
- Generate a 3000Hz sinusoidal tone of amplitude 0.21.
- Mute the 1000Hz sawtooth tone and play the other three sinusoidal tones together.
- Mute all sinusoidal tones and play the 1000Hz sawtooth tone.
- Explain what you heard in terms of aliasing of signals rich in overtones.
Signal Reconstruction
If aliasing does not occur then it is possible to exactly reconstruct a continuous signal from its samples. A somewhat complicated mathematical formula that implements a special low-pass filter is responsible for this feat. Every digital audio processor implements a version of this formula when the signals are converted into analog signals, for example, when used to excite a speaker. Because frequencies higher than the Nyquist frequency are aliasing during sampling, it is natural to expect that the reconstructed signal will have only components with frequency smaller than the Nyquist frequency.
- PYTHON
- Check out the SignalReconstruction notebook.
Phase and delay
Show how delay can cause phase cancellation of a specific frequency. Demonstrate a simple Kareoke effect by inverting and summing left and right channels. Explain how using multiple delays we can create "inverted phase" at multiple frequencies. But this is not ideal. We also affect other nearby frequencies.
If we assume the speed of sound to 344m/s, A 55Hz sinusoid has a period of {6.25}meters. (Round to the nearest hundredth's place) Half of this wavelength is {3.13}m (again, round to nearest hundredth's place). Verify that two mics placed at this distance will result in phase cancellation by plugging this value into the provided Pd patch, phase_cancellation. Remember that you can enter values into number boxes by clicking on them, typing the number, and pressing enter.
- delay
- Windowing: Size and shape
Using audacity, generate a 440Hz sawtooth wave (Generate->Tone...). Once the spectrum pops up, set the Algorithm to 'Spectrum', the Function (this is the windowing function) to 'Rectangular' the Size (this is the length of the window) to '128' and the axis to 'Log frequency' (this let's us look at low frequency components more easily).
Now we expect that a the magnitude spectrum of a 440Hz sawtooth wave will have a strong peak at 440Hz, along with a series of peaks that taper at whole number multiples of 440Hz (the overtones of the tone). Is this what you see? Increase the size of the window until a distinct peak is visible at 440Hz. What is the window size here?
Switch the Function to 'Hanning' and repeat the process of raising the window size. What is the difference in the magnitude spectrum between the two windowing methods?
Optional Advanced Material
Explain Forward and Recursive filters.
Visit the website http://www.earlevel.com/main/2003/02/27/pole-zero-placement/ to get your hands on an interactive two-pole, two-zero filter. (we call this a bi-quadratic filter because both the numerator and denominator of the transfer function are quadratic -- second order -- polynomials). The graph below the pole-zero plot shows the magnitude spectrum of the filter's frequency response. Each vertical bar represents frequencies at 1/8 of the nyquist rate.
First, design a filter that has a band pass at 5512.5 (1/8 of the Nyquist) that rejects as much as possible the DC (0 frequency component). To do this, position the two poles so that the peak of the frequency response lies directly on the first vertical line on the graph. Then, manipulate the zeros in such a way that there is a steep fall off and the 0th frequency component is below 0 in amplitude. You can use the pd patch 08-biquad.pd to listen to and see what effect this filter has on noise. Simply copy the correct coefficients into the appropriate places in the patch.
Report the filter coefficients that you found for your filter.
Next, answer the following questions: 1. Where should the zeros be placed if you want to design a low-pass filter? a high-pass filter? a symmetric resonance? 2. What type of filter results from a design that has zeros on the unit circle and poles at the same angle as the zeros? 3. Suggest a way to design a more sophisticated filter by cascading biquads. If you have two biquads with frequency responses H1 and H2, what would be the resulting combined response after cascading?