1. Home
  2. Explore
  3. Computer Science
  4. Digital Audio Waveforms

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Digital Audio Waveforms

From Sound Waves to the Discrete Fourier Transform

This lesson follows the complete journey from a spoken sound, to digital audio samples, to simulated pressure waves, and finally to the frequency analysis performed by the discrete Fourier transform, or DFT.

The central idea is that sound can be viewed in two complementary ways:

  • In the time domain, sound is represented by how air pressure changes over time.
  • In the frequency domain, sound is represented as a combination of waves oscillating at different frequencies.

Understanding how to move between these two descriptions is one of the foundations of digital audio processing.

1. How sound becomes digital data

When a person speaks, several physical processes occur. The vocal folds vibrate, while the mouth and tongue shape the resulting motion of air. This creates changing pressure waves that travel through the air.

A microphone converts those pressure changes into an electrical signal. Inside a typical microphone, a thin membrane vibrates in response to the incoming air pressure. That motion can move a coil relative to a magnet, producing a voltage. The voltage varies in proportion to the motion caused by the sound.

The computer cannot directly store a continuously changing voltage. Instead, an analog-to-digital converter measures the voltage repeatedly at regular time intervals. Each measurement is called a sample.

If a signal is sampled at 48,00048{,}00048,000 samples per second, then the computer records 48,00048{,}00048,000 numerical measurements every second. These values are stored in an audio file such as a WAV file.

The original continuous sound is therefore approximated by a sequence:

x0,x1,x2,…,xN−1x_0, x_1, x_2, \ldots, x_{N-1}x0​,x1​,x2​,…,xN−1​

where each xix_ixi​ is the measured signal value at one instant.

Sample rate and duration

The sample rate describes how many samples are recorded per second. If an audio file contains NNN samples and has a sample rate of fsf_sfs​, its duration is

T=Nfs.T = \frac{N}{f_s}.T=fs​N​.

For example, if a file contains just under 40,00040{,}00040,000 samples recorded at 48,00048{,}00048,000 samples per second, then its duration is approximately

T≈40,00048,000≈0.8 seconds.T \approx \frac{40{,}000}{48{,}000} \approx 0.8 \text{ seconds}.T≈48,00040,000​≈0.8 seconds.

The sample rate determines the time spacing between samples:

Δt=1fs.\Delta t = \frac{1}{f_s}.Δt=fs​1​.

A higher sample rate means that samples are taken closer together in time.

A digital audio signal is a sequence of measurements of air pressure or voltage, taken at regular time intervals.

Visualizing samples

A simple way to visualize an audio signal is to plot:

  • Time along the horizontal axis.
  • Sample value, proportional to microphone voltage, along the vertical axis.

Each sample can be drawn as a point. The result reveals the individual measurements directly.

Another visualization draws a vertical line from the horizontal axis to each sample value. The height of the line represents the sample’s amplitude, meaning the strength or magnitude of the signal at that moment.

A waveform with large positive and negative values represents stronger pressure changes. A waveform close to zero represents weaker pressure changes.

Think About It

If an audio signal contains 192,000192{,}000192,000 samples and was recorded at 48,00048{,}00048,000 samples per second, what duration does it represent?

2. Understanding the WAV file format

A WAV file is organized into named sections, often called chunks. The file begins with identifying information that should indicate the RIFF and WAVE formats.

A program can first read the opening bytes and verify that they contain the expected identifiers. If they do not, the file is probably not a valid WAV file of the expected kind.

After the opening identifiers, the file contains chunks. Each chunk generally includes:

  1. A name identifying the chunk.
  2. A size indicating how many bytes belong to it.
  3. The chunk’s actual data.

Because the size is known, a program can skip over a chunk even if it does not need to understand its contents. This makes it possible to scan the file and build a lookup table such as:

  • Chunk name
  • Location where the chunk begins

The lookup table allows the program to jump directly to important chunks instead of reading the entire file sequentially every time.

Two especially important chunks are the format chunk and the data chunk.

The format chunk

The format chunk contains metadata describing how the audio samples should be interpreted. Important fields include:

  • Whether the audio is mono or stereo.
  • The sample rate.
  • The number of bits used for each sample.
  • The format of the stored sample data.

This information is essential. The same bytes could represent very different audio if they were interpreted using a different sample rate or bit depth.

The data chunk

The data chunk contains the actual audio samples. The samples may be stored using different numbers of bits, including formats such as:

  • 8-bit samples
  • 16-bit samples
  • 24-bit samples
  • Other supported sample widths

The program must read the correct number of bytes for each sample according to the format metadata.

Converting bytes into floating-point samples

Raw file data consists of bytes, but audio processing is easier when every sample is represented as a floating-point number in the range

−1≤x≤1.-1 \leq x \leq 1.−1≤x≤1.

The conversion process has several stages:

  1. Read the required number of bytes.
  2. Interpret those bytes as an integer using the correct byte order and signedness.
  3. Convert the integer to a floating-point value.
  4. Divide by the largest value representable with that many bytes.

The final division is a normalization step. It maps the integer sample range into the convenient range from −1-1−1 to 111.

For example, if a signed sample format can represent values from −M-M−M to MMM, then a normalized sample can be calculated approximately as

x=vM,x = \frac{v}{M},x=Mv​,

where vvv is the stored integer and xxx is the resulting floating-point value.

The special case of 24-bit audio

Many programming languages provide built-in integer types for 8-bit, 16-bit, and 32-bit values, but not always for 24-bit values.

A 24-bit sample uses exactly three bytes. Because there may be no built-in 24-bit type, the program must combine the bytes manually using bit manipulation.

The difficult part is handling negative values correctly. Signed integers usually use two’s-complement representation, in which the highest bit indicates whether the number is negative. A 24-bit negative value must therefore be sign-extended properly when it is placed into a larger integer type.

Once the integer has been reconstructed, it can be normalized like any other sample.

3. Playing digital audio

Loading a WAV file is only half of the process. To hear the signal, the program must send samples to the sound card at the appropriate rate.

An audio playback system repeatedly requests a block of empty sample data. The program fills that block with samples from the loaded audio signal.

The process is:

  1. The audio system requests a block of samples.
  2. The program copies the next samples from the WAV data into the block.
  3. The block is sent to the sound card.
  4. The sound card converts the numbers into an electrical signal.
  5. Speakers or headphones vibrate according to that signal.
  6. Those vibrations create new pressure waves in the air.

If the program has not reached the end of the file, it continues copying samples into successive blocks. The use of blocks is practical because the audio system processes small groups of samples repeatedly rather than requesting the entire file at once.

The timing must be correct. If samples are played too quickly, the sound becomes shorter and higher-pitched. If they are played too slowly, the sound becomes longer and lower-pitched.

4. Simulating sound as pressure waves

A fluid simulation can be adapted to behave approximately like air. In a fluid simulation, particles move according to rules involving density, pressure, gravity, viscosity, and obstacles.

For a sound-like simulation, several changes are useful:

  • Gravity should be disabled.
  • The target density should be reduced to better represent air.
  • Viscosity should be disabled or reduced, because excessive viscosity makes the simulated material behave more like thick liquid than air.

The simulation can visualize either individual particles or the density field. For sound waves, density is especially useful because density is closely related to pressure.

At a given position, density can be estimated as a smoothed sum of nearby particles. Conceptually,

ρ(p)≈∑iK(∥p−pi∥),\rho(\mathbf{p}) \approx \sum_i K\left(\left\|\mathbf{p}-\mathbf{p}_i\right\|\right),ρ(p)≈i∑​K(∥p−pi​∥),

where:

  • ρ(p)\rho(\mathbf{p})ρ(p) is the estimated density at position p\mathbf{p}p.
  • pi\mathbf{p}_ipi​ is the position of particle iii.
  • KKK is a smoothing function.
  • Nearby particles contribute more strongly than distant particles.

A visualization cell can be colored according to the density calculated at its position. Regions of high density represent compressed areas, while regions of low density represent rarefied areas.

Compressions and rarefactions

A sound wave consists of alternating regions:

  • A compression is a region where air molecules are more tightly packed.
  • A rarefaction is a region where air molecules are more spread out.

These alternating pressure regions travel through the air. The individual molecules generally oscillate locally, while the wave pattern carries energy outward.

Using a vibrating obstacle as a speaker

The simulation can contain a rectangular obstacle. This obstacle can act as a simplified speaker membrane.

The membrane is moved back and forth according to an input audio signal. At each simulation time, the program:

  1. Determines the current time.
  2. Looks up the corresponding audio sample.
  3. Uses the sample value to offset the membrane’s position.

The audio file contains discrete samples, but the membrane should move smoothly. To avoid abrupt jumps between samples, the program uses linear interpolation.

Suppose the signal has values xix_ixi​ and xi+1x_{i+1}xi+1​, and the desired position lies a fraction α\alphaα between them. Linear interpolation gives

x(t)=(1−α)xi+αxi+1,x(t) = (1-\alpha)x_i + \alpha x_{i+1},x(t)=(1−α)xi​+αxi+1​,

where 0≤α≤10 \leq \alpha \leq 10≤α≤1.

The membrane therefore follows a smooth approximation of the original waveform.

Measuring the simulated sound

A simulated microphone can estimate pressure at a chosen location by reading the fluid density there. A simple pressure estimate is

p=C(ρ−ρ0),p = C(\rho - \rho_0),p=C(ρ−ρ0​),

where:

  • ρ\rhoρ is the measured density.
  • ρ0\rho_0ρ0​ is the fluid’s rest or target density.
  • CCC is an arbitrary scaling constant.
  • ppp is the recorded pressure-like value.

Subtracting the rest density is important. The microphone should record changes from the normal background state, not the constant baseline density itself.

The resulting pressure values are stored as audio samples and can later be played back.

Why the initial simulation sounded silent

The original audio clip contained about 192,000192{,}000192,000 samples over four seconds. The simulation, however, completed only about 1,2001{,}2001,200 simulation steps during those four seconds.

That means the simulated recording contained only about 1,2001{,}2001,200 pressure measurements rather than 192,000192{,}000192,000. The simulation was therefore sampling the pressure far too infrequently.

When the simulation was slowed dramatically, it had more time to respond to the rapidly moving speaker membrane. The recorded pressure waveform began to resemble the input audio more closely.

The resulting audio was initially distorted and crackly because:

  • The fluid could not always react quickly enough.
  • The simulation used relatively few steps.
  • The fluid parameters were difficult to tune.
  • Numerical approximations introduced errors.

With an extremely slow simulation, the recorded sound became recognizable, although it still contained noise and imperfections.

A simulation must update quickly enough relative to the signal it is trying to reproduce; otherwise, important changes are skipped or distorted.

Think About It

Why would increasing the number of simulated particles make the simulation unstable if the other simulation settings remain unchanged?

5. Saving, reversing, slowing, and speeding audio

Once audio can be loaded and played, it can also be modified and written back to a new WAV file.

Reversing a signal

To reverse audio, the program simply reverses the order of the samples:

x0,x1,x2,…,xN−1x_0, x_1, x_2, \ldots, x_{N-1}x0​,x1​,x2​,…,xN−1​

becomes

xN−1,xN−2,…,x0.x_{N-1}, x_{N-2}, \ldots, x_0.xN−1​,xN−2​,…,x0​.

The reversed signal plays the original sound backward.

A backward recording may sound unfamiliar when reversed, but reversing it again restores the original ordering.

Changing playback speed

The playback speed can be changed by interpreting the samples as if they had been recorded at a different sample rate.

