2
2
#
3
3
# SPDX-License-Identifier: MIT
4
4
5
- ''' Adapted from the FFT Example: Waterfall Spectrum Analyzer
5
+ """ Adapted from the FFT Example: Waterfall Spectrum Analyzer
6
6
by 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 """
8
8
9
9
import array
10
10
import board
11
11
import audiobusio
12
12
import busio
13
13
from 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
15
19
import adafruit_is31fl3741
16
20
from adafruit_is31fl3741 .adafruit_rgbmatrixqt import Adafruit_RGBMatrixQT
17
21
30
34
# array of colors for the LEDs
31
35
# goes from purple to red
32
36
# 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
+ ]
44
91
45
92
# size of the FFT data sample
46
93
fft_size = 64
47
94
48
95
# 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
+ )
51
99
52
100
# 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 ))
54
102
55
103
# sends visualized data to the RGB matrix with colors
56
104
def waves (data , y ):
57
- offset = max (0 , (13 - len (data ))// 2 )
105
+ offset = max (0 , (13 - len (data )) // 2 )
58
106
59
107
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
+
61
110
62
111
# main loop
63
112
def main ():
@@ -78,18 +127,18 @@ def main():
78
127
# spectrum() is always nonnegative, but add a tiny value
79
128
# to change any zeros to nonzero numbers
80
129
spectrogram1 = np .log (spectrogram1 + 1e-7 )
81
- spectrogram1 = spectrogram1 [1 : (fft_size // 2 ) - 1 ]
130
+ spectrogram1 = spectrogram1 [1 : (fft_size // 2 ) - 1 ]
82
131
# sets range of the spectrogram
83
132
min_curr = np .min (spectrogram1 )
84
133
max_curr = np .max (spectrogram1 )
85
134
# resets values
86
135
if max_curr > max_all :
87
136
max_all = max_curr
88
137
else :
89
- max_curr = max_curr - 1
138
+ max_curr = max_curr - 1
90
139
min_curr = max (min_curr , 3 )
91
140
# 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 ))
93
142
# sets negative numbers to zero
94
143
data = data * np .array ((data > 0 ))
95
144
# resets y
@@ -101,4 +150,5 @@ def main():
101
150
# writes data to the RGB matrix
102
151
is31 .show ()
103
152
153
+
104
154
main ()
0 commit comments