<!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>
