選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

112 行
3.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 "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 AudioAnalyzeFFT256::init(void)
  33. {
  34. // TODO: replace this with static const version
  35. arm_cfft_radix4_init_q15(&fft_inst, 256, 0, 1);
  36. //state = 0;
  37. //outputflag = false;
  38. }
  39. static void copy_to_fft_buffer(void *destination, const void *source)
  40. {
  41. const int16_t *src = (const int16_t *)source;
  42. int16_t *dst = (int16_t *)destination;
  43. // TODO: optimize this
  44. for (int i=0; i < AUDIO_BLOCK_SAMPLES; i++) {
  45. *dst++ = *src++; // real
  46. *dst++ = 0; // imaginary
  47. }
  48. }
  49. static void apply_window_to_fft_buffer(void *buffer, const void *window)
  50. {
  51. int16_t *buf = (int16_t *)buffer;
  52. const int16_t *win = (int16_t *)window;;
  53. for (int i=0; i < 256; i++) {
  54. int32_t val = *buf * *win++;
  55. //*buf = signed_saturate_rshift(val, 16, 15);
  56. *buf = val >> 15;
  57. buf += 2;
  58. }
  59. }
  60. void AudioAnalyzeFFT256::update(void)
  61. {
  62. audio_block_t *block;
  63. block = receiveReadOnly();
  64. if (!block) return;
  65. if (!prevblock) {
  66. prevblock = block;
  67. return;
  68. }
  69. copy_to_fft_buffer(buffer, prevblock->data);
  70. copy_to_fft_buffer(buffer+256, block->data);
  71. //window = AudioWindowBlackmanNuttall256;
  72. //window = NULL;
  73. if (window) apply_window_to_fft_buffer(buffer, window);
  74. arm_cfft_radix4_q15(&fft_inst, buffer);
  75. // G. Heinzel's paper says we're supposed to average the magnitude
  76. // squared, then do the square root at the end.
  77. if (count == 0) {
  78. for (int i=0; i < 128; i++) {
  79. uint32_t tmp = *((uint32_t *)buffer + i);
  80. uint32_t magsq = multiply_16tx16t_add_16bx16b(tmp, tmp);
  81. sum[i] = magsq / naverage;
  82. }
  83. } else {
  84. for (int i=0; i < 128; i++) {
  85. uint32_t tmp = *((uint32_t *)buffer + i);
  86. uint32_t magsq = multiply_16tx16t_add_16bx16b(tmp, tmp);
  87. sum[i] += magsq / naverage;
  88. }
  89. }
  90. if (++count == naverage) {
  91. count = 0;
  92. for (int i=0; i < 128; i++) {
  93. output[i] = sqrt_uint32_approx(sum[i]);
  94. }
  95. outputflag = true;
  96. }
  97. release(prevblock);
  98. prevblock = block;
  99. }