If the stored samples remain the same but the declared sample rate is reduced, the samples take longer to play. The result is:

  • Slower playback
  • Longer duration
  • Lower pitch

If the declared sample rate is increased, the result is:

  • Faster playback
  • Shorter duration
  • Higher pitch

This happens because changing the playback rate changes the frequency of every waveform component. If a waveform is compressed in time, it completes its oscillations more quickly, increasing its frequency. If it is stretched in time, its frequency decreases.

6. Constructing a pure sinusoidal signal

To understand frequency analysis, it helps to begin with simple signals rather than complex speech.

A pure sinusoidal signal can be generated from:

  • A frequency
  • A sample rate
  • A duration

First, the program calculates the number of samples:

N=fsT.N = f_sT.N=fs​T.

For each sample, it computes the corresponding time. If the sample index is nnn, then

tn=nfs.t_n = \frac{n}{f_s}.tn​=fs​n​.

The sample value is then calculated using a sine or cosine function:

x(tn)=cos⁡(2πftn).x(t_n) = \cos(2\pi f t_n).x(tn​)=cos(2πftn​).

The same signal can be written using τ\tauτ, where τ=2π\tau = 2\piτ=2π:

x(tn)=cos⁡(τftn).x(t_n) = \cos(\tau f t_n).x(tn​)=cos(τftn​).

Why the factor 2π2\pi2π appears

Sine and cosine can be understood using a point moving around a unit circle.

The point’s horizontal and vertical coordinates are given by cosine and sine. One complete trip around the circle corresponds to an angle of 2π2\pi2π radians.

A radian measures an angle by comparing an arc length to the circle’s radius. One full revolution is therefore

2π radians.2\pi \text{ radians}.2π radians.

If the signal frequency is fff hertz, the point should complete fff full revolutions every second. At time ttt, the angle is therefore

θ=2πft.\theta = 2\pi ft.θ=2πft.

The signal value is the point’s vertical or horizontal coordinate:

x(t)=sin⁡(θ)x(t) = \sin(\theta)x(t)=sin(θ)

or

x(t)=cos⁡(θ).x(t) = \cos(\theta).x(t)=cos(θ).

The frequency determines how quickly the angle increases and therefore how many oscillations occur per second.

A signal with frequency 111 Hz completes one oscillation each second. Over four seconds, it completes four oscillations.

7. Amplitude, phase, and combining waves

A more general sinusoidal component can be written as

x(t)=Acos⁡(2πft+ϕ),x(t) = A\cos(2\pi ft+\phi),x(t)=Acos(2πft+ϕ),

where:

  • AAA is the amplitude.
  • fff is the frequency.
  • ϕ\phiϕ is the phase.

Amplitude

The amplitude controls the size of the oscillation. Increasing AAA makes the waveform taller and generally makes the sound stronger or louder.

If AAA is reduced, the waveform shrinks toward zero.

Phase

The phase ϕ\phiϕ shifts the wave horizontally. It determines where in its cycle the wave begins.

Two waves can have the same frequency and amplitude but different phases. They will have the same shape, but one will be shifted relative to the other.

Superposition

A complex sound can be modeled as a sum of simpler sinusoidal waves:

x(t)=∑k=1KAkcos⁡(2πfkt+ϕk).x(t) = \sum_{k=1}^{K} A_k\cos(2\pi f_kt+\phi_k).x(t)=k=1∑K​Ak​cos(2πfk​t+ϕk​).

Each component contributes according to its own:

  • Frequency fkf_kfk​
  • Amplitude AkA_kAk​
  • Phase ϕk\phi_kϕk​

The final waveform is created by adding their values at each time.

This is the principle of superposition. It explains how a complicated sound can be built from many simple oscillations.

For example, a signal containing components at 150150150 Hz, 214214214 Hz, and 357357357 Hz can sound more complex than any one of the components by itself.

8. The geometric intuition behind the DFT

