draw signal plot

DSP, Plugin and Host development discussion.
Post Reply New Topic
RELATED
PRODUCTS

Post

hi,

what's the best way to draw a waveform plot having a limited amount of pixels?
i mean: a single sine cycle of 1 seconds is about 44100 samples (at @44.1 sr).
i don't have plot with 44100 samples of course, but (for example) 300.

in samplers plugin (when samples are already "rendered") i usually take the max peak within each step block.

but for synthesis signal this means render the whole signal and calculate those max values every time the signal change (i.e. sum of sins, different phases, etc), which become huge on cpu for a basic plot.

if i sin(t * step) and plot the signal (with step = f * sr / 300 and t going from 0 to 299), doesn't plot very well when freqs are higher.

how do you do this?

thanks, as usual

Post

Resample the signal and plot it.

Post

Do I understand right you plan to plot one second of audio? Then you don't see the waveform itself, but only the envelope. Correct?

When exactly do you plot? If it is while producing audio, then it's trivial: graphics are derived from the real audio. So I assume you plot ahead of time...
We are the KVR collective. Resistance is futile. You will be assimilated. Image
My MusicCalc is served over https!!

Post

you misunderstand :) i want to plot the waveform of the generated signal within a synth. 1 sec was just an idea (i.e. render It with fondamental at 1hz).

such Sytrus osc plot: https://howtomakeelectronicmusic.com/wp ... Sytrus.jpg

i.e. i don't need to analyse incoming audio, rather show how the waveform will be (with "decent" precision) :)

Post

Say you need to produce a 100 Hz tone at a 44.1kHz sampling rate. Can your synth do that? For how many voices until cpu can't take it?

What is the difference with rendering 441 samples containing one full cycle, but to be displayed?

Me thinks you're over-thinking it...
We are the KVR collective. Resistance is futile. You will be assimilated. Image
My MusicCalc is served over https!!

Post

BertKoor wrote: Tue Oct 20, 2020 7:05 pm Say you need to produce a 100 Hz tone at a 44.1kHz sampling rate. Can your synth do that? For how many voices until cpu can't take it?

What is the difference with rendering 441 samples containing one full cycle, but to be displayed?

Me thinks you're over-thinking it...
i already did. the plot with 441 samples is horrible when lots of higher freqs are sum...

example: try plot a 100hz + 15khz sine with 441 samples... my 2 years old son draw better :P

Post

It is what it is, not? Maybe some waveforms are not supposed to be looked at, but rather to be heard :shrug:
We are the KVR collective. Resistance is futile. You will be assimilated. Image
My MusicCalc is served over https!!

Post

Derozer wrote: Tue Oct 20, 2020 7:13 pm
BertKoor wrote: Tue Oct 20, 2020 7:05 pm Say you need to produce a 100 Hz tone at a 44.1kHz sampling rate. Can your synth do that? For how many voices until cpu can't take it?

What is the difference with rendering 441 samples containing one full cycle, but to be displayed?

Me thinks you're over-thinking it...
i already did. the plot with 441 samples is horrible when lots of higher freqs are sum...

example: try plot a 100hz + 15khz sine with 441 samples... my 2 years old son draw better :P
You can first sample with high enough frequency that all features are guaranteed to be visible (eg. 3-4 times the highest frequency should be enough) and then recursive sub-divide by checking interval mid-points against some pixel tolerance (eg. 0.25 pixels is usually good enough) to make it pretty.

edit: Once all the intervals are below whatever pixel tolerance you have chosen, you then construct a polyline path and stroke it using your favorite general-purpose vector-graphics library, preferably with some sort of anti-aliasing.

Post

Another possibility is to sample both the waveform and it's first derivative at exactly the Nyquist rate (ie. two points per cycle at the highest frequency). This is potentially much faster than generating twice the number of sampling points for the waveform directly (since often you have the quadratures already). With the end-points and their derivatives, you can then either draw it as as a Hermite-spline (which is not quite as accurate, but much faster typically as sub-dividing a cubic is cheap and your vector graphics API can probably do it for you) or you can use the derivatives to check whether the cubic-segment is close enough to a line or whether you should sub-divide by evaluating additional points of the true waveform (which is more accurate, but obviously slower).

Post

mystran wrote: Tue Oct 20, 2020 9:35 pm You can first sample with high enough frequency that all features are guaranteed to be visible (eg. 3-4 times the highest frequency should be enough) and then recursive sub-divide by checking interval mid-points against some pixel tolerance (eg. 0.25 pixels is usually good enough) to make it pretty.
but this means render the whole signal at very higher sample rate. Let say the max freq that can be plot is 20khz, i should process (in case of 4x) 80k sample, and for each sample calculate sin() (or via Euler :P) for each harmonics. such a huge task for a plot i would say, isn't?
mystran wrote: Tue Oct 20, 2020 9:53 pm Another possibility is to sample both the waveform and it's first derivative at exactly the Nyquist rate (ie. two points per cycle at the highest frequency). This is potentially much faster than generating twice the number of sampling points for the waveform directly (since often you have the quadratures already).
that a nice observation. isn't there any "spline" function that will emulate the Whittaker–Shannon interpolation having a bunch of samples? for example, here i have 4 points, and the "curve" will respect that unique path between points (not linear-interpolation between them):

