Skip to content

Human-eye linear dimming for PWM strips #2767

Closed

Description

@mcer12

PWM dimming does not look linear to human eye. Not much we can do in terms of addressable strips but it should be very simple to add a lookup table for PWM strips, as a optional setting. This would make low brightness dimming tremendously better and smooth!

Enabling this option would:

  1. set PWM resolution to 12bit (should be enough)
  2. add lookup table for 8bit
  3. lowering PWM frequency a bit might be required for this

Activity

  1. mcer12 commented on Aug 28, 2022

    @mcer12
    Author

    Here's a great article about this with a python script to generate the lookup table. I personally used this for one of my projects and it's really awesome in practice. It's day and night difference. (don't mind the broken ssl certificate)
    https://jared.geek.nz/2013/feb/linear-led-pwm

  2. Aircoookie commented on Sep 9, 2022

    @Aircoookie
    Member

    Thank you for the idea!
    Gamma correction with 2.5 gamma is actually really close to the CIE1931 formula (except that the latter gives a higher output at low brightness levels, which might be nice).
    In any case, it seems beneficial to go with a gamma of 2.5 instead of the current 2.8, as that aligns much closer to perceived brightness. Maybe we should swap the current gamma table for the CIE table entirely, though I'm not sure if that would improve or worsen low brightness hue accuracy.

    Regardless, I agree that a PWM resolution of 12 or even just 10 would help a lot with low brightness color accuracy.

    Capture

  3. mcer12 commented on Sep 29, 2022

    @mcer12
    Author

    @Aircoookie CIE1931 sounds like the proper way to do things. With addressable strips, it doesn't really matter which one we pick because on low brightness they are all useless anyway. From my personal projects, to archieve really smooth 255 step pwm dimming, we need 12bit table, 10bit sounds like it's good enough but it's really not, it's exponential so on brightness below 10 it will still be choppy. I guess it would be a sensible compromise if there were memory constraints but ESP32 and ESP8266 both should handle the calculation without lookup table if that's the case. Excuse me if some of the suggestions are already implemented since I am not familiar with the code yet.

  4. mcer12 commented on Sep 29, 2022

    @mcer12
    Author

    @Aircoookie It's worth a note that Philips Hue (and probably others) also use CIE1931 since it's an industry standard. So it should make WLED more accurate and compatible with other lights.

  5. softhack007 commented on Nov 10, 2022

    @softhack007
    Member

    Found this recently, where a lookup table is used for achieving LED brightness curves that look linear to the human eye.

    Looks easy:
    https://github.com/Zanduino/SmoothLED_8bit
    https://github.com/Zanduino/SmoothLED_10bit/wiki/CIE1931-Compensation
    https://github.com/Zanduino/SmoothLED_10bit

    A common misconception is that gamma correction should be used, as it does have a very similar response to the eye. However, gamma correction has nothing to do with how humans perceive light, it is just coincidence that it appears to work. The CIE 1931 lightness formula is what actually describes how we perceive light.

  6. mcer12 commented on Nov 10, 2022

    @mcer12
    Author

    @softhack007 I'm sorry but this is totally off topic, it's atmega code, useless with ESP. ESP32 does 12bit pwm natively so we don't need any interrupts, just a lookup table so it's even more simple here. It's possible to use https://github.com/StefanBruens/ESP8266_new_pwm for esp8266 but that would require reimplementation of pwm and it's not clear if it would be compatible with the rest of the WLED code (because of HW interrupts) so to me it makes sense to do this ESP32 exclusive which should be a minor change.

  7. softhack007 commented on Nov 10, 2022

    @softhack007
    Member

    @softhack007 I'm sorry but this is totally off topic, it's atmega code, useless with ESP.

    Why? Its essentially a lookup-table. That's not specific to atmega or ESP32. My idea was just to use this mapping table for "WLED brightness" -> PWM brightness, and use the resulting number for the PWM driver.

    If you think that's off-topic, no problem, just go ahead and don't let me disturb your circles...

    Edit: I was thinking about this table, not the rest of the code:
    https://github.com/Zanduino/SmoothLED_8bit/blob/8fc9b85842597b3af0eb4c3a8ef013306f15a737/src/SmoothLED.h#L77-L91
    or https://github.com/Zanduino/SmoothLED_10bit/blob/896d341e34c661b04d212d9e16965525d0f6cd46/src/SmoothLED.h#L88-L150

    a small script to generate a table is in the code comments, so it should be simple to make a similar static LUT for 12-bit or other resolutions.

  8. softhack007 commented on Nov 10, 2022

    @softhack007
    Member

    @mcer12 wrote

    Here's a great article about this with a python script to generate the lookup table. I personally used this for one of my projects and it's really awesome in practice. It's day and night difference. (don't mind the broken ssl certificate)
    https://jared.geek.nz/2013/feb/linear-led-pwm

    Seems i overlooked that you made a very similar proposal a few weeks ago. What you said was also totally off topic, I presume?

  9. mcer12 commented on Nov 10, 2022

    @mcer12
    Author

    @softhack007 No actually my issue is not off-topic and please don't be combatitive, it's not very productive. I would love to see how exactly the 8bit lookup table that you shared solves choppiness at low brightness. Because this is already how WLED works (gamma correction), just calculated without lookup table. What I proposed and what we're talking about here is 12bit lookup table with 255 values which results in prefectly smooth 8bit linear dimming even at low brightness.

  10. mcer12 commented on Nov 10, 2022

    @mcer12
    Author

    @softhack007 10bit makes it little bit better but doesn't solve the issue whatsoever. All you really have to do is take a look at first few values in the the lookup table (lowest brightness values) to see the issue.

  11. softhack007 commented on Nov 10, 2022

    @softhack007
    Member

    Hi @mcer12 ok, this was also what I had in mind. Thanks for explaining.

    Personally, I find it not very helpful to get slammed as "off-topic", and then see that the same idea was expressed "on-topic" already. But OK, i'm not angry about that any more. So let's forget it.

  12. mcer12 commented on Nov 10, 2022

    @mcer12
    Author

    @softhack007 mate if you had the same idea then why you're sharing a library that doesn't implement that idea? :D The library does exactly what WLED already does. If the library mapped 10bit values to 8bit, then that would have been a differnt conversation. If you took it as slamming then I'm sorry but you were off topic so I don't know what to tell you.

  13. softhack007 commented on Nov 10, 2022

    @softhack007
    Member

    Yes I can see your problem that you are facing.

    Just to clarify what's needed: is the question
    a) how to get from "WLED brightness" (8 bit) to "PWM" brightness (eg. 12 bit)
    b) get from higher resolution future WLED brightness (eg 12bit ) down to PWM 8 bit?

    Case b) does not seem very usefull, because the lost resolution (-4bit) combined with any kind d of calibration curve will lead to the same bad results in low brightness ranges as if you came from 8bit.
    In case a) finding the best PWM resolution that allows to preserve details in the low brightness ranges might be possible. In this case, I can imagine that existing scripts/formulas can be used to make a LUT, either reprenting each input value or use interpolation to make the LUT smaller.

  14. mcer12 commented on Nov 10, 2022

    @mcer12
    Author

    @softhack007 you're not explaining it very clearly, I guess that a) is probably correct. We need to generate LUT with 255 values 0-4095. Using 12bit to generate human-eye linear 8bit.

  15. 7 remaining items

  16. softhack007 commented on Nov 10, 2022

    @softhack007
    Member

    @mcer12 yes that looks promising. Even the the 8bit version seems to preserve more details in the lowest brightness values, compared to gamma. 👍

  17. Aircoookie commented on Nov 11, 2022

    @Aircoookie
    Member

    I would propose two things:

    • Swap current gamma 2.8 table either for gamma 2.5 (safe) or better, CIE if that has no visible negative impact on hue/saturation accuracy if used for colors. Alternatively, could use separate LUTs for brightness and color correction, but that would raise complexity a little.
    • Stay with 8 bit color handling for now, but use 12 bits for ESP32 analog out (10bit on esp8266? need to check what works since esp8266 PWM is bitbang essentially). Would still go a long way, since combining 8 bit color with 8 bit master brightness still gives us an effective resolution of 16 bits.

    What do you think?

  18. blazoncek commented on Nov 12, 2022

    @blazoncek
    Contributor

    I quickly checked the code and it doesn't look good. I expected that gamma is applied on low level right before pushing the pixel color / analogWrite.

    Didn't I say so? 😉

    One thing that comes to mind is to move gamma correction entirely to bus manager and make gamma checkboxes strip independent.
    Then it can be applied per-bus in an independent fashion, possibly using different LUT for different bus type.

    This would increase bus configuration complexity, though.

    But would also have the benefit of sending uncorrected color/brightness information to virtual LEDs over DDP to have them use different LUT if configured so.

    EDIT: Looks like I've done the separation already.

  19. blazoncek commented on Nov 12, 2022

    @blazoncek
    Contributor

    Re-checked the code.

    We have calcGammaTable() that will fill gamma LUT with new values calculated on the fly (not used anywhere ATM).
    If you do not mix analog & digital LEDs it could be done by just adjusting this function to account for CIE LUT.
    Possibly read from JSON file on startup.
    The only change needed here would be to change byte gammaT[] to uint16_t gammaT[] and add something like uint16_t maxGammaV to tell what is the maximum value in gamma LUT.

    Actual brightness for analog is calculated using C * _bri / 255, C being channel value and _bri being strip/global gamma adjusted brightness (in strip.setBrightness()).

    So if WS2812FX::_brightness and Bus::_bri are converted to uint16_t there is a possibility to use them in combination with calcGammaTable() to accommodate nicer brightness changes without major disruption of other code.

  20. added
    backburner
    on Oct 7, 2023
  21. blazoncek commented on Mar 7, 2024

    @blazoncek
    Contributor

    @mcer12 you are invited to test better low brightness PWM handling in 0.15-b1. Available on Discord only ATM.

  22. added
    fixed in source
    on Mar 7, 2024
  23. blazoncek commented on Mar 7, 2024

    @blazoncek
    Contributor

    Implemented in c0beb62

  24. blazoncek commented on Mar 22, 2024

    @blazoncek
    Contributor

    Available in 0.15.0 beta 1.

  25. mcer12 commented on Apr 8, 2024

    @mcer12
    Author

    @blazoncek I just tried it and it looks much better. Testing this on ESP32 board. From what I understand from your changes, only ESP32 variants use the LUT, ESP8266 uses the original gamma correction. That said, you seem to be using some other calculation, not CIE1931 because if I generate the same lut with CIE1931 formula, the tables don't match.

    EDIT: Just a suggestion, it would be good to add a little note to what calculation is used, so it's not just mystery values :)

  26. blazoncek commented on Apr 8, 2024

    @blazoncek
    Contributor

    That said, you seem to be using some other calculation, not CIE1931 because if I generate the same lut with CIE1931 formula, the tables don't match.

    EDIT: Just a suggestion, it would be good to add a little note to what calculation is used, so it's not just mystery values :)

    // using gamma 1.7 to extrapolate PWM

    This was the closest to CIE LUT, with decent performance and not using any RAM.

    EDIT: I was looking for a formula to get CIE values but was unable to find one. If you have one, you can make a PR and substitute gamma extrapolation.

  27. mcer12 commented on Apr 8, 2024

    @mcer12
    Author

    @blazoncek Sorry for the confusion... I was asking about the LUT as it looks different to what I just generated but now I see you actually used the LUT I shared in this thread.

    This is my newly generated lut, using the same script and it should be correct. Not sure why the original is different, maybe it's a rounding issue, no idea.

    const uint16_t cie[256] = {
    0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 18, 20, 21, 23, 25, 27,
    28, 30, 32, 34, 36, 37, 39, 41, 43, 45, 47, 49, 52, 54, 56, 59,
    61, 64, 66, 69, 72, 75, 77, 80, 83, 87, 90, 93, 96, 100, 103, 107,
    111, 115, 118, 122, 126, 131, 135, 139, 144, 148, 153, 157, 162, 167, 172, 177,
    182, 187, 193, 198, 204, 209, 215, 221, 227, 233, 239, 246, 252, 259, 265, 272,
    279, 286, 293, 300, 308, 315, 323, 330, 338, 346, 354, 362, 371, 379, 388, 396,
    405, 414, 423, 432, 442, 451, 461, 470, 480, 490, 501, 511, 521, 532, 543, 553,
    564, 576, 587, 598, 610, 622, 634, 646, 658, 670, 683, 695, 708, 721, 734, 748,
    761, 775, 788, 802, 816, 831, 845, 860, 874, 889, 904, 920, 935, 951, 966, 982,
    999, 1015, 1031, 1048, 1065, 1082, 1099, 1116, 1134, 1152, 1170, 1188, 1206, 1224, 1243, 1262,
    1281, 1300, 1320, 1339, 1359, 1379, 1399, 1420, 1440, 1461, 1482, 1503, 1525, 1546, 1568, 1590,
    1612, 1635, 1657, 1680, 1703, 1726, 1750, 1774, 1797, 1822, 1846, 1870, 1895, 1920, 1945, 1971,
    1996, 2022, 2048, 2074, 2101, 2128, 2155, 2182, 2209, 2237, 2265, 2293, 2321, 2350, 2378, 2407,
    2437, 2466, 2496, 2526, 2556, 2587, 2617, 2648, 2679, 2711, 2743, 2774, 2807, 2839, 2872, 2905,
    2938, 2971, 3005, 3039, 3073, 3107, 3142, 3177, 3212, 3248, 3283, 3319, 3356, 3392, 3429, 3466,
    3503, 3541, 3578, 3617, 3655, 3694, 3732, 3772, 3811, 3851, 3891, 3931, 3972, 4012, 4054, 4095,
    };

    Here's what you can use, just change resolution:
    https://gist.github.com/mathiasvr/19ce1d7b6caeab230934080ae1f1380e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backburnerdiscussionenhancementfixed in source

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions