Skip to content

Commit f5db43e

Browse files
committed
fixed ulab spectrogram imports
1 parent 527a999 commit f5db43e

File tree

5 files changed

+705
-130
lines changed
  • EyeLights_Audio_Spectrum/EyeLights_Audio_Spectrum_CircuitPython
  • Feather_Sense_Audio_Visualizer_13x9_RGB_LED_Matrix
  • Ukulele
  • ulab_Crunch_Numbers_Fast/waterfall

5 files changed

+705
-130
lines changed

EyeLights_Audio_Spectrum/EyeLights_Audio_Spectrum_CircuitPython/code.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
from adafruit_is31fl3741.adafruit_ledglasses import LED_Glasses
1919
from rainbowio import colorwheel
2020
from ulab import numpy as np
21-
from ulab.scipy.signal import spectrogram
2221

22+
try:
23+
from ulab.utils import spectrogram
24+
except ImportError:
25+
from ulab.scipy.signal import spectrogram
2326

2427
# FFT/SPECTRUM CONFIG ----
2528

Feather_Sense_Audio_Visualizer_13x9_RGB_LED_Matrix/audio_spectrum_lightshow/code.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
from adafruit_is31fl3741.adafruit_rgbmatrixqt import Adafruit_RGBMatrixQT
1919
from rainbowio import colorwheel
2020
from ulab import numpy as np
21-
# if using CP7 and below:
22-
from ulab.scipy.signal import spectrogram
23-
# if using CP8 and above:
24-
# from ulab.utils import spectrogram
2521

22+
try:
23+
from ulab.utils import spectrogram
24+
except ImportError:
25+
from ulab.scipy.signal import spectrogram
2626

2727
# FFT/SPECTRUM CONFIG ----
2828

@@ -40,7 +40,7 @@
4040
i2c = I2C(board.SCL, board.SDA, frequency=1000000)
4141

4242
# Initialize the IS31 LED driver, buffered for smoother animation
43-
#glasses = LED_Glasses(i2c, allocate=adafruit_is31fl3741.MUST_BUFFER)
43+
# glasses = LED_Glasses(i2c, allocate=adafruit_is31fl3741.MUST_BUFFER)
4444
glasses = Adafruit_RGBMatrixQT(i2c, allocate=adafruit_is31fl3741.MUST_BUFFER)
4545

4646
glasses.show() # Clear any residue on startup
@@ -152,7 +152,7 @@
152152

153153
# Apply vertical scale to spectrum data. Results may exceed
154154
# matrix height...that's OK, adds impact!
155-
#data = (spectrum - lower) * (7 / (dynamic_level - lower))
155+
# data = (spectrum - lower) * (7 / (dynamic_level - lower))
156156
data = (spectrum - lower) * ((glasses.height + 2) / (dynamic_level - lower))
157157

158158
for column, element in enumerate(column_table):

Feather_Sense_Audio_Visualizer_13x9_RGB_LED_Matrix/waterfall_visualizer/code.py

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
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
66
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 """
88

99
import array
1010
import board
1111
import audiobusio
1212
import busio
1313
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
1519
import adafruit_is31fl3741
1620
from adafruit_is31fl3741.adafruit_rgbmatrixqt import Adafruit_RGBMatrixQT
1721

@@ -30,34 +34,79 @@
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
4693
fft_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
56104
def 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
63112
def 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+
104154
main()

0 commit comments

Comments
 (0)