You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 satır
4.1KB

  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 "analyze_fft256.h"
  28. #include "sqrt_integer.h"
  29. #include "utility/dspinst.h"
  30. // 140312 - PAH - slightly faster copy
  31. __attribute__((unused))
  32. static void copy_to_fft_buffer(void *destination, const void *source)
  33. {
  34. const uint16_t *src = (const uint16_t *)source;
  35. uint32_t *dst = (uint32_t *)destination;
  36. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  37. *dst++ = *src++; // real sample plus a zero for imaginary
  38. }
  39. }
  40. __attribute__((unused))
  41. static void apply_window_to_fft_buffer(void *buffer, const void *window)
  42. {
  43. int16_t *buf = (int16_t *)buffer;
  44. const int16_t *win = (int16_t *)window;;
  45. for (int i=0; i < 256; i++) {
  46. int32_t val = *buf * *win++;
  47. //*buf = signed_saturate_rshift(val, 16, 15);
  48. *buf = val >> 15;
  49. buf += 2;
  50. }
  51. }
  52. void AudioAnalyzeFFT256::update(void)
  53. {
  54. audio_block_t *block;
  55. block = receiveReadOnly();
  56. if (!block) return;
  57. #if AUDIO_BLOCK_SAMPLES == 128 && defined (__ARM_ARCH_7EM__)
  58. if (!prevblock) {
  59. prevblock = block;
  60. return;
  61. }
  62. copy_to_fft_buffer(buffer, prevblock->data);
  63. copy_to_fft_buffer(buffer+256, block->data);
  64. //window = AudioWindowBlackmanNuttall256;
  65. //window = NULL;
  66. if (window) apply_window_to_fft_buffer(buffer, window);
  67. arm_cfft_radix4_q15(&fft_inst, buffer);
  68. // G. Heinzel's paper says we're supposed to average the magnitude
  69. // squared, then do the square root at the end.
  70. if (count == 0) {
  71. for (int i=0; i < 128; i++) {
  72. uint32_t tmp = *((uint32_t *)buffer + i);
  73. uint32_t magsq = multiply_16tx16t_add_16bx16b(tmp, tmp);
  74. sum[i] = magsq / naverage;
  75. }
  76. } else {
  77. for (int i=0; i < 128; i++) {
  78. uint32_t tmp = *((uint32_t *)buffer + i);
  79. uint32_t magsq = multiply_16tx16t_add_16bx16b(tmp, tmp);
  80. sum[i] += magsq / naverage;
  81. }
  82. }
  83. if (++count == naverage) {
  84. count = 0;
  85. for (int i=0; i < 128; i++) {
  86. output[i] = sqrt_uint32_approx(sum[i]);
  87. }
  88. outputflag = true;
  89. }
  90. release(prevblock);
  91. prevblock = block;
  92. #elif AUDIO_BLOCK_SAMPLES == 64
  93. if (prevblocks[2] == NULL) {
  94. prevblocks[2] = prevblocks[1];
  95. prevblocks[1] = prevblocks[0];
  96. prevblocks[0] = block;
  97. return;
  98. }
  99. if (count == 0) {
  100. count = 1;
  101. copy_to_fft_buffer(buffer, prevblocks[2]->data);
  102. copy_to_fft_buffer(buffer+128, prevblocks[1]->data);
  103. copy_to_fft_buffer(buffer+256, prevblocks[1]->data);
  104. copy_to_fft_buffer(buffer+384, block->data);
  105. if (window) apply_window_to_fft_buffer(buffer, window);
  106. arm_cfft_radix4_q15(&fft_inst, buffer);
  107. } else {
  108. count = 2;
  109. const uint32_t *p = (uint32_t *)buffer;
  110. for (int i=0; i < 128; i++) {
  111. uint32_t tmp = *p++;
  112. int16_t v1 = tmp & 0xFFFF;
  113. int16_t v2 = tmp >> 16;
  114. output[i] = sqrt_uint32_approx(v1 * v1 + v2 * v2);
  115. }
  116. }
  117. release(prevblocks[2]);
  118. prevblocks[2] = prevblocks[1];
  119. prevblocks[1] = prevblocks[0];
  120. prevblocks[0] = block;
  121. #endif
  122. }