The DFT attempts to reverse the process of synthesis. Instead of starting with known frequencies and adding them together, it begins with a sampled signal and estimates which frequencies, amplitudes, and phases are present.

A useful visualization treats the samples geometrically.

For a chosen test frequency fff, each sample is placed on a rotating coordinate system. For sample index nnn, the test angle can be written as

θn=2πftn.\theta_n = 2\pi f t_n.θn​=2πftn​.

The rotating test point is

rn=[cos⁡(θn)sin⁡(θn)].\mathbf{r}_n = \begin{bmatrix} \cos(\theta_n)\\ \sin(\theta_n) \end{bmatrix}.rn​=[cos(θn​)sin(θn​)​].

The signal sample scales this point:

sn=xnrn.\mathbf{s}_n = x_n\mathbf{r}_n.sn​=xn​rn​.

The program then calculates the average location of all these sample points:

c(f)=1N∑n=0N−1sn.\mathbf{c}(f) = \frac{1}{N}\sum_{n=0}^{N-1}\mathbf{s}_n.c(f)=N1​n=0∑N−1​sn​.

This average is called the sample center in the visualization.

What happens when the test frequency matches?

Suppose the input contains a sinusoid at the same frequency as the test frequency.

As the test point rotates, the sample values scale it in a coordinated way. The sample points form a small circle or cluster offset from the origin. Their average is therefore displaced from zero.

The distance from the origin indicates the strength of the matching frequency. The direction of the displacement indicates the phase.

In simplified terms:

  • The distance of the sample center relates to amplitude.
  • The angle of the sample center relates to phase.
  • A center near the origin suggests that the tested frequency is absent or cancels out.

Because the center often represents roughly half the amplitude, the amplitude calculation generally requires multiplying the measured distance by 222.

What happens when the test frequency does not match?

If the test frequency does not match a component in the signal, the sample points tend to spread symmetrically around the origin. Positive and negative contributions cancel, giving an average near zero.

For a signal containing frequencies 333, 555, and 999 Hz:

  • Testing 333 Hz produces a displaced sample center.
  • Testing 555 Hz produces a displaced sample center.
  • Testing 999 Hz produces a displaced sample center.
  • Testing unrelated frequencies generally produces a center near zero.

Other frequencies can alter the individual sample-point positions without changing their average significantly, because their contributions cancel over the observation interval.

Extracting amplitude and phase

If the sample center has coordinates

c=(cx,cy),\mathbf{c} = (c_x,c_y),c=(cx​,cy​),

then its distance from the origin is

r=cx2+cy2.r = \sqrt{c_x^2+c_y^2}.r=cx2​+cy2​​.

The corresponding phase can be obtained from the angle

ϕ=atan2⁡(cy,cx).\phi = \operatorname{atan2}(c_y,c_x).ϕ=atan2(cy​,cx​).

The amplitude is proportional to the distance. In the described visualization, the actual amplitude is approximately

A=2r,A = 2r,A=2r,

except for special cases discussed later.

9. Frequency spectra

Instead of testing one frequency at a time visually, the program can calculate the sample center for many test frequencies and record the corresponding amplitudes.

Plotting amplitude against test frequency produces a frequency spectrum.

A strong peak indicates that the signal contains a strong component at that frequency. For a signal composed of frequencies 333, 555, and 999 Hz, the spectrum should contain large peaks at those values.

The spectrum can also contain smaller bumps between the actual frequencies. These may occur because the observation interval does not contain an exact integer number of cycles for the tested frequency.

Spectral leakage

When a signal is observed for a finite duration, the sampled section may not begin and end at matching points in its cycle. The test pattern then fails to line up perfectly.

Energy that should appear at one frequency spreads into nearby frequencies. This effect is called spectral leakage.

For example, a signal at a fractional frequency such as 7.57.57.5 Hz may produce energy not only at 7.57.57.5 Hz but also in surrounding frequency bins.

