您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

filter_biquad.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <Arduino.h>
  27. #include "filter_biquad.h"
  28. #include "utility/dspinst.h"
  29. #if defined(__ARM_ARCH_7EM__)
  30. void AudioFilterBiquad::update(void)
  31. {
  32. audio_block_t *block;
  33. int32_t b0, b1, b2, a1, a2, sum;
  34. uint32_t in2, out2, bprev, aprev, flag;
  35. uint32_t *data, *end;
  36. int32_t *state;
  37. block = receiveWritable();
  38. if (!block) return;
  39. end = (uint32_t *)(block->data) + AUDIO_BLOCK_SAMPLES/2;
  40. state = (int32_t *)definition;
  41. do {
  42. b0 = *state++;
  43. b1 = *state++;
  44. b2 = *state++;
  45. a1 = *state++;
  46. a2 = *state++;
  47. bprev = *state++;
  48. aprev = *state++;
  49. sum = *state & 0x3FFF;
  50. data = end - AUDIO_BLOCK_SAMPLES/2;
  51. do {
  52. in2 = *data;
  53. sum = signed_multiply_accumulate_32x16b(sum, b0, in2);
  54. sum = signed_multiply_accumulate_32x16t(sum, b1, bprev);
  55. sum = signed_multiply_accumulate_32x16b(sum, b2, bprev);
  56. sum = signed_multiply_accumulate_32x16t(sum, a1, aprev);
  57. sum = signed_multiply_accumulate_32x16b(sum, a2, aprev);
  58. out2 = signed_saturate_rshift(sum, 16, 14);
  59. sum &= 0x3FFF;
  60. sum = signed_multiply_accumulate_32x16t(sum, b0, in2);
  61. sum = signed_multiply_accumulate_32x16b(sum, b1, in2);
  62. sum = signed_multiply_accumulate_32x16t(sum, b2, bprev);
  63. sum = signed_multiply_accumulate_32x16b(sum, a1, out2);
  64. sum = signed_multiply_accumulate_32x16t(sum, a2, aprev);
  65. bprev = in2;
  66. aprev = pack_16b_16b(
  67. signed_saturate_rshift(sum, 16, 14), out2);
  68. // retaining part of the sum is meant to implement the
  69. // "first order noise shaping" described in this article:
  70. // http://www.earlevel.com/main/2003/02/28/biquads/
  71. // TODO: is logical AND really correct, or maybe it
  72. // should really be signed_saturate_rshift() ???
  73. sum &= 0x3FFF;
  74. bprev = in2;
  75. *data++ = aprev;
  76. } while (data < end);
  77. flag = *state & 0x80000000;
  78. *state++ = sum | flag;
  79. *(state-2) = aprev;
  80. *(state-3) = bprev;
  81. } while (flag);
  82. transmit(block);
  83. release(block);
  84. }
  85. void AudioFilterBiquad::setCoefficients(uint32_t stage, const int *coefficients)
  86. {
  87. if (stage >= 4) return;
  88. int32_t *dest = definition + (stage << 3);
  89. __disable_irq();
  90. if (stage > 0) *(dest - 1) |= 0x80000000;
  91. *dest++ = *coefficients++;
  92. *dest++ = *coefficients++;
  93. *dest++ = *coefficients++;
  94. *dest++ = *coefficients++ * -1;
  95. *dest++ = *coefficients++ * -1;
  96. //*dest++ = 0;
  97. //*dest++ = 0; // clearing filter state causes loud pop
  98. dest += 2;
  99. *dest &= 0x80000000;
  100. __enable_irq();
  101. }
  102. #elif defined(KINETISL)
  103. void AudioFilterBiquad::update(void)
  104. {
  105. audio_block_t *block;
  106. block = receiveReadOnly();
  107. if (block) release(block);
  108. }
  109. #endif