Image

can Hermite-spline do these curves in respect of the "analog" signal that will be outputted?

Post

Derozer wrote: Wed Oct 21, 2020 10:30 am
mystran wrote: Tue Oct 20, 2020 9:35 pm You can first sample with high enough frequency that all features are guaranteed to be visible (eg. 3-4 times the highest frequency should be enough) and then recursive sub-divide by checking interval mid-points against some pixel tolerance (eg. 0.25 pixels is usually good enough) to make it pretty.
but this means render the whole signal at very higher sample rate. Let say the max freq that can be plot is 20khz, i should process (in case of 4x) 80k sample, and for each sample calculate sin() (or via Euler :P) for each harmonics. such a huge task for a plot i would say, isn't?
It's 80k samples if you want 1 second of audio, but if you're fundamental is (say) 100Hz and you only need a single cycle, then it's only 800 samples.

Keep in mind that if you're plotting a single-cycle waveform with harmonic partials, then you can generate the whole cycle with FFT (even if this wasn't the approach you use to generate it in real-time) and you can oversample it as much as you like by zero-padding in spectral domain. This makes the actual sampling cheap.

If you're drawing routines don't care about having gazillion segments, then you can feed this directly to those, but often path-rendering routines with higher image quality tend to scale somewhat badly with the number of line-segments in a poly-line, so it can still make sense to either filter out any useless points or use an adaptive sub-division scheme (which is generally easier to do in a robust way). Note that you can use adaptive sub-division also to choose which points from high-resolution data to actually send to the drawing, it's not like you necessarily need to delay the computation of the data points if it's faster to do it in bulk (eg. with FFT).
that a nice observation. isn't there any "spline" function that will emulate the Whittaker–Shannon interpolation having a bunch of samples? for example, here i have 4 points, and the "curve" will respect that unique path between points (not linear-interpolation between them):
This is the exact same problem as interpolating (eg. resampling) audio for other purposes and most sensible interpolation schemes (or splines, whatever) will converge to the correct result when either the intervals become infinitely small or the order (ie. number of points you're using) goes to infinity. The more information you use, the better the fit and for "exact" results you need infinitely high order.

If you have a critically sampled signal (ie. Nyquist rate, 2 points per cycle at highest frequency) and you want to draw the correct interpolated curve (such as Edison does) then you take any old linear-phase resampling FIR and use it to find the intermediate points, either to directly sample at a higher resolution, or to adaptively sub-divide the intervals until it's "good enough" for visual purposes. The latter approach is usually more efficient. The better the FIR the more accurate your result.

Note that another possibility is to use a high-quality FIR to first oversample the signal by 2x or 4x and then just draw something like cubic Catmull-Rom splines which would be poor and potentially miss features for a critically sampled signal, but are likely to be visually just fine for somewhat oversampled signal. In theory you can also do adaptive sub-division to some extent, then use non-uniform cubic-splines, but the math for computing those does get a bit trickier.

The reason I was suggesting either 4 points per cycle or 2 points per cycle + derivativesf is that if you use an adaptive subdivision scheme that checks the mid-point and you only have 2 points sampled at the peaks of a sine-wave at the maximum frequency, then the average of these is half-way between the two samples, which results in zero-error at the mid-point, but the maximum error is at the mid-point of the next round of sub-division. If the signal is sampled at 4 points per cycle, then this shouldn't ever happen and you should be guaranteed to always see at least some error.

Post

Derozer wrote: Wed Oct 21, 2020 10:30 am Image

can Hermite-spline do these curves in respect of the "analog" signal that will be outputted?
Assuming we're doing Hermite-splines the usual way by estimating derivatives by finite differences (ie. looking at 4 points) then the answer is no. For example, with a step in a waveform, any "ripple" (which should extend infinitely far, even if it decays as you get further away from the step) is only present in the segments where the step falls within the 4-point window, because that's as far as the interpolator can see. In frequency domain sense, the interpolator is not great close to Nyquist rate.

However, if you first oversample by at least 2x with a high-quality interpolator (which you can do fast with FFT just for the window of interest), then you're asking your cubic-interpolator to only reconstruct frequencies up to 0.25 times the sampling rate and the response is actually fairly flat for that range, possibly good enough for visual purposes. At 3x it should probably be close to perfect. For the step-example, the high-quality interpolator will fill in the ripple between the original sampling points and the cubic interpolator now only has to turn it into nice smooth curves.

This might actually be the ideal strategy if you're trying to plot arbitrary WAV-files like Edison. Note that you don't need to compute such a spline (or the 2x oversampled signal for that matter) for the whole file, but rather just for the part that is actually visible in the window (and then you can take this further with only computing newly exposed parts when scrolling, but that's not the point).

Post

hermite seems a good approch...
here's my attempt in js: https://jsfiddle.net/u9ed3ag4/
it looks quite smooth i would say, what do you think?

Post Reply

Return to “DSP and Plugin Development”