Index: third_party/WebKit/Source/platform/audio/IIRFilter.cpp |
diff --git a/third_party/WebKit/Source/platform/audio/IIRFilter.cpp b/third_party/WebKit/Source/platform/audio/IIRFilter.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..239cee52e244cb971a343e684e24c9b3d391341f |
--- /dev/null |
+++ b/third_party/WebKit/Source/platform/audio/IIRFilter.cpp |
@@ -0,0 +1,124 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "config.h" |
+ |
+#include "platform/audio/IIRFilter.h" |
+ |
+#include "wtf/MathExtras.h" |
+ |
+#include <complex> |
+ |
+namespace blink { |
+ |
+// The length of the memory buffers for the IIR filter. This MUST be a power of two and must be |
+// greater than the possible length of the filter coefficients. |
+const int kBufferLength = 32; |
+static_assert(kBufferLength >= IIRFilter::kMaxOrder + 1, |
+ "Internal IIR buffer length must be greater than maximum IIR Filter order."); |
+ |
+IIRFilter::IIRFilter(const AudioFloatArray* feedforward, const AudioFloatArray* feedback) |
+ : m_bufferIndex(0) |
+ , m_feedback(feedback) |
+ , m_feedforward(feedforward) |
+{ |
+ // These are guaranteed to be zero-initialized. |
+ m_xBuffer.allocate(kBufferLength); |
+ m_yBuffer.allocate(kBufferLength); |
+} |
+ |
+IIRFilter::~IIRFilter() |
+{ |
+} |
+ |
+void IIRFilter::reset() |
+{ |
+ m_xBuffer.zero(); |
+ m_yBuffer.zero(); |
+} |
+ |
+void IIRFilter::process(const float* sourceP, float* destP, size_t framesToProcess) |
+{ |
+ // Compute |
+ // |
+ // y[n] = sum(b[k] * x[k], k = 0, M) - sum(a[k] * y[k], k = 1, N) |
+ // |
+ // where b[k] are the feedforward coefficients and a[k] are the feedback coefficients of the |
+ // filter. |
+ |
+ // This is a Direct Form I implementation of an IIR Filter. Should we consider doing a |
+ // different implementation such as Transposed Direct Form II? |
+ const float* feedback = m_feedback->data(); |
+ const float* feedforward = m_feedforward->data(); |
+ |
+ ASSERT(feedback); |
+ ASSERT(feedforward); |
+ |
+ // Sanity check to see if the feedback coefficients have been scaled appropriately. It must |
+ // be EXACTLY 1! |
+ ASSERT(feedback[0] == 1); |
+ |
+ int feedbackLength = m_feedback->size(); |
+ int feedforwardLength = m_feedforward->size(); |
+ |
+ for (size_t n = 0; n < framesToProcess; ++n) { |
+ float yn = feedforward[0] * sourceP[n]; |
+ |
+ for (int k = 1; k < feedforwardLength; ++k) { |
+ yn += feedforward[k] * m_xBuffer[(k + m_bufferIndex) % kBufferLength]; |
+ } |
+ |
+ for (int k = 1; k < feedbackLength; ++k) { |
+ yn -= feedback[k] * m_yBuffer[(k + m_bufferIndex) % kBufferLength]; |
+ } |
+ |
+ // Save the current input and output values in the memory buffers for the next output. |
+ m_xBuffer[m_bufferIndex] = sourceP[n]; |
+ m_yBuffer[m_bufferIndex] = yn; |
+ |
+ m_bufferIndex = (m_bufferIndex - 1) & (kBufferLength - 1); |
+ |
+ destP[n] = yn; |
+ } |
+} |
+ |
+static std::complex<double> polyEval(const float* coef, std::complex<double> z, int order) |
hongchan
2015/10/12 22:55:56
This method just pops up from nowhere. Can we move
Raymond Toy
2015/10/29 17:10:00
Renamed to evaluatePolynomial and moved to the top
|
+{ |
+ // Use Horner's method to evaluate the polynomial P(z) = sum(coef[k]*z^k, k, 0, order); |
+ std::complex<double> result = 0; |
+ |
+ for (int k = order; k >= 0; --k) |
+ result = result * z + static_cast<std::complex<double>>(coef[k]); |
+ |
+ return result; |
+} |
+ |
+void IIRFilter::getFrequencyResponse(int nFrequencies, const float* frequency, float* magResponse, float* phaseResponse) |
+{ |
+ // Evaluate the z-transform of the filter at the given normalized frequencies from 0 to 1. (One |
+ // corresponds to the Nyquist frequency.) |
+ // |
+ // The z-tranform of the filter is |
+ // |
+ // H(z) = sum(b[k]*z^(-k), k, 0, M) / sum(a[k]*z^(-k), k, 0, N); |
+ // |
+ // The desired frequency response is H(exp(j*omega)), where omega is in [0, 1). |
+ // |
+ // Let P(x) = sum(c[k]*x^k, k, 0, P) be a polynomial of order P. Then each of the sums in H(z) |
+ // is equivalent to evaluating a polynomial at the point 1/z. |
+ |
+ for (int k = 0; k < nFrequencies; ++k) { |
+ // zRecip = 1/z = exp(-j*frequency) |
+ double omega = -piDouble * frequency[k]; |
+ std::complex<double> zRecip = std::complex<double>(cos(omega), sin(omega)); |
+ |
+ std::complex<double> numerator = polyEval(m_feedforward->data(), zRecip, m_feedforward->size() - 1); |
+ std::complex<double> denominator = polyEval(m_feedback->data(), zRecip, m_feedback->size() - 1); |
+ std::complex<double> response = numerator / denominator; |
+ magResponse[k] = static_cast<float>(abs(response)); |
+ phaseResponse[k] = static_cast<float>(atan2(imag(response), real(response))); |
+ } |
+} |
+ |
+} // blink |