Skip to content

Commit

Permalink
Merge pull request #2336 from adafruit/ulab-spectrogram
Browse files Browse the repository at this point in the history
fixed ulab spectrogram imports
  • Loading branch information
jepler authored Jan 20, 2024
2 parents b36c88a + 79a9e0f commit 4033e68
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
from adafruit_is31fl3741.adafruit_ledglasses import LED_Glasses
from rainbowio import colorwheel
from ulab import numpy as np
from ulab.scipy.signal import spectrogram

try:
from ulab.utils import spectrogram
except ImportError:
from ulab.scipy.signal import spectrogram

# FFT/SPECTRUM CONFIG ----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
from adafruit_is31fl3741.adafruit_rgbmatrixqt import Adafruit_RGBMatrixQT
from rainbowio import colorwheel
from ulab import numpy as np
# if using CP7 and below:
from ulab.scipy.signal import spectrogram
# if using CP8 and above:
# from ulab.utils import spectrogram

try:
from ulab.utils import spectrogram
except ImportError:
from ulab.scipy.signal import spectrogram

# FFT/SPECTRUM CONFIG ----

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

# Initialize the IS31 LED driver, buffered for smoother animation
#glasses = LED_Glasses(i2c, allocate=adafruit_is31fl3741.MUST_BUFFER)
# glasses = LED_Glasses(i2c, allocate=adafruit_is31fl3741.MUST_BUFFER)
glasses = Adafruit_RGBMatrixQT(i2c, allocate=adafruit_is31fl3741.MUST_BUFFER)

glasses.show() # Clear any residue on startup
Expand Down Expand Up @@ -152,7 +152,7 @@

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

for column, element in enumerate(column_table):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@
#
# SPDX-License-Identifier: MIT

'''Adapted from the FFT Example: Waterfall Spectrum Analyzer
"""Adapted from the FFT Example: Waterfall Spectrum Analyzer
by Jeff Epler
https://learn.adafruit.com/ulab-crunch-numbers-fast-with-circuitpython/overview '''
https://learn.adafruit.com/ulab-crunch-numbers-fast-with-circuitpython/overview """

import array
import board
import audiobusio
import busio
from ulab import numpy as np
from ulab.scipy.signal import spectrogram

try:
from ulab.utils import spectrogram
except ImportError:
from ulab.scipy.signal import spectrogram
import adafruit_is31fl3741
from adafruit_is31fl3741.adafruit_rgbmatrixqt import Adafruit_RGBMatrixQT

Expand All @@ -27,6 +31,7 @@
is31.global_current = 0x03
is31.enable = True

# fmt: off
# array of colors for the LEDs
# goes from purple to red
# gradient generated using https://colordesigner.io/gradient-generator
Expand All @@ -41,23 +46,26 @@
0xedff00,0xf5eb00,0xfcd600,0xffc100,0xffab00,
0xff9500,0xff7c00,0xff6100,0xff4100,0xff0000,
0xff0000,0xff0000]
# fmt: on

# size of the FFT data sample
fft_size = 64

# setup for onboard mic
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
sample_rate=16000, bit_depth=16)
mic = audiobusio.PDMIn(
board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16
)

# use some extra sample to account for the mic startup
samples_bit = array.array('H', [0] * (fft_size+3))
samples_bit = array.array("H", [0] * (fft_size + 3))

