def main(): max_all = 10 while True: mic.record(samples_bit, len(samples_bit)) samples = np.array(samples_bit[3:]) spectrogram1 = spectrogram(samples) # 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] min_curr = np.min(spectrogram1) max_curr = np.max(spectrogram1) if max_curr > max_all: max_all = max_curr else: max_curr = max_curr - 1 print(min_curr, max_all) min_curr = max(min_curr, 3) # Plot FFT data = (spectrogram1 - min_curr) * (51. / (max_all - min_curr)) # This clamps any negative numbers to zero data = data * np.array((data > 0)) graph.show(data)
def main(): # value for audio samples max_all = 10 # variable to move data along the matrix scroll_offset = 0 # setting the y axis value to equal the scroll_offset y = scroll_offset while True: # record the audio sample mic.record(samples_bit, len(samples_bit)) # send the sample to the ulab array samples = np.array(samples_bit[3:]) # creates a spectogram of the data spectrogram1 = spectrogram(samples) # 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] # 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 min_curr = max(min_curr, 3) # stores spectrogram in data data = (spectrogram1 - min_curr) * (51. / (max_all - min_curr)) # sets negative numbers to zero data = data * np.array((data > 0)) # resets y y = scroll_offset # runs waves to write data to the LED's waves(data, y) # updates scroll_offset to move data along matrix scroll_offset = (y + 1) % 9 # writes data to the RGB matrix is31.show()
import math try: from ulab import numpy as np except ImportError: import numpy as np print("Testing np.min:") print(np.min([1])) print(np.min(np.array([1], dtype=np.float))) a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.uint8) print(np.min(a)) print(np.min(a, axis=0)) print(np.min(a, axis=1)) a = np.array([range(255 - 5, 255), range(240 - 5, 240), range(250 - 5, 250)], dtype=np.uint8) print(np.min(a)) print(np.min(a, axis=0)) print(np.min(a, axis=1)) a = np.array([range(255 - 5, 255), range(240 - 5, 240), range(250 - 5, 250)], dtype=np.int8) print(np.min(a)) ## Problem here print(np.min(a, axis=0)) print(np.min(a, axis=1)) a = np.array([range(255 - 5, 255), range(240 - 5, 240), range(250 - 5, 250)], dtype=np.uint16)
# encounter an error when accessing the LED driver, whether from bumping # around the wires or sometimes an I2C device just gets wedged. To more # robustly handle the latter, the code will restart if that happens. try: mic.record(rec_buf, fft_size) # Record batch of 16-bit samples samples = np.array(rec_buf) # Convert to ndarray # Compute spectrogram and trim results. Only the left half is # normally needed (right half is mirrored), but we trim further as # only the low_bin to high_bin elements are interesting to graph. spectrum = spectrogram(samples)[low_bin:high_bin + 1] # Linearize spectrum output. spectrogram() is always nonnegative, # but add a tiny value to change any zeros to nonzero numbers # (avoids rare 'inf' error) spectrum = np.log(spectrum + 1e-7) # Determine minimum & maximum across all spectrum bins, with limits lower = max(np.min(spectrum), 4) upper = min(max(np.max(spectrum), lower + 6), 20) # Adjust dynamic level to current spectrum output, keeps the graph # 'lively' as ambient volume changes. Sparkle but don't saturate. if upper > dynamic_level: # Got louder. Move level up quickly but allow initial "bump." dynamic_level = upper * 0.7 + dynamic_level * 0.3 else: # Got quieter. Ease level down, else too many bumps. dynamic_level = dynamic_level * 0.5 + lower * 0.5 # Apply vertical scale to spectrum data. Results may exceed # matrix height...that's OK, adds impact! data = (spectrum - lower) * (7 / (dynamic_level - lower))
import time, gc, os from ulab import numpy as np from machine import PIN digi = Pin(33, Pin.IN) pot = ADC(Pin(32)) pot.atten(ADC.ATTN_11DB) pot.width(ADC.WIDTH_11BIT) TIMING = [0] * 50 vals = [0] * 50 ticks = 0 while True: ticks = ticks + 1 TIMING[ticks % len(TIMING)] = time.ticks_us() vals[ticks % len(TIMING)] = pot.read() if time.ticks_us() - ticks >= 0: pass if ticks > 100000: break a = 1000 * 1000 * len(TIMING) / ((np.max(TIMING) - np.min(TIMING)))
def standardise(X): axis = np.argmax(X.shape) minor_shape = np.min(X.shape) mu = np.mean(X, axis=axis).reshape((minor_shape, 1)) sigma = np.std(X, axis=axis).reshape((minor_shape, 1)) return (X - mu) / sigma