ploopyco / headphones

A set of 3D-printed headphones, alongside a DAC/amp/EQ board powered by a Raspberry Pico.
833 stars 43 forks source link

Fix off by one error in fix_28_t #24

Closed george-norton closed 10 months ago

george-norton commented 10 months ago

I believe there is an out by one error in the sample normalization code, it doesn't account for the sign bit in the sample.

The min sample value is 0x8000 (-32768) and the max value is 0x7FFF (32767), these should map to fixed point values 0xF0000000 (-1) and 0x0FFFE000 (0.999969482) but currently they end up as 0xF8000000 (−0.5) and 0x7FFF000 (0.499984741). In practice this doesn't have much of an effect as when converting back to a sample we also take the wrong bits, but numbers which originated as floats/doubles are twice as big as expected relative to the samples. I suspect there is no major effect with the filter coefficients, but the preamp scaling is probably a bit off.

george-norton commented 10 months ago

Hmm, having written all that maybe the mapping to -0.5..0.5 makes more sense..

nyabinary commented 10 months ago

What is the status of this?

george-norton commented 10 months ago

I think it is the beginning of a discussion rather than something that will be merged soon. I am not clear what the correct behaviour is, and I hear clipping with the change applied, so for sure the preamp needs tweaking.

nyabinary commented 10 months ago

@george-norton Hmm the firmware in general needs optimization IMO, rewrite it in Rust ;), I'm joking but to really take advantage of all the power the headphones have packed into them there needs to be optimizations overall.

ploopyco commented 10 months ago

I believe there is an out by one error in the sample normalization code, it doesn't account for the sign bit in the sample.

The min sample value is 0x8000 (-32768) and the max value is 0x7FFF (32767), these should map to fixed point values 0xF0000000 (-1) and 0x0FFFE000 (0.999969482) but currently they end up as 0xF8000000 (−0.5) and 0x7FFF000 (0.499984741). In practice this doesn't have much of an effect as when converting back to a sample we also take the wrong bits, but numbers which originated as floats/doubles are twice as big as expected relative to the samples. I suspect there is no major effect with the filter coefficients, but the preamp scaling is probably a bit off.

I think your reasoning makes sense. I also just have this haunting feeling in the back of my head that, for some reason, this might cause distortion. I don't know why. I'll give it a try and see what I find.

george-norton commented 10 months ago

Yes, I am hearing clipping. Not looked into it yet, not sure if it is just the preamp that needs fixing up, or if the filters really are producing values that overflow at some point..