whats a good way to represent a waveform within a certain height range
I'm happily drawing waveforms to screen from pcm data. I have a problem where occassionally the waveforms height will exceed the height of the display area height.
How can I ensure that the waveform plotting data will never exceed a determined height without having to rip through the entire set开发者_Go百科 of pcm data and normalizing from the maximum value found?
using a normalized representation is exactly what you would do.
you could cheat and pre-calculate max values for a given range, if that's a constraint offered by the implementation.
Unfortunately, there is no good way to discover the actual maximum of a signal without going through sample by sample and finding it.
If you know the number of bits in the PCM samples, you can assume the scaling will be bounded by [-2^(bits-1), 2^(bits-1)-1]
. That will be the absolute highest and lowest the signal can go. However, this is the most pessimistic scaling - if you have a 16-bit signal that never goes outside the range [-1024,1024]
, for instance, you're giving up a lot of display area (as well as ADC dynamic range, but that's another story).
If you are willing to dynamically scale the signal, you can simply make the graph scale bigger each time your signal would get clipped. A more sophisticated approach would be to upscale as necessary, but then slowly relax the max scale downwards over time. A good way to relax the max scale is exponential decay, like multiply the max scale by .98 (or some other number < 1) on each iteration.
精彩评论