Spectral leakage becomes especially noticeable when:

  • The signal duration is short.
  • The signal frequency does not align with the available frequency resolution.
  • The recording begins or ends partway through a cycle.

Increasing the sample rate does not necessarily fix this problem. A higher sample rate gives more samples per second, but the frequency resolution is mainly determined by the total recording duration.

10. The Nyquist-Shannon sampling theorem

The DFT can only analyze frequencies that were preserved during sampling. The key relationship is described by the Nyquist-Shannon sampling theorem.

If the highest frequency in a signal is fmax⁡f_{\max}fmax​, the sample rate must be greater than twice that frequency:

fs>2fmax⁡.f_s > 2f_{\max}.fs​>2fmax​.

The value

2fmax⁡2f_{\max}2fmax​

is called the Nyquist rate.

Aliasing

If the sample rate is too low, the samples can imitate a different, lower-frequency signal. This false frequency is called an alias.

Imagine a pure 808080 Hz signal. If it is sampled at only 808080 samples per second, the samples may repeatedly land at similar points in the waveform and create an apparently flat or misleading signal.

Sampling at 818181 samples per second may produce a sequence that appears to oscillate at a much lower frequency. The samples have effectively assumed a false identity.

This happens because the sampling intervals are so far apart that the samples skip over important peaks and valleys.

Why twice the frequency is required

To distinguish a waveform’s oscillation, the sampling process needs enough measurements to represent its movement. A rate of at least twice the highest frequency is the theoretical minimum.

However, sampling exactly at the Nyquist rate can be risky. Depending on the phase of the signal, samples may still land in an unhelpful pattern, such as repeatedly landing near zero crossings.

In practice, the sample rate is chosen above the theoretical minimum.

Audio sample rates

Human hearing extends to roughly 202020 kHz. To preserve frequencies up to that range, the sample rate needs to be at least approximately

2×20 kHz=40 kHz.2 \times 20\text{ kHz} = 40\text{ kHz}.2×20 kHz=40 kHz.

Real-world audio systems commonly use rates near 44.144.144.1 kHz or 484848 kHz.

The extra margin is useful because frequencies above the desired range can otherwise alias into lower frequencies. Filters are used before sampling to remove excessively high frequencies, but real filters are not perfectly sharp, so additional room is helpful.

An important consequence is that the highest frequency worth testing does not need to be hardcoded as the limit of human hearing. It is determined by the sample rate:

fmax⁡,detectable≈fs2.f_{\max,\text{detectable}} \approx \frac{f_s}{2}.fmax,detectable​≈2fs​​.

For example, a signal sampled at 484848 kHz has a theoretical upper frequency limit near 242424 kHz.

The sample rate determines the highest frequency that can be represented without aliasing.

Think About It

Why does increasing the sample rate expand the range of detectable frequencies, while increasing the recording duration improves frequency resolution?

11. Frequency resolution

The DFT does not test every possible real-valued frequency. It tests a set of regularly spaced frequency bins.

If the recording lasts TTT seconds, the frequency spacing is

Δf=1T.\Delta f = \frac{1}{T}.Δf=T1​.

Equivalently, if there are NNN samples at sample rate fsf_sfs​, then

T=NfsT = \frac{N}{f_s}T=fs​N​

and therefore

Δf=fsN.\Delta f = \frac{f_s}{N}.Δf=Nfs​​.

These two forms describe the same relationship.

Why duration matters

Suppose a 1.51.51.5 Hz signal is recorded for one second. It completes

1.5×1=1.51.5 \times 1 = 1.51.5×1=1.5

cycles, which is not an integer number of cycles. The beginning and end do not line up neatly.

If the same signal is recorded for two seconds, it completes

1.5×2=31.5 \times 2 = 31.5×2=3

complete cycles. It now aligns with the observation interval and can be detected cleanly.

A ten-second recording has frequency spacing

Δf=110=0.1 Hz.\Delta f = \frac{1}{10} = 0.1 \text{ Hz}.Δf=101​=0.1 Hz.

