Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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