# sends visualized data to the RGB matrix with colors
def waves(data, y):
offset = max(0, (13-len(data))//2)
offset = max(0, (13 - len(data)) // 2)

for x in range(min(13, len(data))):
is31.pixel(x+offset, y, heatmap[int(data[x])])
is31.pixel(x + offset, y, heatmap[int(data[x])])


# main loop
def main():
Expand All @@ -78,18 +86,18 @@ def main():
# spectrum() is always nonnegative, but add a tiny value
# to change any zeros to nonzero numbers
spectrogram1 = np.log(spectrogram1 + 1e-7)
spectrogram1 = spectrogram1[1:(fft_size//2)-1]
spectrogram1 = spectrogram1[1 : (fft_size // 2) - 1]
# sets range of the spectrogram
min_curr = np.min(spectrogram1)
max_curr = np.max(spectrogram1)
# resets values
if max_curr > max_all:
max_all = max_curr
else:
max_curr = max_curr-1
max_curr = max_curr - 1
min_curr = max(min_curr, 3)
# stores spectrogram in data
data = (spectrogram1 - min_curr) * (51. / (max_all - min_curr))
data = (spectrogram1 - min_curr) * (51.0 / (max_all - min_curr))
# sets negative numbers to zero
data = data * np.array((data > 0))
# resets y
Expand All @@ -101,4 +109,5 @@ def main():
# writes data to the RGB matrix
is31.show()


main()
65 changes: 36 additions & 29 deletions Ukulele/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
import audiobusio
import board
import neopixel
from ulab.scipy.signal import spectrogram

try:
from ulab.utils import spectrogram
except ImportError:
from ulab.scipy.signal import spectrogram
from ulab import numpy as np
from rainbowio import colorwheel
from adafruit_lsm6ds import lsm6ds33
Expand All @@ -46,11 +50,11 @@
WHITE,
)

MAX_BRIGHTNESS = 0.3 #set max brightness for sound reactive mode
NORMAL_BRIGHTNESS = 0.1 #set brightness for non-reactive mode
VOLUME_CALIBRATOR = 50 #multiplier for brightness mapping
ROCKSTAR_TILT_THRESHOLD = 200 #shake threshold
SOUND_THRESHOLD = 430000 #main strum or pluck threshold
MAX_BRIGHTNESS = 0.3 # set max brightness for sound reactive mode
NORMAL_BRIGHTNESS = 0.1 # set brightness for non-reactive mode
VOLUME_CALIBRATOR = 50 # multiplier for brightness mapping
ROCKSTAR_TILT_THRESHOLD = 200 # shake threshold
SOUND_THRESHOLD = 430000 # main strum or pluck threshold

# Set to the length in seconds for the animations
POWER_ON_DURATION = 1.3
Expand All @@ -72,11 +76,11 @@
pixels.show()


#PIXEL MAPS: Used for reordering pixels so the animations can run in different configurations.
#My LED strips inside the neck are accidentally swapped left-right,
#so these maps also correct for that

# PIXEL MAPS: Used for reordering pixels so the animations can run in different configurations.
# My LED strips inside the neck are accidentally swapped left-right,
# so these maps also correct for that

# fmt: off
#Bottom up along both sides at once
pixel_map_reverse = PixelMap(pixels, [
0, 103, 1, 102, 2, 101, 3, 100, 4, 99, 5, 98, 6, 97, 7, 96, 8, 95, 9, 94, 10,
Expand Down Expand Up @@ -122,6 +126,7 @@
83, 22, 81, 24, 79, 26, 77, 29, 74, 31, 72, 33, 70, 35, 68, 37, 66, 39, 64, 41,
62, 43, 60, 45, 58, 47, 56, 49, 54, 51, 52,
], individual_pixels=True)
# fmt: on

pixel_map = [
pixel_map_reverse,
Expand All @@ -131,15 +136,15 @@
pixel_map_skip,
]

#Set up accelerometer & mic
# Set up accelerometer & mic
sensor = lsm6ds33.LSM6DS33(i2c)
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK,
board.MICROPHONE_DATA,
sample_rate=16000,
bit_depth=16)
mic = audiobusio.PDMIn(
board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16
)

NUM_SAMPLES = 256
samples_bit = array.array('H', [0] * (NUM_SAMPLES+3))
samples_bit = array.array("H", [0] * (NUM_SAMPLES + 3))


def power_on(duration):
"""
Expand All @@ -152,6 +157,7 @@ def power_on(duration):
break # Stop animating
powerup.animate()


def rockstar_tilt(duration):
"""
Tilt animation - lightning effect with a rotating color
Expand Down Expand Up @@ -182,6 +188,7 @@ def rockstar_tilt(duration):
pixels.show()
time.sleep(0.03)


# Cusomize LED Animations ------------------------------------------------------
powerup = RainbowComet(pixel_map[3], speed=0, tail_length=25, bounce=False)
rainbow = Rainbow(pixel_map[4], speed=0, period=6, name="rainbow", step=2.4)
Expand All @@ -191,13 +198,13 @@ def rockstar_tilt(duration):
rainbow_comet = RainbowComet(pixel_map[2], speed=0, tail_length=80, bounce=True)
rainbow_comet2 = RainbowComet(
pixel_map[0], speed=0, tail_length=104, colorwheel_offset=80, bounce=True
)
)
rainbow_comet3 = RainbowComet(
pixel_map[1], speed=0, tail_length=25, colorwheel_offset=80, step=4, bounce=False
)
)
strum = RainbowComet(
pixel_map[3], speed=0, tail_length=25, bounce=False, colorwheel_offset=50, step=4
)
)
lava = Comet(pixel_map[3], speed=0.01, color=ORANGE, tail_length=40, bounce=False)
sparkle = Sparkle(pixel_map[4], speed=0.01, color=BLUE, num_sparkles=10)
sparkle2 = Sparkle(pixel_map[1], speed=0.05, color=PURPLE, num_sparkles=4)
Expand All @@ -214,18 +221,18 @@ def rockstar_tilt(duration):
AnimationGroup(
sparkle,
strum,
),
),
AnimationGroup(
sparkle2,
rainbow_comet3,
),
),
auto_clear=True,
auto_reset=True,
)


