22#
33# SPDX-License-Identifier: MIT
44
5- ''' Adapted from the FFT Example: Waterfall Spectrum Analyzer
5+ """ Adapted from the FFT Example: Waterfall Spectrum Analyzer
66by Jeff Epler
7- https://learn.adafruit.com/ulab-crunch-numbers-fast-with-circuitpython/overview '''
7+ https://learn.adafruit.com/ulab-crunch-numbers-fast-with-circuitpython/overview """
88
99import array
1010import board
1111import audiobusio
1212import busio
1313from ulab import numpy as np
14- from ulab .scipy .signal import spectrogram
14+
15+ try :
16+ from ulab .utils import spectrogram
17+ except ImportError :
18+ from ulab .scipy .signal import spectrogram
1519import adafruit_is31fl3741
1620from adafruit_is31fl3741 .adafruit_rgbmatrixqt import Adafruit_RGBMatrixQT
1721
3034# array of colors for the LEDs
3135# goes from purple to red
3236# gradient generated using https://colordesigner.io/gradient-generator
33- heatmap = [0xb000ff ,0xa600ff ,0x9b00ff ,0x8f00ff ,0x8200ff ,
34- 0x7400ff ,0x6500ff ,0x5200ff ,0x3900ff ,0x0003ff ,
35- 0x0003ff ,0x0047ff ,0x0066ff ,0x007eff ,0x0093ff ,
36- 0x00a6ff ,0x00b7ff ,0x00c8ff ,0x00d7ff ,0x00e5ff ,
37- 0x00e0ff ,0x00e6fd ,0x00ecf6 ,0x00f2ea ,0x00f6d7 ,
38- 0x00fac0 ,0x00fca3 ,0x00fe81 ,0x00ff59 ,0x00ff16 ,
39- 0x00ff16 ,0x45ff08 ,0x62ff00 ,0x78ff00 ,0x8bff00 ,
40- 0x9bff00 ,0xaaff00 ,0xb8ff00 ,0xc5ff00 ,0xd1ff00 ,
41- 0xedff00 ,0xf5eb00 ,0xfcd600 ,0xffc100 ,0xffab00 ,
42- 0xff9500 ,0xff7c00 ,0xff6100 ,0xff4100 ,0xff0000 ,
43- 0xff0000 ,0xff0000 ]
37+ heatmap = [
38+ 0xB000FF ,
39+ 0xA600FF ,
40+ 0x9B00FF ,
41+ 0x8F00FF ,
42+ 0x8200FF ,
43+ 0x7400FF ,
44+ 0x6500FF ,
45+ 0x5200FF ,
46+ 0x3900FF ,
47+ 0x0003FF ,
48+ 0x0003FF ,
49+ 0x0047FF ,
50+ 0x0066FF ,
51+ 0x007EFF ,
52+ 0x0093FF ,
53+ 0x00A6FF ,
54+ 0x00B7FF ,
55+ 0x00C8FF ,
56+ 0x00D7FF ,
57+ 0x00E5FF ,
58+ 0x00E0FF ,
59+ 0x00E6FD ,
60+ 0x00ECF6 ,
61+ 0x00F2EA ,
62+ 0x00F6D7 ,
63+ 0x00FAC0 ,
64+ 0x00FCA3 ,
65+ 0x00FE81 ,
66+ 0x00FF59 ,
67+ 0x00FF16 ,
68+ 0x00FF16 ,
69+ 0x45FF08 ,
70+ 0x62FF00 ,
71+ 0x78FF00 ,
72+ 0x8BFF00 ,
73+ 0x9BFF00 ,
74+ 0xAAFF00 ,
75+ 0xB8FF00 ,
76+ 0xC5FF00 ,
77+ 0xD1FF00 ,
78+ 0xEDFF00 ,
79+ 0xF5EB00 ,
80+ 0xFCD600 ,
81+ 0xFFC100 ,
82+ 0xFFAB00 ,
83+ 0xFF9500 ,
84+ 0xFF7C00 ,
85+ 0xFF6100 ,
86+ 0xFF4100 ,
87+ 0xFF0000 ,
88+ 0xFF0000 ,
89+ 0xFF0000 ,
90+ ]
4491
4592# size of the FFT data sample
4693fft_size = 64
4794
4895# setup for onboard mic
49- mic = audiobusio .PDMIn (board .MICROPHONE_CLOCK , board .MICROPHONE_DATA ,
50- sample_rate = 16000 , bit_depth = 16 )
96+ mic = audiobusio .PDMIn (
97+ board .MICROPHONE_CLOCK , board .MICROPHONE_DATA , sample_rate = 16000 , bit_depth = 16
98+ )
5199
52100# use some extra sample to account for the mic startup
53- samples_bit = array .array ('H' , [0 ] * (fft_size + 3 ))
101+ samples_bit = array .array ("H" , [0 ] * (fft_size + 3 ))
54102
55103# sends visualized data to the RGB matrix with colors
56104def waves (data , y ):
57- offset = max (0 , (13 - len (data ))// 2 )
105+ offset = max (0 , (13 - len (data )) // 2 )
58106
59107 for x in range (min (13 , len (data ))):
60- is31 .pixel (x + offset , y , heatmap [int (data [x ])])
108+ is31 .pixel (x + offset , y , heatmap [int (data [x ])])
109+
61110
62111# main loop
63112def main ():
@@ -78,18 +127,18 @@ def main():
78127 # spectrum() is always nonnegative, but add a tiny value
79128 # to change any zeros to nonzero numbers
80129 spectrogram1 = np .log (spectrogram1 + 1e-7 )
81- spectrogram1 = spectrogram1 [1 : (fft_size // 2 ) - 1 ]
130+ spectrogram1 = spectrogram1 [1 : (fft_size // 2 ) - 1 ]
82131 # sets range of the spectrogram
83132 min_curr = np .min (spectrogram1 )
84133 max_curr = np .max (spectrogram1 )
85134 # resets values
86135 if max_curr > max_all :
87136 max_all = max_curr
88137 else :
89- max_curr = max_curr - 1
138+ max_curr = max_curr - 1
90139 min_curr = max (min_curr , 3 )
91140 # stores spectrogram in data
92- data = (spectrogram1 - min_curr ) * (51. / (max_all - min_curr ))
141+ data = (spectrogram1 - min_curr ) * (51.0 / (max_all - min_curr ))
93142 # sets negative numbers to zero
94143 data = data * np .array ((data > 0 ))
95144 # resets y
@@ -101,4 +150,5 @@ def main():
101150 # writes data to the RGB matrix
102151 is31 .show ()
103152
153+
104154main ()
0 commit comments