mozilla
Your Search Results

    BiquadFilterNode.getFrequencyResponse()

    This article is in need of a technical review.

    The getFrequencyResponse() method of the BiquadFilterNode interface takes the current filtering algorithm's settings and calculates the frequency response for frequencies specified in the frequencyHz array of frequencies.

    The results are stored in two preexisting output arrays:

    • magResponse receiving the linear magnitude
    • phaseResponse receiving the phase response in radians.

    Syntax

    var audioCtx = new AudioContext();
    var biquadFilter = audioCtx.createBiquadFilter();
    biquadfilter.getFrequencyResponse(myFrequencyArray,magResponseOutput,phaseResponseOutput);
    

    Returns

    A BiquadFilterNode.

    Example

    In the following example we are using a biquad filter on a media stream (for the full demo, see our stream-source-buffer demo live, or read the source.) As part of this demo, we get the frequcy responses for this biquad filter, for five sample frequencies. We first create the float 32 arrays we need, one containing the input frequencies, and two to contain the output magnitude and phase values:

    var myFrequencyArray = new Float32Array(5);
    myFrequencyArray[0] = 1000;
    myFrequencyArray[1] = 2000;
    myFrequencyArray[2] = 3000;
    myFrequencyArray[3] = 4000;
    myFrequencyArray[4] = 5000;
    
    var magResponseOutput = new Float32Array(5);
    var phaseResponseOutput = new Float32Array(5);

    Next we create a <ul> element in our HTML to contain our results, and grab a reference to it in our JavaScript:

    <p>Biquad filter frequency response for: </p>
    <ul class="freq-response-output">
    </ul>
    var freqResponseOutput = document.querySelector('.freq-response-output');

    Finally, after creating our biquad filter, we use getFrequencyResponse() to generate the response data and put it in our arrays, then loop through each data set and output  them in a human-readable list at the bottom of the page:

    var biquadFilter = audioCtx.createBiquadFilter();
    biquadFilter.type = "lowshelf";
    biquadFilter.frequency.value = 1000;
    biquadFilter.gain.value = range.value;
    
      ...
    
    function calcFrequencyResponse() {
      biquadFilter.getFrequencyResponse(myFrequencyArray,magResponseOutput,phaseResponseOutput);
    
      for(i = 0; i <= myFrequencyArray.length-1;i++){
        var listItem = document.createElement('li');
        listItem.innerHTML = '<strong>' + myFrequencyArray[i] + 'Hz</strong>: Magnitude ' + magResponseOutput[i] + ', Phase ' + phaseResponseOutput[i] + ' radians.';
        freqResponseOutput.appendChild(listItem);
      }
    }
    
    calcFrequencyResponse();

    Parameters

    getFrequencyResponse() takes as parameters three Float32Arrays:

    frequencyHz
    An array containing hertz values that you want the frequency response for.
    magResponse
    An array that will contain the outputted magnitude of the frequency response for each inputted frequency (hertz) value. The magnitude values are unitless.
    phaseResponse
    An array that will contain the outputted phase of the frequency response for each inputted frequency (hertz) value, measured in radians.

    Specifications

    Specification Status Comment
    Web Audio API
    The definition of 'getFrequencyResponse()' in that specification.
    Working Draft  

    Browser compatibility

    Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
    Basic support 10.0webkit 25.0 (25.0)  Not supported 15.0webkit
    22 (unprefixed)
    6.0webkit
    Feature Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
    Basic support ? 26.0 1.2 ? ? ? 33.0

    See also

    Document Tags and Contributors

    Contributors to this page: fscholz, chrisdavidmills
    Last updated by: fscholz,