It can therefore test frequencies such as 1.11.11.1, 1.21.21.2, 1.31.31.3, and so on through the frequency bins.

Longer recordings provide finer frequency resolution, while higher sample rates provide more samples and a higher maximum detectable frequency.

12. Implementing a basic DFT

A practical DFT implementation can proceed as follows.

Step 1: Determine the number of frequency bins

Only frequencies up to approximately fs/2f_s/2fs​/2 need to be considered. Since the frequency spacing is fs/Nf_s/Nfs​/N, the number of bins is approximately

fs/2fs/N=N2.\frac{f_s/2}{f_s/N} = \frac{N}{2}.fs​/Nfs​/2​=2N​.

Thus, the spectrum can contain about N/2N/2N/2 useful frequency values.

Step 2: Determine the frequency spacing

The spacing between bins is

Δf=1T\Delta f = \frac{1}{T}Δf=T1​

or equivalently

Δf=fsN.\Delta f = \frac{f_s}{N}.Δf=Nfs​​.

The kkkth frequency bin is

fk=kΔf.f_k = k\Delta f.fk​=kΔf.

Step 3: Test each frequency

For each frequency bin, the program calculates the rotating test point for every sample and scales it by the sample value.

The center is calculated as

c(fk)=1N∑n=0N−1xn[cos⁡(2πfktn)sin⁡(2πfktn)].\mathbf{c}(f_k) = \frac{1}{N} \sum_{n=0}^{N-1} x_n \begin{bmatrix} \cos(2\pi f_k t_n)\\ \sin(2\pi f_k t_n) \end{bmatrix}.c(fk​)=N1​n=0∑N−1​xn​[cos(2πfk​tn​)sin(2πfk​tn​)​].

Step 4: Extract amplitude and phase

The distance of the center from the origin gives a measure of amplitude:

rk=∥c(fk)∥.r_k = \left\|\mathbf{c}(f_k)\right\|.rk​=∥c(fk​)∥.

The phase is obtained from the center’s angle:

ϕk=atan2⁡(ck,y,ck,x).\phi_k = \operatorname{atan2}(c_{k,y},c_{k,x}).ϕk​=atan2(ck,y​,ck,x​).

The amplitude is generally scaled by 222:

Ak=2rk.A_k = 2r_k.Ak​=2rk​.

Step 5: Store the spectrum

Each frequency bin can be stored as a structure containing:

  • Frequency
  • Amplitude
  • Phase

The resulting collection is the signal’s frequency representation.

13. Special amplitude cases

The factor of 222 is needed in the general case because the positive and negative frequency contributions share the measured energy.

There are two special cases where doubling is not appropriate:

  1. Zero frequency
  2. Half the sample rate

Zero frequency

A zero-frequency component does not oscillate. It is simply a constant vertical shift in the signal.

When the test frequency is zero, every rotating test point points in the same direction. The sample points pile on top of one another rather than forming the usual offset circle. The amplitude should therefore not be doubled.

Half the sample rate

The same adjustment is needed for the component at exactly

fs2.\frac{f_s}{2}.2fs​​.

This is the highest frequency represented by the sampled data and has special symmetry in the frequency representation.

14. Reconstructing a signal from its spectrum

Once the DFT has produced frequency, amplitude, and phase data, the original signal can be reconstructed by adding the sinusoidal components back together:

x^(t)=∑kAkcos⁡(2πfkt+ϕk).\hat{x}(t) = \sum_{k} A_k\cos(2\pi f_kt+\phi_k).x^(t)=k∑​Ak​cos(2πfk​t+ϕk​).

Here, x^(t)\hat{x}(t)x^(t) is the reconstructed approximation.

For a simple synthetic signal whose highest frequency is 171717 Hz, a sample rate just above twice that value can be sufficient. A sample rate of 353535 samples per second satisfies the requirement

