A wearable ornament that can respond to music (spectrograph mode) or dancing (shaker mode). Based on an Adafruit Circuit Playground Bluefruit
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

teaandtechtime_fft.py 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c) 2019 Tom Schucker for Tea and Tech Time
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. # THE SOFTWARE.
  22. """
  23. `teaandtechtime_fft`
  24. ================================================================================
  25. CircuitPython FFT Library
  26. * Author(s): Tom Schucker
  27. Implementation Notes
  28. ---------------------
  29. This variant comes from @thedod's `no-itertools` fork:
  30. https://github.com/thedod/CircuitPython_FFT/tree/no-itertools
  31. **Hardware:**
  32. **Software and Dependencies:**
  33. * Adafruit CircuitPython firmware for the supported boards:
  34. https://github.com/adafruit/circuitpython/releases
  35. """
  36. # imports
  37. from math import pi, sin, cos, sqrt, pow, log
  38. import array
  39. __version__ = "0.0.0-auto.0"
  40. __repo__ = "https://github.com/tschucker/Teaandtechtime_CircuitPython_FFT.git"
  41. #Computes the complex fft of the input array needs to be power of 2 length to work.
  42. def fft(x):
  43. N = len(x)
  44. if N <= 1: return x
  45. even = fft([x[i] for i in range(0, N, 2)])
  46. odd = fft([x[i] for i in range(1, N, 2)])
  47. T = [cos(2*pi*k/N)*odd[k].real+sin(2*pi*k/N)*odd[k].imag + (cos(2*pi*k/N)*odd[k].imag-sin(2*pi*k/N)*odd[k].real)*1j for k in range(N//2)]
  48. return [even[k].real + T[k].real + (even[k].imag + T[k].imag)*1j for k in range(N//2)] + \
  49. [even[k].real - T[k].real + (even[k].imag - T[k].imag)*1j for k in range(N//2)]
  50. #Computes the complex inverse fft of the input array needs to be power of 2 length to work
  51. #not the most efficiant but uses the same fft code.
  52. def ifft(x):
  53. fft_len = float(len(x))
  54. x_swap = []
  55. for s in x:
  56. x_swap.append(s.imag + s.real*1j)
  57. temp = fft(x_swap)
  58. temp_swap = []
  59. for s in temp:
  60. temp_swap.append((s.imag/fft_len) + (s.real/fft_len)*1j)
  61. return temp_swap
  62. #Computes the double sided spectrogram of the input array needs to be a power of 2 to work
  63. def spectrogram(x):
  64. freq = fft(x)
  65. temp_list = []
  66. for f in freq:
  67. abs_val = abs(f)
  68. if abs_val != 0.0:
  69. temp_list.append(int(log(abs_val)))
  70. else:
  71. temp_list.append(0)
  72. return temp_list