MODE = 0
LASTMODE = 1 # start up in sound reactive mode
LASTMODE = 1 # start up in sound reactive mode
i = 0

# Main loop
Expand All @@ -246,9 +253,9 @@ def rockstar_tilt(duration):
spectrum[1] = 0
peak_idx = np.argmax(spectrum)
peak_freq = peak_idx * 16000 / 256
# print((peak_idx, peak_freq, spectrum[peak_idx]))
# print((peak_idx, peak_freq, spectrum[peak_idx]))
magnitude = spectrum[peak_idx]
# time.sleep(1)
# time.sleep(1)
if peak_freq == 812.50 and magnitude > SOUND_THRESHOLD:
animations.next()
time.sleep(1)
Expand All @@ -263,20 +270,20 @@ def rockstar_tilt(duration):
print("mode = 1")
LASTMODE = 1
time.sleep(1)
# Read accelerometer
# Read accelerometer
x, y, z = sensor.acceleration
accel_total = x * x + y * y # x=tilt, y=rotate
# print (accel_total)
accel_total = x * x + y * y # x=tilt, y=rotate
# print (accel_total)
if accel_total > ROCKSTAR_TILT_THRESHOLD:
MODE = 3
print("Tilted: ", accel_total)
if MODE == 1:
VOLUME = magnitude / (VOLUME_CALIBRATOR * 100000)
if VOLUME > MAX_BRIGHTNESS:
VOLUME = MAX_BRIGHTNESS
# print(VOLUME)
# print(VOLUME)
pixels.brightness = VOLUME
# time.sleep(2)
# time.sleep(2)
animations.animate()
elif MODE == 2:
pixels.brightness = NORMAL_BRIGHTNESS
Expand Down
8 changes: 7 additions & 1 deletion ulab_Crunch_Numbers_Fast/waterfall/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
import audiobusio
import displayio
from ulab import numpy as np
from ulab.scipy.signal import spectrogram

try:
from ulab.utils import spectrogram
except ImportError:
from ulab.scipy.signal import spectrogram

display = board.DISPLAY

# Create a heatmap color palette
palette = displayio.Palette(52)
# fmt: off
for i, pi in enumerate((0xff0000, 0xff0a00, 0xff1400, 0xff1e00,
0xff2800, 0xff3200, 0xff3c00, 0xff4600,
0xff5000, 0xff5a00, 0xff6400, 0xff6e00,
Expand All @@ -31,6 +36,7 @@
0x00a4ff, 0x0094ff, 0x0084ff, 0x0074ff,
0x0064ff, 0x0054ff, 0x0044ff, 0x0032ff,
0x0022ff, 0x0012ff, 0x0002ff, 0x0000ff)):
# fmt: on
palette[51-i] = pi

class RollingGraph(displayio.TileGrid):
Expand Down

0 comments on commit 4033e68

Please sign in to comment.