ENع
→Digital audio
الدرس S1 · Special topics

Digital audio: 44.1 kHz, equalisers and spectrograms

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

18 دقيقة10 خطوات5 أسئلةAdvanced
عن هذا الدرس

A stand-alone deep dive into how sound lives in a computer. Follow one short recording, three notes with an annoying 3 kHz whine, from the microphone to 44,100 sixteen-bit samples a second, see it as a spectrogram built from Hann-windowed FFT frames, then remove the whine with one equaliser band, a biquad filter, and do it all in Python with scipy. It builds on the lessons on sampling, quantization, the DFT and FFT, windowing and IIR filters; this lesson links to them rather than teaching them again.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل S1 · تفاعلي
01 / 10

Why 44.1 kHz

We hear up to about 20 kHz. The sampling theorem says: sample faster than twice the highest frequency. At 44,100 samples a second the limit, fs/2, is 22.05 kHz, leaving 2.05 kHz for the anti-alias filter to roll off.

hearing        about 20 Hz .. 20 kHz
sampling rule  fs > 2 · fmax  ->  fs > 40 kHz

CD rate        fs   = 44100 samples/s
Nyquist        fs/2 = 22050 Hz
guard band     22050 − 20000 = 2050 Hz

the analog low-pass (anti-alias) filter must
pass 20 kHz and block everything above 22.05 kHz
(see the lessons on sampling and anti-aliasing)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Why 44.1 kHz

    We hear up to about 20 kHz. The sampling theorem says: sample faster than twice the highest frequency. At 44,100 samples a second the limit, fs/2, is 22.05 kHz, leaving 2.05 kHz for the anti-alias filter to roll off.

    hearing        about 20 Hz .. 20 kHz
    sampling rule  fs > 2 · fmax  ->  fs > 40 kHz
    
    CD rate        fs   = 44100 samples/s
    Nyquist        fs/2 = 22050 Hz
    guard band     22050 − 20000 = 2050 Hz
    
    the analog low-pass (anti-alias) filter must
    pass 20 kHz and block everything above 22.05 kHz
    (see the lessons on sampling and anti-aliasing)
  2. 16 bits per sample

    Each sample is stored as a 16-bit integer from −32768 to +32767: 65,536 levels. A full-scale sine then sits about 98.1 dB above the rounding noise. Two channels cost 1,411,200 bits every second.

    q[n] = round(32767 · x[n])   (int16)
     q[0..4] = 0, 1706, 3285, 4634, 5688
    
    levels      2^16 = 65,536
    range       6.02 · 16 + 1.76 = 98.1 dB
                (full-scale sine vs rounding noise)
    bit rate    44100 × 16 bits × 2 channels
                = 1,411,200 bit/s = 1411.2 kbit/s
    
    the samples arrive every 1/44100 s = 22.7 µs
  3. Cut the sound into frames

    Our recording: notes A4, C#5 and E5, a quarter second each, plus a faint 3 kHz whine. We cut it into frames of 1024 samples (23.2 ms), each shaped by a Hann window, starting every 512 samples.

    x[n] = 0.5·sin(note) + 0.05·sin(2π·3000·n/fs)
    notes  A4 440 Hz, C#5 554.37 Hz, E5 659.26 Hz
           (equal temperament: 440 · 2^(k/12))
    whine  3000 Hz, 20 dB quieter than a note
    
    frame  N = 1024 samples = 23.2 ms
    hop    512 samples = 11.6 ms (50 % overlap)
    Hann   w[n] = 0.5 − 0.5·cos(2πn/N)
           fades each frame in and out
    0.75 s -> 63 frames
  4. One frame, one spectrum

    The FFT turns frame 10 into 513 frequency bins, 43.1 Hz apart (fs/N). Two peaks stand out: the 440 Hz note at −6.3 dBFS and the 3 kHz whine at −26.7 dBFS.

    frame 10: samples 5120 .. 6143  (116 .. 139 ms)
    
    X[k] = Σ w[n]·x[n]·e^(−j2πkn/N),  k = 0 .. 512
    bin spacing  fs / N = 44100 / 1024 = 43.1 Hz
    
    level  20·log10(|X[k]| / (N/4))  dBFS
           (a full-scale sine reads 0 dBFS)
    
    note   bin 10 (431 Hz)   −6.3 dBFS
    whine  bin 70 (3015 Hz)  −26.7 dBFS
    0.5 -> −6.0 dB, 0.05 -> −26.0 dB, less the
    loss when a tone falls between bins
  5. Stack the spectra: a spectrogram

    Do that for all 63 frames and stand the spectra side by side: time across, frequency up, loudness as colour. The three notes climb like stairs; the whine is a thin line at 3 kHz all the way through.

    spectrogram = |STFT|², drawn in dB
      column m = spectrum of frame m
      63 columns × 513 bins (0 .. 4 kHz shown)
    
    colour steps of 12 dB:
      ≥ −12, −24, −36, −48, −60, −72 dBFS
      quieter cells are left blank
    
    a frame that straddles two notes
    shows both of them
  6. Time or frequency: pick one

    A frame of N samples lasts N/fs and its bins are fs/N apart: the product is always 1. N = 1024 gives 23.2 ms and 43.1 Hz; N = 4096 sharpens bins to 10.8 Hz but blurs time to 92.9 ms.

    duration × bin spacing = (N/fs) × (fs/N) = 1
    
    N       duration    bin spacing
    512     11.6 ms     86.1 Hz
    1024    23.2 ms     43.1 Hz
    4096    92.9 ms     10.8 Hz
    
    long frames: sharp pitch, smeared onsets
    short frames: sharp onsets, smeared pitch
  7. An equaliser band

    An equaliser turns some frequencies up or down. A parametric band has three knobs: centre frequency, gain and width (Q). We set one band to 3 kHz, −18 dB, Q = 4: a narrow dip right on the whine.

    peaking band (Audio EQ Cookbook, R. Bristow-Johnson)
      f0 = 3000 Hz,  gain = −18 dB,  Q = 4
      A  = 10^(gain/40),  w0 = 2π·f0/fs
      α  = sin(w0) / (2Q)
    
    |H| at 3000 Hz   −18.00 dB
    |H| at the notes: never below −0.11 dB
    |H| at 1 kHz     −0.28 dB
    
    a graphic EQ is a row of such bands,
    each at its own fixed frequency
  8. Five multiplies per sample

    The band is a biquad: each output mixes the last three inputs and two outputs. y[n] = b0·x[n] + b1·x[n−1] + b2·x[n−2] − a1·y[n−1] − a2·y[n−2]. That is 220,500 multiplies a second per channel.

    Direct Form 1 (a0 scaled to 1):
      b0 = 0.88861   a1 = −1.58814
      b1 = −1.58814  a2 = 0.74514
      b2 = 0.85653
    
    5 multiplies × 44100 samples/s
      = 220,500 multiplies/s per channel
    
    feedback (a1, a2) makes it an IIR filter:
    one biquad gives a sharp, cheap band
  9. The whine is gone, the notes stay

    Run every sample through the band and look again. The whine drops from −26.7 to −44.7 dBFS, 18 dB down; the note still reads −6.3 dBFS. On the spectrogram the 3 kHz line fades; the stairs stay.

    frame 10, before -> after the EQ
      note   −6.3 -> −6.3 dBFS
      whine  −26.7 -> −44.7 dBFS
    
    18 dB less = 1/8 of the amplitude
    the output goes to a DAC and a speaker
    (see the lesson on reconstruction)
  10. The same in Python

    With numpy and scipy, signal.spectrogram computes the whole picture: Hann frames of 1024, overlap 512. signal.lfilter runs the biquad over every sample, given b and a from the cookbook formulas.

    from scipy import signal
    f, t, S = signal.spectrogram(
        x, fs, 'hann', 1024, 512)
    y = signal.lfilter(b, a, x)
    
    # x: float array, fs = 44100
    # S: power per bin and frame;
    #    10*np.log10(S) gives dB
    # f: 513 bin frequencies, t: 63 frame centres
    # b, a: the peakingEQ coefficients
    
    (Python 3, numpy, scipy)

اختبار يتاح بعد الخطوة الأخيرة.