audio-reactive-led-strip icon indicating copy to clipboard operation
audio-reactive-led-strip copied to clipboard

Optimization improvements for the raspberry pi?

Open iceonfire1 opened this issue 6 years ago • 6 comments

This project is great, super fun to use / play with. The major issue I encountered was getting a decent fps on the raspberry pi.

I ran the code through the basic python profiler and noticed these lines from _update_pi() in led.py was taking a lot of time:

if np.array_equal(p[:, i], _prev_pixels[:, i]): continue

Commenting it out, as:

# Update the pixels for i in range(config.N_PIXELS): # Ignore pixels if they haven't changed (saves bandwidth) #if np.array_equal(p[:, i], _prev_pixels[:, i]): # continue strip._led_data[i] = rgb[i] # _prev_pixels = np.copy(p) _prev_pixels = p

(sorry about the line indents, IDK how this comment formatting works)

Took me from ~5fps -> ~18fps on the pi zero w and from ~20 to >60fps on the pi 3b+, with 240 led's.

I'm assuming the comment is about network bandwidth and this step is unnecessary for the pi, since the led's are all updated in each step anyway. Can anyone confirm this, though?

Does anyone else have suggestions for optimizations for the pi? It'd be pretty cool if I could use a zero w instead of a 3b+ for this.

iceonfire1 avatar Jul 30 '18 14:07 iceonfire1

This worked for me pretty good. I‘m using a RPi 3 running on 70fps without problems (before max. 25 FPS).

andrealbers avatar Nov 22 '18 17:11 andrealbers

dude!!! thats amazing. I just went from <10 fps to 30 fps on the Pi Zero.

dkaulukukui avatar Dec 22 '18 20:12 dkaulukukui

I think you're right, seems like everyone has had better results with this who has tried.

It appears that that line is just to say essentially if the array of pixels is the same as last time then don't send it, but I could see this causing a computational overhead to replace the network overhead.

joeybab3 avatar Feb 11 '19 06:02 joeybab3

`def _update_pi(): """Writes new LED values to the Raspberry Pi's LED strip

Raspberry Pi uses the rpi_ws281x to control the LED strip directly.
This function updates the LED strip with new values.
"""
global pixels, _prev_pixels
# Truncate values and cast to integer
pixels = np.clip(pixels, 0, 255).astype(int)
# Optional gamma correction
p = _gamma[pixels] if config.SOFTWARE_GAMMA_CORRECTION else np.copy(pixels)
# Encode 24-bit LED values in 32 bit integers
r = np.left_shift(p[0][:].astype(int), 8)
g = np.left_shift(p[1][:].astype(int), 16)
b = p[2][:].astype(int)
rgb = np.bitwise_or(np.bitwise_or(r, g), b)
# Update the pixels
for i in range(config.N_PIXELS):
    # Ignore pixels if they haven't changed (saves bandwidth)
    # Update the pixels 
	for i in range(config.N_PIXELS): 
		# Ignore pixels if they haven't changed (saves bandwidth) 
		#if np.array_equal(p[:, i], _prev_pixels[:, i]): 
		# continue 
		strip._led_data[i] = rgb[i] 
		# _prev_pixels = np.copy(p) 
		_prev_pixels = p
    	strip._led_data[i] = rgb[i]
_prev_pixels = np.copy(p)
strip.show()`

I have this in my led.py but the fps rate is decreasing. Can someone help me?

meesvandenberg avatar Apr 08 '19 18:04 meesvandenberg

I can confirm it too. Went from 3-4 fps to 11-12 with 144 LEDs.

senchden avatar Aug 02 '20 21:08 senchden

I just try this in my Pi Zero but I am still getting 4-5 FPS. Could you please check if I am doing something wrong? `def _update_pi(): """Writes new LED values to the Raspberry Pi's LED strip

Raspberry Pi uses the rpi_ws281x to control the LED strip directly.
This function updates the LED strip with new values.
"""
global pixels, _prev_pixels
# Truncate values and cast to integer
pixels = np.clip(pixels, 0, 255).astype(int)
# Optional gamma correction
p = _gamma[pixels] if config.SOFTWARE_GAMMA_CORRECTION else np.copy(pixels)
# Encode 24-bit LED values in 32 bit integers
r = np.left_shift(p[0][:].astype(int), 8)
g = np.left_shift(p[1][:].astype(int), 16)
b = p[2][:].astype(int)
rgb = np.bitwise_or(np.bitwise_or(r, g), b)
# Update the pixels
for i in range(config.N_PIXELS):
    # Ignore pixels if they haven't changed (saves bandwidth)
    #if np.array_equal(p[:, i], _prev_pixels[:, i]):
    #    continue
        
    strip._led_data[i] = int(rgb[i])
#_prev_pixels = np.copy(p)
_prev_pixels = p
strip.show()`

lflondonol92 avatar Sep 14 '20 16:09 lflondonol92