音をデジタルにする——標本化・量子化・ナイキスト
マイクの電圧を「配列」にする
前回、マイクは空気の粗密を、同じ形で上下する電圧に変えました。でも、この電圧はなめらかに変わるアナログの量です。コンピューターが保存したり、第6回の DFT で分析したりするには、[0.12, 0.31, 0.45, ...] のような数字の配列にしなければなりません。
この AD 変換 は、時間を区切って測る標本化(サンプリング)と、値を段に丸める量子化からなります。基本は前の講座のアナログとデジタルの回で、センサーを例に学びました。今回は音の立場から、なぜ 44.1kHz なのか、足りないとどんな音になるのか、16bit は何デシベルか、数字をどう音に戻すのかを見ます。
標本化——1 秒に何回測るか
1 秒に測る回数をサンプリング周波数 といいます。標本化定理によると、音に含まれる最も高い周波数を として、
なら元の音を復元できます。 の半分 をナイキスト周波数といい、「記録できる周波数の上限」を表します。
なぜ 44.1kHz と 48kHz なのか
第3回で見たように、人の耳に聞こえるのはおよそ 20Hz〜20kHz です。上限 20kHz の 2 倍は 40kHz。そこに少し余裕を足したのが音楽 CD の 44.1kHz で、ナイキスト周波数は 22.05kHz です。
余裕が必要なのは、ADC の手前で高い音を消すローパスフィルターが、ぴたりとは切れず坂のようにだんだん弱めるからです。20kHz は通し、22.05kHz より上は消す。その「坂」のための余白です。
44,100 という半端な数は、CD 以前のデジタル録音で、音の数字をビデオテープに記録していたことに由来するとよく説明されます(走査線 1 本に 3 個の値 × 245 本 × 1 秒に 60 回で 、など)。映像や放送、パソコンの多くは 48kHz(ナイキスト周波数 24kHz)です。昔からの電話は 8kHz で、送るのは約 3.4kHz まで。「サ行」などの高い倍音(第5回)が削られるので、電話の声はこもって聞こえます。
エイリアシングは「折り返した音」に聞こえる
ナイキスト周波数より高い音をフィルターなしで標本化すると、測った点が別の低い周波数の波とまったく同じ並びになります。これがエイリアシングで、化けた先の周波数は、
で求められます(round は四捨五入)。 で 30kHz の超音波を測ると、
聞こえないはずの 30kHz が、聞こえる 14.1kHz の音として記録されます。ナイキスト周波数を鏡にして折り返すので、折り返し雑音とも呼ばれます。音を高くしていくと、ナイキスト周波数を超えたところから記録される音は逆に下がります。
var FS = 8000, ac = null, gn = null, src = null, playing = false, lastF = 0, wait = 0, ph = 0;
function fin() { return Math.round(Math.max(0, Math.min(1, mx / W)) * 800) * 10; }
function makeSrc(f) {
var buf = ac.createBuffer(1, FS, FS), d = buf.getChannelData(0);
for (var n = 0; n < FS; n++) d[n] = Math.sin(2 * Math.PI * f * n / FS);
var s = ac.createBufferSource(); s.buffer = buf; s.loop = true; s.connect(gn); s.start();
return s;
}
c.addEventListener('click', function () {
if (!playing) {
if (!ac) { ac = new (window.AudioContext || window.webkitAudioContext)(); gn = ac.createGain(); gn.gain.value = 0.05; gn.connect(ac.destination); }
lastF = fin(); src = makeSrc(lastF); playing = true;
} else { src.stop(); playing = false; }
});
function fx(f) { return 40 + f / 8000 * 520; }
function loop() {
var f = fin(), fa = f - FS * Math.round(f / FS), h = Math.abs(fa);
wait++;
if (playing && f !== lastF && wait > 8) { src.stop(); src = makeSrc(f); lastF = f; wait = 0; }
ph += 0.03;
ctx.fillStyle = '#0d1117'; ctx.fillRect(0, 0, W, H);
var cy = 95, A = 60, dx = 26, i, t;
ctx.strokeStyle = 'rgba(255,255,255,0.15)'; ctx.lineWidth = 1;
ctx.beginPath(); ctx.moveTo(40, cy); ctx.lineTo(560, cy); ctx.stroke();
ctx.strokeStyle = 'rgba(79,195,247,0.55)'; ctx.beginPath();
for (i = 0; i <= 520; i++) { t = i / dx / FS; ctx.lineTo(40 + i, cy - A * Math.sin(2 * Math.PI * f * t + ph)); }
ctx.stroke();
var bad = f > FS / 2;
ctx.strokeStyle = bad ? '#ff7043' : '#66bb6a'; ctx.lineWidth = 2; ctx.beginPath();
for (i = 0; i <= 520; i++) { t = i / dx / FS; ctx.lineTo(40 + i, cy - A * Math.sin(2 * Math.PI * fa * t + ph)); }
ctx.stroke();
ctx.fillStyle = '#ffd54f';
for (i = 0; i <= 20; i++) { ctx.beginPath(); ctx.arc(40 + i * dx, cy - A * Math.sin(2 * Math.PI * f * i / FS + ph), 4, 0, 7); ctx.fill(); }
var ay = 235;
ctx.fillStyle = 'rgba(102,187,106,0.12)'; ctx.fillRect(40, ay - 20, 260, 20);
ctx.fillStyle = 'rgba(255,112,67,0.12)'; ctx.fillRect(300, ay - 20, 260, 20);
ctx.strokeStyle = '#e2e8f0'; ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(40, ay); ctx.lineTo(560, ay); ctx.stroke();
ctx.strokeStyle = '#a78bfa'; ctx.setLineDash([4, 4]); ctx.beginPath(); ctx.moveTo(300, ay - 34); ctx.lineTo(300, ay + 8); ctx.stroke(); ctx.setLineDash([]);
ctx.font = '12px sans-serif'; ctx.fillStyle = '#718096';
ctx.fillText('0', 36, ay + 18); ctx.fillText('4000', 288, ay + 18); ctx.fillText('8000 Hz', 535, ay + 18);
ctx.fillStyle = '#a78bfa'; ctx.fillText('ナイキスト周波数', 250, ay - 38);
if (bad) {
ctx.strokeStyle = '#ff7043'; ctx.lineWidth = 1.5; ctx.beginPath();
ctx.moveTo(fx(f), ay - 6); ctx.quadraticCurveTo((fx(f) + fx(h)) / 2, ay - 60, fx(h), ay - 6); ctx.stroke();
}
ctx.fillStyle = '#4fc3f7'; ctx.beginPath(); ctx.arc(fx(f), ay, 6, 0, 7); ctx.fill();
ctx.fillStyle = bad ? '#ff7043' : '#66bb6a'; ctx.beginPath(); ctx.arc(fx(h), ay, 4, 0, 7); ctx.fill();
ctx.font = '14px sans-serif';
ctx.fillStyle = '#4fc3f7'; ctx.fillText('入力: ' + f + ' Hz', 40, 284);
ctx.fillStyle = bad ? '#ff7043' : '#66bb6a'; ctx.fillText('記録される音: ' + h + ' Hz', 200, 284);
ctx.fillText(bad ? '4000Hz を超えた → 折り返して低い音に化ける' : '4000Hz 以下 → 正しく記録できる', 40, 310);
ctx.fillStyle = playing ? '#ffd54f' : '#718096';
ctx.fillText(playing ? '再生中(クリックで止める)' : 'クリックで鳴らす', 400, 284);
requestAnimationFrame(loop);
}
loop(); 青が入力、黄色の点が測った値、緑またはオレンジが「点から考えられるいちばん低い波」です。右半分では、右へ動かすほど記録される音が下がることを目と耳で確かめてください(ブラウザによって少し違って聞こえます)。化けた音は本物と区別できないので、測る前に防ぐしかありません。それが ADC の手前のローパスフィルター(アンチエイリアシングフィルター)の役目です。
量子化——何段階で測るか
次は、測った値をどれだけ細かい数字にするかです。音ではふつう、マイナスからプラスまで対称に段を作ります。16bit なら 段で、値は から までの整数です。
16bit は 96dB
第3回のデシベルを思い出しましょう。2 つの音圧の比を dB で表すと でした。16bit で表せる「いちばん大きな振れ幅」と「1 段の幅」の比は なので、
この幅をダイナミックレンジといいます。式の形からわかるとおり、1 bit 増やすごとに約 6dB 広がります。
| ビット数 | 段の数 | ダイナミックレンジ | 主な使いみち |
|---|---|---|---|
| 8 bit | 256 | 約 48 dB | 昔のゲーム機やパソコン |
| 16 bit | 65,536 | 約 96 dB | 音楽 CD |
| 24 bit | 約 1,678 万 | 約 144 dB | 録音・編集、ハイレゾ |
小さな音ほど、量子化の影響を受ける
段の幅は音の大きさに関係なく一定です。大きな音は何万段も使えますが、フェードアウトのような小さな音は、ほんの数段しか使えません。元の波との差(量子化誤差)が、ザラザラした雑音として聞こえます。
var ac = null, src = null, playing = false;
function bits() { return 1 + Math.floor(Math.max(0, Math.min(0.999, mx / W)) * 16); }
function env(u) { return Math.exp(-4.6 * u); }
function q(x, b) {
var L = Math.pow(2, b - 1), k = Math.floor(x * L);
k = Math.max(-L, Math.min(L - 1, k));
return (k + 0.5) / L;
}
c.addEventListener('click', function () {
if (playing) { src.stop(); playing = false; return; }
if (!ac) ac = new (window.AudioContext || window.webkitAudioContext)();
var sr = ac.sampleRate, n = sr * 2, b = bits(), buf = ac.createBuffer(1, n, sr), d = buf.getChannelData(0);
for (var i = 0; i < n; i++) d[i] = 0.08 * q(0.9 * env(i / n) * Math.sin(2 * Math.PI * 440 * i / sr), b);
src = ac.createBufferSource(); src.buffer = buf; src.connect(ac.destination);
src.onended = function () { playing = false; };
src.start(); playing = true;
});
function sig(u) { return 0.9 * env(u) * Math.sin(2 * Math.PI * 14 * u); }
function loop() {
var b = bits(), i, u, y;
ctx.fillStyle = '#0d1117'; ctx.fillRect(0, 0, W, H);
var cy = 95, A = 75;
ctx.strokeStyle = 'rgba(255,255,255,0.12)'; ctx.lineWidth = 1;
ctx.beginPath(); ctx.moveTo(40, cy); ctx.lineTo(560, cy); ctx.stroke();
ctx.strokeStyle = 'rgba(79,195,247,0.5)'; ctx.lineWidth = 1.5; ctx.beginPath();
for (i = 0; i <= 520; i++) ctx.lineTo(40 + i, cy - A * sig(i / 520));
ctx.stroke();
ctx.strokeStyle = '#a78bfa'; ctx.lineWidth = 2; ctx.beginPath();
for (i = 0; i <= 520; i += 2) ctx.lineTo(40 + i, cy - A * q(sig(i / 520), b));
ctx.stroke();
ctx.strokeStyle = 'rgba(255,213,79,0.6)'; ctx.strokeRect(404, cy - 12, 156, 24);
var zy = 250, Z = 20;
ctx.strokeStyle = 'rgba(255,255,255,0.12)'; ctx.beginPath(); ctx.moveTo(40, zy); ctx.lineTo(560, zy); ctx.stroke();
ctx.strokeStyle = 'rgba(79,195,247,0.5)'; ctx.lineWidth = 1.5; ctx.beginPath();
for (i = 0; i <= 520; i++) { u = 0.7 + 0.3 * i / 520; ctx.lineTo(40 + i, zy - A * Z * sig(u)); }
ctx.stroke();
ctx.strokeStyle = '#a78bfa'; ctx.lineWidth = 2; ctx.beginPath();
for (i = 0; i <= 520; i += 2) {
u = 0.7 + 0.3 * i / 520; y = zy - A * Z * q(sig(u), b);
ctx.lineTo(40 + i, Math.max(150, Math.min(H - 10, y)));
}
ctx.stroke();
ctx.font = '13px sans-serif';
ctx.fillStyle = '#a78bfa'; ctx.fillText(b + ' bit = ' + Math.pow(2, b) + ' 段', 40, 185);
ctx.fillStyle = '#ffd54f'; ctx.fillText('ダイナミックレンジ 約' + Math.round(6.02 * b) + 'dB', 190, 185);
ctx.fillStyle = '#718096'; ctx.font = '12px sans-serif';
ctx.fillText('青: 元の音 紫: 量子化した音 黄色の枠を 20 倍に拡大 ↓', 40, 22);
ctx.fillStyle = playing ? '#ff7043' : '#718096'; ctx.font = '13px sans-serif';
ctx.fillText(playing ? '再生中(クリックで止める)' : 'クリックで鳴らす', 400, 185);
requestAnimationFrame(loop);
}
loop(); 4bit まで下げると、上の大きな部分はそれらしい形なのに、拡大した小さな部分は階段や平らな線になります。実際の録音では、ごく小さなノイズをわざと加えて誤差を目立たなくするディザも使われます。
プログラムでは、-1〜1 の小数を 16bit の整数にするのが量子化です。
// 1 秒分の 440Hz を、44.1kHz・16bit の数字の列にする
const fs = 44100;
const samples = new Int16Array(fs); // 44,100 個
for (let n = 0; n < fs; n++) {
const x = 0.5 * Math.sin(2 * Math.PI * 440 * n / fs); // 標本化
samples[n] = Math.round(x * 32767); // 量子化
}
console.log(samples.length * 2, 'バイト'); // 88200 バイト
n / fs が「n 番目の測定の時刻」、Math.round が「段に丸める」にあたります。
DAC——数字を音に戻す
再生のときは DAC(DA 変換器)が数字を電圧に戻します。ただ、DAC がそのまま出すのは、次の数字まで同じ電圧を保つ階段の形です。階段の角には、元の音にない高い周波数が含まれます。第6回のスペクトルで見ると、 のほかに 、、… とサンプリング周波数のまわりに写し(イメージ)ができています。そこで DAC のあとにもローパスフィルターを通してナイキスト周波数より上を消すと、元のなめらかな波が戻ります。標本化定理の「復元できる」とは、この操作のことです。
var filt = false, ph = 0;
c.addEventListener('click', function () { filt = !filt; });
function sinc(x) { return Math.abs(x) < 1e-6 ? 1 : Math.sin(Math.PI * x) / (Math.PI * x); }
function loop() {
var f = 0.04 + Math.max(0, Math.min(1, mx / W)) * 0.4, i, n, t, y, s;
ph += 0.02;
ctx.fillStyle = '#0d1117'; ctx.fillRect(0, 0, W, H);
var cy = 100, A = 65, dx = 13;
function smp(k) { return Math.sin(2 * Math.PI * f * k + ph); }
ctx.strokeStyle = 'rgba(255,255,255,0.12)'; ctx.lineWidth = 1;
ctx.beginPath(); ctx.moveTo(40, cy); ctx.lineTo(560, cy); ctx.stroke();
ctx.strokeStyle = filt ? 'rgba(255,213,79,0.25)' : '#ffd54f'; ctx.lineWidth = 2; ctx.beginPath();
for (n = 0; n < 40; n++) { y = cy - A * smp(n); ctx.lineTo(40 + n * dx, y); ctx.lineTo(40 + (n + 1) * dx, y); }
ctx.stroke();
if (filt) {
ctx.strokeStyle = '#66bb6a'; ctx.lineWidth = 2.5; ctx.beginPath();
for (i = 0; i <= 520; i += 2) {
t = i / dx - 0.5; s = 0;
for (n = Math.floor(t) - 12; n <= Math.floor(t) + 12; n++) s += smp(n) * sinc(t - n);
ctx.lineTo(40 + i, cy - A * s);
}
ctx.stroke();
}
var ay = 280, sc = 120;
ctx.strokeStyle = '#e2e8f0'; ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(40, ay); ctx.lineTo(560, ay); ctx.stroke();
function fx(v) { return 40 + v / 2.5 * 520; }
ctx.font = '12px sans-serif'; ctx.fillStyle = '#718096';
ctx.fillText('0', fx(0) - 3, ay + 16); ctx.fillText('fs/2', fx(0.5) - 10, ay + 16);
ctx.fillText('fs', fx(1) - 5, ay + 16); ctx.fillText('2fs', fx(2) - 8, ay + 16);
ctx.strokeStyle = '#a78bfa'; ctx.setLineDash([4, 4]); ctx.beginPath(); ctx.moveTo(fx(0.5), ay - 100); ctx.lineTo(fx(0.5), ay); ctx.stroke(); ctx.setLineDash([]);
var comps = [f, 1 - f, 1 + f, 2 - f, 2 + f];
for (i = 0; i < comps.length; i++) {
var v = comps[i]; if (v > 2.5) continue;
var hgt = Math.abs(sinc(v)) * sc * 0.75, img = i > 0;
ctx.fillStyle = img ? (filt ? 'rgba(255,112,67,0.15)' : '#ff7043') : '#4fc3f7';
ctx.fillRect(fx(v) - 4, ay - hgt, 8, hgt);
}
ctx.font = '14px sans-serif';
ctx.fillStyle = '#4fc3f7'; ctx.fillText('元の音', fx(f) - 20, ay - 92);
ctx.fillStyle = '#ff7043'; ctx.fillText('階段の角が作る写し(イメージ)', 280, 200);
ctx.fillStyle = filt ? '#66bb6a' : '#ffd54f';
ctx.fillText(filt ? 'フィルター オン: 写しが消え、なめらかな波に' : 'フィルター オフ: DAC の出力は階段の形(クリックでオン)', 40, 22);
requestAnimationFrame(loop);
}
loop(); データ量を計算する
非圧縮の音のデータ量は、掛け算で求められます。
CD と同じ 44.1kHz・16bit・ステレオで、3 分(180 秒)の曲なら、
録音・編集でよく使う 48kHz・24bit・ステレオなら、1 秒あたり バイト、3 分で約 51.8MB になります。
WAV と MP3
WAV は、サンプリング周波数・ビット数などを書いた小さな見出し(多くは 44 バイト)のあとに、上で作ったような数字の列をそのまま並べたファイルです(ステレオなら左・右・左・右……)。だから計算どおりの大きさになります。
MP3 や AAC は非可逆圧縮です。音を短い区切りごとに周波数に分け(第6回の考え方)、耳に聞こえにくい部分を削ります。たとえば大きな音のすぐ近くの周波数の小さな音は、かき消されて聞こえません(マスキング)。128kbps の MP3 は、CD の約 1,411kbps の約 11 分の 1 です。削った情報は戻らないので、MP3 を WAV に変換しても元の音には戻りません。
一方、FLAC などの可逆圧縮は、ZIP のように完全に元に戻せる形で小さくします。縮む割合は曲によりますが、元の半分から 7 割ほどになることが多いです。
よくある疑問
Q. ハイレゾ(96kHz・24bit など)は、CD より音がいいの? 記録できる範囲は広がりますが、ふつうに聞いて違いがわかるかどうかは意見が分かれています。録音や編集の途中では、計算の余裕のために 24bit がよく使われます。
Q. サンプリング周波数を後から上げれば、高音は戻る? 戻りません。22.05kHz より上は録音の時点で消えています。数字の間を埋めて 96kHz にしても、増えるのは「すでにある音」の点だけです。
Q. ビット数を増やせば、サンプリング周波数は低くてもいい? いいえ。標本化は「どれだけ高い音まで」、量子化は「どれだけ小さな音まで」を決める別の性質で、互いに補えません。
まとめ
- AD 変換は標本化(時間を区切る)と量子化(値を段に丸める)
- 。可聴域の上限 約 20kHz に余裕を足して、CD は 44.1kHz、映像や放送は 48kHz、電話は 8kHz
- ナイキスト周波数を超えた音は折り返して低い音に化ける()。測る前のローパスフィルターで防ぐ
- bit のダイナミックレンジは約 dB。16bit で約 96dB、24bit で約 144dB。小さな音ほど量子化の影響を受ける
- DAC の出力は階段の形。再生用のローパスフィルターで写しを消すと、なめらかな波に戻る
- データ量 = × バイト数 × チャンネル数 × 秒数。CD 品質の 3 分で約 31.8MB
- WAV は数字の列をそのまま、MP3 は聞こえにくい部分を削る非可逆圧縮、FLAC は可逆圧縮
音が数字の配列になったので、あとは計算で自由に変えられます。次回は、配列の足し算と掛け算だけで、高い音を消したり(ローパス)、低い音を消したり(ハイパス)、エコーをかけたりするデジタルフィルターを JavaScript で書きます。今回登場した「ローパスフィルター」も、そこで正体がわかります。