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.

analyze_fft1024.cpp 4.8KB

10 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_fft1024.h"
  27. #include "sqrt_integer.h"
  28. #include "utility/dspinst.h"
  29. #include "arm_math.h"
  30. // TODO: this should be a class member, so more than one FFT can be used
  31. static arm_cfft_radix4_instance_q15 fft_inst;
  32. void AudioAnalyzeFFT1024::init(void)
  33. {
  34. // TODO: replace this with static const version
  35. arm_cfft_radix4_init_q15(&fft_inst, 1024, 0, 1);
  36. //state = 0;
  37. //outputflag = false;
  38. }
  39. // 140312 - PAH - slightly faster copy
  40. static void copy_to_fft_buffer(void *destination, const void *source)
  41. {
  42. const uint16_t *src = (const uint16_t *)source;
  43. uint32_t *dst = (uint32_t *)destination;
  44. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  45. *dst++ = *src++; // real sample plus a zero for imaginary
  46. }
  47. }
  48. static void apply_window_to_fft_buffer(void *buffer, const void *window)
  49. {
  50. int16_t *buf = (int16_t *)buffer;
  51. const int16_t *win = (int16_t *)window;;
  52. for (int i=0; i < 1024; i++) {
  53. int32_t val = *buf * *win++;
  54. //*buf = signed_saturate_rshift(val, 16, 15);
  55. *buf = val >> 15;
  56. buf += 2;
  57. }
  58. }
  59. void AudioAnalyzeFFT1024::update(void)
  60. {
  61. audio_block_t *block;
  62. block = receiveReadOnly();
  63. if (!block) return;
  64. switch (state) {
  65. case 0:
  66. blocklist[0] = block;
  67. state = 1;
  68. break;
  69. case 1:
  70. blocklist[1] = block;
  71. state = 2;
  72. break;
  73. case 2:
  74. blocklist[2] = block;
  75. state = 3;
  76. break;
  77. case 3:
  78. blocklist[3] = block;
  79. state = 4;
  80. break;
  81. case 4:
  82. blocklist[4] = block;
  83. state = 5;
  84. break;
  85. case 5:
  86. blocklist[5] = block;
  87. state = 6;
  88. break;
  89. case 6:
  90. blocklist[6] = block;
  91. state = 7;
  92. break;
  93. case 7:
  94. blocklist[7] = block;
  95. // TODO: perhaps distribute the work over multiple update() ??
  96. // github pull requsts welcome......
  97. copy_to_fft_buffer(buffer+0x000, blocklist[0]->data);
  98. copy_to_fft_buffer(buffer+0x100, blocklist[1]->data);
  99. copy_to_fft_buffer(buffer+0x200, blocklist[2]->data);
  100. copy_to_fft_buffer(buffer+0x300, blocklist[3]->data);
  101. copy_to_fft_buffer(buffer+0x400, blocklist[4]->data);
  102. copy_to_fft_buffer(buffer+0x500, blocklist[5]->data);
  103. copy_to_fft_buffer(buffer+0x600, blocklist[6]->data);
  104. copy_to_fft_buffer(buffer+0x700, blocklist[7]->data);
  105. if (window) apply_window_to_fft_buffer(buffer, window);
  106. arm_cfft_radix4_q15(&fft_inst, buffer);
  107. // TODO: support averaging multiple copies
  108. for (int i=0; i < 512; i++) {
  109. uint32_t tmp = *((uint32_t *)buffer + i); // real & imag
  110. uint32_t magsq = multiply_16tx16t_add_16bx16b(tmp, tmp);
  111. output[i] = sqrt_uint32_approx(magsq);
  112. }
  113. outputflag = true;
  114. release(blocklist[0]);
  115. release(blocklist[1]);
  116. release(blocklist[2]);
  117. release(blocklist[3]);
  118. blocklist[0] = blocklist[4];
  119. blocklist[1] = blocklist[5];
  120. blocklist[2] = blocklist[6];
  121. blocklist[3] = blocklist[7];
  122. state = 4;
  123. break;
  124. }
  125. /*
  126. copy_to_fft_buffer(buffer, prevblock->data);
  127. copy_to_fft_buffer(buffer+256, block->data);
  128. if (window) apply_window_to_fft_buffer(buffer, window);
  129. arm_cfft_radix4_q15(&fft_inst, buffer);
  130. if (count == 0) {
  131. for (int i=0; i < 128; i++) {
  132. uint32_t tmp = *((uint32_t *)buffer + i);
  133. uint32_t magsq = multiply_16tx16t_add_16bx16b(tmp, tmp);
  134. sum[i] = magsq / naverage;
  135. }
  136. } else {
  137. for (int i=0; i < 128; i++) {
  138. uint32_t tmp = *((uint32_t *)buffer + i);
  139. uint32_t magsq = multiply_16tx16t_add_16bx16b(tmp, tmp);
  140. sum[i] += magsq / naverage;
  141. }
  142. }
  143. if (++count == naverage) {
  144. count = 0;
  145. for (int i=0; i < 128; i++) {
  146. output[i] = sqrt_uint32_approx(sum[i]);
  147. }
  148. outputflag = true;
  149. }
  150. */
  151. }