Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

155 Zeilen
4.4KB

  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 "arm_math.h"
  28. // TODO: this should be a class member, so more than one FFT can be used
  29. static arm_cfft_radix4_instance_q15 fft_inst;
  30. void AudioAnalyzeFFT256::init(void)
  31. {
  32. // TODO: replace this with static const version
  33. arm_cfft_radix4_init_q15(&fft_inst, 256, 0, 1);
  34. //for (int i=0; i<2048; i++) {
  35. //buffer[i] = i * 3;
  36. //}
  37. //__disable_irq();
  38. //ARM_DEMCR |= ARM_DEMCR_TRCENA;
  39. //ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA;
  40. //uint32_t n = ARM_DWT_CYCCNT;
  41. //arm_cfft_radix2_q15(&fft_inst, buffer);
  42. //n = ARM_DWT_CYCCNT - n;
  43. //__enable_irq();
  44. //cycles = n;
  45. //arm_cmplx_mag_q15(buffer, buffer, 512);
  46. // each audio block is 278525 cycles @ 96 MHz
  47. // 256 point fft2 takes 65408 cycles
  48. // 256 point fft4 takes 49108 cycles
  49. // 128 point cmag takes 10999 cycles
  50. // 1024 point fft2 takes 125948 cycles
  51. // 1024 point fft4 takes 125840 cycles
  52. // 512 point cmag takes 43764 cycles
  53. //state = 0;
  54. //outputflag = false;
  55. }
  56. static void copy_to_fft_buffer(void *destination, const void *source)
  57. {
  58. const int16_t *src = (const int16_t *)source;
  59. int16_t *dst = (int16_t *)destination;
  60. // TODO: optimize this
  61. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  62. *dst++ = *src++; // real
  63. *dst++ = 0; // imaginary
  64. }
  65. }
  66. static void apply_window_to_fft_buffer(void *buffer, const void *window)
  67. {
  68. int16_t *buf = (int16_t *)buffer;
  69. const int16_t *win = (int16_t *)window;;
  70. for (int i=0; i < 256; i++) {
  71. int32_t val = *buf * *win++;
  72. //*buf = signed_saturate_rshift(val, 16, 15);
  73. *buf = val >> 15;
  74. buf += 2;
  75. }
  76. }
  77. void AudioAnalyzeFFT256::update(void)
  78. {
  79. audio_block_t *block;
  80. block = receiveReadOnly();
  81. if (!block) return;
  82. if (!prevblock) {
  83. prevblock = block;
  84. return;
  85. }
  86. copy_to_fft_buffer(buffer, prevblock->data);
  87. copy_to_fft_buffer(buffer+256, block->data);
  88. //window = AudioWindowBlackmanNuttall256;
  89. //window = NULL;
  90. if (window) apply_window_to_fft_buffer(buffer, window);
  91. arm_cfft_radix4_q15(&fft_inst, buffer);
  92. // TODO: is this averaging correct? G. Heinzel's paper says we're
  93. // supposed to average the magnitude squared, then do the square
  94. // root at the end after dividing by naverage.
  95. arm_cmplx_mag_q15(buffer, buffer, 128);
  96. if (count == 0) {
  97. for (int i=0; i < 128; i++) {
  98. output[i] = buffer[i];
  99. }
  100. } else {
  101. for (int i=0; i < 128; i++) {
  102. output[i] += buffer[i];
  103. }
  104. }
  105. if (++count == naverage) {
  106. count = 0;
  107. for (int i=0; i < 128; i++) {
  108. output[i] /= naverage;
  109. }
  110. outputflag = true;
  111. }
  112. release(prevblock);
  113. prevblock = block;
  114. #if 0
  115. block = receiveReadOnly();
  116. if (state == 0) {
  117. //Serial.print("0");
  118. if (block == NULL) return;
  119. copy_to_fft_buffer(buffer, block->data);
  120. state = 1;
  121. } else if (state == 1) {
  122. //Serial.print("1");
  123. if (block == NULL) return;
  124. copy_to_fft_buffer(buffer+256, block->data);
  125. arm_cfft_radix4_q15(&fft_inst, buffer);
  126. state = 2;
  127. } else {
  128. //Serial.print("2");
  129. arm_cmplx_mag_q15(buffer, output, 128);
  130. outputflag = true;
  131. if (block == NULL) return;
  132. copy_to_fft_buffer(buffer, block->data);
  133. state = 1;
  134. }
  135. release(block);
  136. #endif
  137. }