35>2×17.35 > 2 \times 17.35>2×17.

The reconstructed signal should match the original signal when the sampling and DFT calculations are handled correctly.

For real speech, the spectrum contains many components. Low-amplitude components may contribute little to the overall sound, while a sufficiently large group of components can recreate recognizable speech.

15. Keeping only the strongest frequencies

The frequency data can be sorted by amplitude. The program can then reconstruct the audio using only the strongest components.

The effect changes as more frequencies are included:

  • Top 5 frequencies: Mostly a low hum.
  • Top 50 frequencies: A more complex sound, but speech is still unclear.
  • Top 250 frequencies: Some speech-like qualities begin to emerge.
  • Top 750 frequencies: Speech becomes more recognizable.
  • Top 1,500 frequencies: The spoken words become clearer.
  • Top 5,000 frequencies: The speech is very clear, though some hum or hiss may remain.
  • A very large number of frequencies: The reconstruction becomes effectively indistinguishable from the original.

This demonstrates that a sound can often be approximated using a subset of its frequency components. The most influential frequencies carry much of the recognizable information, while weaker components contribute fine detail and texture.

This idea is useful for:

  • Compression
  • Noise reduction
  • Sound filtering
  • Feature extraction
  • Audio visualization
  • Signal analysis

16. Why the basic DFT is slow

The direct DFT tests every frequency bin against every input sample. If there are NNN samples and roughly N/2N/2N/2 frequency bins, the amount of work grows approximately as

O(N2).O(N^2).O(N2).

That becomes very slow for long audio recordings.

The fast Fourier transform, or FFT, is an efficient algorithm for computing the same Fourier information. It reorganizes the calculations so that many repeated operations are avoided.

The FFT does not produce a fundamentally different type of result from the DFT. It is a much faster method for calculating the transform.

Comprehensive review

Digital sound begins as changing air pressure. A microphone converts those pressure changes into voltage, and an analog-to-digital converter records the voltage as regularly spaced numerical samples.

A WAV file stores those samples along with metadata such as the sample rate, channel configuration, bit depth, and data format. Reading the file requires identifying its chunks, interpreting the sample bytes correctly, and normalizing the values into a convenient range such as −1-1−1 to 111.

Playback reverses the process. Numerical samples are sent to a sound card, which drives speakers or headphones to recreate pressure variations in the air.

Sound can also be represented visually or simulated physically. In a fluid simulation, density changes correspond approximately to pressure changes. A vibrating obstacle can act as a simplified speaker, while a density measurement can act as a simplified microphone. However, the simulation must update frequently enough to capture the input signal accurately.

A sinusoidal wave is described by its frequency, amplitude, and phase:

x(t)=Acos⁡(2πft+ϕ).x(t)=A\cos(2\pi ft+\phi).x(t)=Acos(2πft+ϕ).

Complex sounds can be created by adding many such waves together. The DFT reverses this process by examining a sampled signal and determining which frequencies, amplitudes, and phases contributed to it.

The geometric interpretation is especially useful: when the test frequency matches a signal component, the rotated sample points produce a nonzero average center. The center’s distance reveals the component’s strength, and its angle reveals the phase. When frequencies do not match, their contributions tend to cancel.

The sample rate controls the highest frequency that can be represented:

fmax⁡≈fs2.f_{\max}\approx\frac{f_s}{2}.fmax​≈2fs​​.

The recording duration controls the frequency resolution:

Δf=1T=fsN.\Delta f=\frac{1}{T}=\frac{f_s}{N}.Δf=T1​=Nfs​​.

If a signal does not complete an integer number of cycles during the recording, its energy can spread across neighboring frequencies, producing spectral leakage.

Finally, the extracted frequency components can be added together to reconstruct the original signal. A few strong components may produce only a rough approximation, while thousands of components can reproduce speech with impressive accuracy. The direct DFT is conceptually straightforward but computationally expensive, which is why the faster FFT is used for practical audio analysis.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent