AAFilter.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. ////////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// FIR low-pass (anti-alias) filter with filter coefficient design routine and
  4. /// MMX optimization.
  5. ///
  6. /// Anti-alias filter is used to prevent folding of high frequencies when
  7. /// transposing the sample rate with interpolation.
  8. ///
  9. /// Author : Copyright (c) Olli Parviainen
  10. /// Author e-mail : oparviai 'at' iki.fi
  11. /// SoundTouch WWW: http://www.surina.net/soundtouch
  12. ///
  13. ////////////////////////////////////////////////////////////////////////////////
  14. //
  15. // Last changed : $Date: 2009-01-11 11:34:24 +0000 (Sun, 11 Jan 2009) $
  16. // File revision : $Revision: 4 $
  17. //
  18. // $Id: AAFilter.cpp 45 2009-01-11 11:34:24Z oparviai $
  19. //
  20. ////////////////////////////////////////////////////////////////////////////////
  21. //
  22. // License :
  23. //
  24. // SoundTouch audio processing library
  25. // Copyright (c) Olli Parviainen
  26. //
  27. // This library is free software; you can redistribute it and/or
  28. // modify it under the terms of the GNU Lesser General Public
  29. // License as published by the Free Software Foundation; either
  30. // version 2.1 of the License, or (at your option) any later version.
  31. //
  32. // This library is distributed in the hope that it will be useful,
  33. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  35. // Lesser General Public License for more details.
  36. //
  37. // You should have received a copy of the GNU Lesser General Public
  38. // License along with this library; if not, write to the Free Software
  39. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40. //
  41. ////////////////////////////////////////////////////////////////////////////////
  42. #include <memory.h>
  43. #include <assert.h>
  44. #include <math.h>
  45. #include <stdlib.h>
  46. #include "AAFilter.h"
  47. #include "FIRFilter.h"
  48. using namespace soundtouch;
  49. #define PI 3.141592655357989
  50. #define TWOPI (2 * PI)
  51. /*****************************************************************************
  52. *
  53. * Implementation of the class 'AAFilter'
  54. *
  55. *****************************************************************************/
  56. AAFilter::AAFilter(uint len)
  57. {
  58. pFIR = FIRFilter::newInstance();
  59. cutoffFreq = 0.5;
  60. setLength(len);
  61. }
  62. AAFilter::~AAFilter()
  63. {
  64. delete pFIR;
  65. }
  66. // Sets new anti-alias filter cut-off edge frequency, scaled to
  67. // sampling frequency (nyquist frequency = 0.5).
  68. // The filter will cut frequencies higher than the given frequency.
  69. void AAFilter::setCutoffFreq(double newCutoffFreq)
  70. {
  71. cutoffFreq = newCutoffFreq;
  72. calculateCoeffs();
  73. }
  74. // Sets number of FIR filter taps
  75. void AAFilter::setLength(uint newLength)
  76. {
  77. length = newLength;
  78. calculateCoeffs();
  79. }
  80. // Calculates coefficients for a low-pass FIR filter using Hamming window
  81. void AAFilter::calculateCoeffs()
  82. {
  83. uint i;
  84. double cntTemp, temp, tempCoeff,h, w;
  85. double fc2, wc;
  86. double scaleCoeff, sum;
  87. double *work;
  88. SAMPLETYPE *coeffs;
  89. assert(length >= 2);
  90. assert(length % 4 == 0);
  91. assert(cutoffFreq >= 0);
  92. assert(cutoffFreq <= 0.5);
  93. work = new double[length];
  94. coeffs = new SAMPLETYPE[length];
  95. fc2 = 2.0 * cutoffFreq;
  96. wc = PI * fc2;
  97. tempCoeff = TWOPI / (double)length;
  98. sum = 0;
  99. for (i = 0; i < length; i ++)
  100. {
  101. cntTemp = (double)i - (double)(length / 2);
  102. temp = cntTemp * wc;
  103. if (temp != 0)
  104. {
  105. h = fc2 * sin(temp) / temp; // sinc function
  106. }
  107. else
  108. {
  109. h = 1.0;
  110. }
  111. w = 0.54 + 0.46 * cos(tempCoeff * cntTemp); // hamming window
  112. temp = w * h;
  113. work[i] = temp;
  114. // calc net sum of coefficients
  115. sum += temp;
  116. }
  117. // ensure the sum of coefficients is larger than zero
  118. assert(sum > 0);
  119. // ensure we've really designed a lowpass filter...
  120. assert(work[length/2] > 0);
  121. assert(work[length/2 + 1] > -1e-6);
  122. assert(work[length/2 - 1] > -1e-6);
  123. // Calculate a scaling coefficient in such a way that the result can be
  124. // divided by 16384
  125. scaleCoeff = 16384.0f / sum;
  126. for (i = 0; i < length; i ++)
  127. {
  128. // scale & round to nearest integer
  129. temp = work[i] * scaleCoeff;
  130. temp += (temp >= 0) ? 0.5 : -0.5;
  131. // ensure no overfloods
  132. assert(temp >= -32768 && temp <= 32767);
  133. coeffs[i] = (SAMPLETYPE)temp;
  134. }
  135. // Set coefficients. Use divide factor 14 => divide result by 2^14 = 16384
  136. pFIR->setCoefficients(coeffs, length, 14);
  137. delete[] work;
  138. delete[] coeffs;
  139. }
  140. // Applies the filter to the given sequence of samples.
  141. // Note : The amount of outputted samples is by value of 'filter length'
  142. // smaller than the amount of input samples.
  143. uint AAFilter::evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels) const
  144. {
  145. return pFIR->evaluate(dest, src, numSamples, numChannels);
  146. }
  147. uint AAFilter::getLength() const
  148. {
  149. return pFIR->getLength();
  150. }