diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/media/webaudio/test/blink/test_iirFilterNodeGetFrequencyResponse.html | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/media/webaudio/test/blink/test_iirFilterNodeGetFrequencyResponse.html')
-rw-r--r-- | dom/media/webaudio/test/blink/test_iirFilterNodeGetFrequencyResponse.html | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/dom/media/webaudio/test/blink/test_iirFilterNodeGetFrequencyResponse.html b/dom/media/webaudio/test/blink/test_iirFilterNodeGetFrequencyResponse.html new file mode 100644 index 0000000000..09782f73be --- /dev/null +++ b/dom/media/webaudio/test/blink/test_iirFilterNodeGetFrequencyResponse.html @@ -0,0 +1,97 @@ +<!DOCTYPE HTML> +<html> +<head> + <title>Test IIRFilterNode GetFrequencyResponse</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <script type="text/javascript" src="webaudio.js"></script> + <script type="text/javascript" src="biquad-filters.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> +<pre id="test"> +<script class="testbody" type="text/javascript"> +SimpleTest.waitForExplicitFinish(); +addLoadEvent(function() { + // Modified from WebKit/LayoutTests/webaudio/iirfilter-getFrequencyResponse.html + var sampleRate = 48000; + var testDurationSec = 0.01; + + // Compute a set of linearly spaced frequencies. + function createFrequencies(nFrequencies, sampleRate) + { + var frequencies = new Float32Array(nFrequencies); + var nyquist = sampleRate / 2; + var freqDelta = nyquist / nFrequencies; + + for (var k = 0; k < nFrequencies; ++k) { + frequencies[k] = k * freqDelta; + } + + return frequencies; + } + + // Number of frequency samples to take. + var numberOfFrequencies = 1000; + + var context = new OfflineAudioContext(1, testDurationSec * sampleRate, sampleRate); + + var frequencies = createFrequencies(numberOfFrequencies, context.sampleRate); + + // 1-Pole IIR Filter + var iir = context.createIIRFilter([1], [1, -0.9]); + + var iirMag = new Float32Array(numberOfFrequencies); + var iirPhase = new Float32Array(numberOfFrequencies); + var trueMag = new Float32Array(numberOfFrequencies); + var truePhase = new Float32Array(numberOfFrequencies); + + // The IIR filter is + // H(z) = 1/(1 - 0.9*z^(-1)). + // + // The frequency response is + // H(exp(j*w)) = 1/(1 - 0.9*exp(-j*w)). + // + // Thus, the magnitude is + // |H(exp(j*w))| = 1/sqrt(1.81-1.8*cos(w)). + // + // The phase is + // arg(H(exp(j*w)) = atan(0.9*sin(w)/(.9*cos(w)-1)) + + var frequencyScale = Math.PI / (sampleRate / 2); + + for (var k = 0; k < frequencies.length; ++k) { + var omega = frequencyScale * frequencies[k]; + trueMag[k] = 1/Math.sqrt(1.81-1.8*Math.cos(omega)); + truePhase[k] = Math.atan(0.9 * Math.sin(omega) / (0.9 * Math.cos(omega) - 1)); + } + + iir.getFrequencyResponse(frequencies, iirMag, iirPhase); + compareChannels(iirMag, trueMag); + compareChannels(iirPhase, truePhase); + + // Compare IIR and Biquad Filter + // Create an IIR filter equivalent to the biquad filter. Compute the frequency response for both and verify that they are the same. + var biquad = context.createBiquadFilter(); + var coef = createFilter(biquad.type, + biquad.frequency.value / (context.sampleRate / 2), + biquad.Q.value, + biquad.gain.value); + + var iir = context.createIIRFilter([coef.b0, coef.b1, coef.b2], [1, coef.a1, coef.a2]); + + var biquadMag = new Float32Array(numberOfFrequencies); + var biquadPhase = new Float32Array(numberOfFrequencies); + var iirMag = new Float32Array(numberOfFrequencies); + var iirPhase = new Float32Array(numberOfFrequencies); + + biquad.getFrequencyResponse(frequencies, biquadMag, biquadPhase); + iir.getFrequencyResponse(frequencies, iirMag, iirPhase); + compareChannels(iirMag, biquadMag); + compareChannels(iirPhase, biquadPhase); + + SimpleTest.finish(); +}); +</script> +</pre> +</body> +</html> |