Sum of harmonics with different phases - how to get the max peak?

DSP, Plugin and Host development discussion.
RELATED
PRODUCTS

Post

Hi,

let say I've a bunch of harmonics, that I'll add each other.

Such as:
h1 = 1000hz (fundamental)
h4 = 4000hz
h7 = 7000hz

All of them start at different phases:
p1 = 0.21f
p4 = 0.0f;
p7 = 0.78f;

Is there a math way, having those data, to catch the max peak the signal will reach? So I can rescale/normalize the output signal accordingly, before output it?

Thanks

Post

the typical way to find minima and maxima of a function is to find its derivative, set it to zero and solve for the independent variable. in the case of 3 sines:

f(t) = a1*sin(w1*t + p1) + a2*sin(w2*t + p2) + a3*sin(w3*t + p3)
f'(t) = w1*a1*cos(w1*t + p1) + w2*a2*cos(w2*t + p2) + w3*a3*cos(w3*t + p3) = 0

the second equation would now have to be solved for t. problem is:

(1) there may be several solutions
(2) it's not obvious how to find an explicit expression for t - might even be impossible

if you have an initial guess t0 for where the solution might be, you could use newton iteration to find the precise solution (you'll need the 1st together with the 2nd derivative for this). but how to come up with such an initial guess? the brute force way would be to generate one cycle of the waveform at sufficient resolution and keep record its minimum and maximum - and then use these two as initial guesses in the newton iteration (you really want to find both, minimum and maximum and then normalize according to the maximum of the absolute values of these two). but maybe your initial guess is already good enough for this purpose and you can skip the newton iteration. or, you could fit a parabola to the min/max sample locations and their left and right neighbors and solve for the min/max of the parabola and use that as approximate stand-in for the actual min/max values. ....or you could combine these approaches: use the parabola min/max values as initial guess for newton iteration - it really depends on how precise you need it to be
My website: rs-met.com, My presences on: YouTube, GitHub, Facebook

Post

is rendering one frame, then finding teh peak, efficient math alright?
you come and go, you come and go. amitabha neither a follower nor a leader be tagore "where roads are made i lose my way" where there is certainty, consideration is absent.

Post

I'd say it's not a good idea. Just as bad as lowering volume because multiple notes are playing instead of just one.

With classical subtractive synthesis, when the filter cutoff is lowered, then the volume goes down because harmonics are filtered away. The user expects this.

On a Hammond drawbar organ, when drawbars are closed, then the volume goes down. The user expects this.

With additive synthesis like in your case, it is the same. The user expects this.

A track recorded at ideal volume has no peaks at zero db full scale, but rather an RMS level of about -20dB. You don't need to normalize, ths user has a volume knob and will turn it down.
We are the KVR collective. Resistance is futile. You will be assimilated. Image
My MusicCalc is served over https!!

Post

what about finding max(min) every time polarity changes?
so greater than 'hold' peak and reset on 0 cross, while hold prior half cycle for the result

I can't see any problem with that if the harmonics don't cross 0

peak is signal less than signal delayed by one sample, and hold
-and inverse for the negative portion

Post

Hi Derozer,

How much accuracy do you need? One guaranteed upper bound is simply the sum of the amplitudes. It's not the least upper bound (aka supremum), but for your case and with amplitudes of 1.0, it's not far off (edited): 3.0 versus about 2.8, about a 7% error or about 0.6 dB, assuming I did my arithmetic correctly (hopefully this time).

There may be a way to bound the error for the general case, something like the lowest least upper bound (?), but that might be hard to track down, derive, and/or calculate. It might be easier to adapt the approach of xoxos and perform a Monte Carlo simulation to estimate it, if you are worried about it for the general case. The function is periodic with a period the same as that of the fundamental, so that's as far as you need to go.

If you need more accuracy, the approach by Music Engineer, after substituting 4*w1 for w2 and 7*w1 for w3, leads to a 7th degree polynomial in cos(w1*t) with coefficients determined by sum-of-angle formulae and the sin and cos of the phase angles. This isn't very tractable for the general case, and it's a lot of work for the little gain of your particular example. But if you need accuracy for only this one case, it might be worth doing.

Regards,
Dave Clark

Post

DaveClark wrote: Tue Jul 14, 2020 4:08 pm How much accuracy do you need? One guaranteed upper bound is simply the sum of the amplitudes. It's not the least upper bound (aka supremum), but for your case and with amplitudes of 1.0, it's not far off (edited): 3.0 versus about 2.8, about a 7% error or about 0.6 dB, assuming I did my arithmetic correctly (hopefully this time).
This upper bound can be totally useless for some waveforms though. The obvious example would be a saw or square wave, where the sum of amplitudes of the harmonics actually diverges. With a band-limited version with finite harmonics, you will obviously get a finite value, but it can still be ridiculously large compared to the actual peak amplitude, which is usually only a bit higher (due to Gibbs) than the fundamental.

Post

Hi mystran,

In the first paragraph, I was primarily considering the specific problem posted by the OP, but what I wrote is also applicable to similar problems with a few harmonics and, more importantly, arbitrary phase shifts including the cases where all harmonics are aligned for a positive peak for some time t. For these latter cases, the amplitude indeed diverges in the limit as the number of harmonics increases and would make the Gibbs effect look like an infinitesimal sniffle. For the general case, I have already alluded to the fact that a statistical approach is probably more appropriate. Using a square wave as a case study to determine what could possibly happen with the general case would be extremely misleading and potentially disastrous.

Regards,
Dave Clark

Post

@BertKoor: yes I'm aware of what you mean, but I have good reasons to do this, thanks.

@nix808: not sure about your suggestions. If I check the peak after it has been output... well, its been passed, and I've not normalized it. Looking for a sort of pre-processing, I guess.

@DaveClark: yes, I'm looking for the general case (also a collection of sines that make square, saw, or any kind of waveform... with different phases etc), so the sum of num sine is not enough.

Not sure about f'(t) = w1*a1*cos(w1*t + p1) + w2*a2*cos(w2*t + p2) + w3*a3*cos(w3*t + p3) = 0 : Is that formula used for find max peak? What about Min? And what Is that "t"? I need the peak at the whole cycle, not at a fixed "t" (else I would render the whole cycle and find Min/max peak, as suggested by xoxox).

Post

Derozer wrote:Not sure about f'(t) = w1*a1*cos(w1*t + p1) + w2*a2*cos(w2*t + p2) + w3*a3*cos(w3*t + p3) = 0 : Is that formula used for find max peak? What about Min? And what Is that "t"? I need the peak at the whole cycle, not at a fixed "t" (else I would render the whole cycle and find Min/max peak, as suggested by xoxox).
I think the answer to your original question is basically "no, not really".

If you have a periodic waveform, then calculating the derivative (slope) f'(t) as above (where t is a parameter that goes from 0 to 2*pi, in order to encapsulate a full cycle of the fundamental) might allow you to calculate values of t at which the derivative is zero.

The minimum and maximum of the original waveform must also correspond to places where the slope is zero, although not every place with zero slope will be a global max/min - there could be local maxima (in ripples for example) or places where the slope is zero but doesn't change sign.

So you'd have to solve f'(t) for all values of t that give slope=0, if that's even possible, then plug those values of t into your original waveform f(t) and just keep the min and max values.

All in all I think you'd be better off just iterating through f(t) with a bunch of test values and keeping the min and max, like xoxos suggested. Maybe using an adaptive search where you reduce the step size in locations that look 'interesting'.

Post

I could follow the xoxox suggestion, yes. But what if my fundamental is (for example) 0.25 hz? I need to iterate many samples (174k with 44k samplerate). Just for one Harmonic. Do it "real time" would saturate the CPU :(

Post

Derozer wrote: Fri Jul 10, 2020 9:30 am Hi,
The peak level for three sines = 1 magnitude = 3 where the phase aligns. Will the phase align? Yes. How long will it take? Find the lowest common denominator of the product of the three frequencies.

It doesn't matter if it takes ten billion years, the peak = 3.

Where the frequencies have 1/prime frequency ratios (will never align) you can consider the phases "syncd" which is what I assume the question is. Given phase variability the peak is still 3 but with those specific three phases you'll need to find the integral of a single cycle of the lowest frequency component. (see Music Engineer's post.)

So there's your answer. It depends on the frequency ratios, either 3 or some number less than 3 for "phase sync" depending upon the three phases.

Apparently I've just repeated everything that was already posted. I'll do it a bit more:
I'm only rewording it but the solutions to this problem and a range of associated problems are all either indefinite or not computationally efficient. If you're looking for the peak of the continuous function you'll need to approximate it to make it possible to compute, while for the discrete function the best solution is probably to do it naively (render the cycle while finding the min/max.)

Similar problems include finding the frequency range in frequency modulation given the carrier and modulator parameters. All of these problems have no "trivial" solution and involve approximations with a lot of work much like finding the value of pi.
Last edited by aciddose on Wed Jul 15, 2020 10:45 am, edited 1 time in total.
Free plug-ins for Windows, MacOS and Linux. Xhip Synthesizer v8.0 and Xhip Effects Bundle v6.7.
The coder's credo: We believe our work is neither clever nor difficult; it is done because we thought it would be easy.
Work less; get more done.

Post

Derozer wrote:I could follow the xoxox suggestion, yes. But what if my fundamental is (for example) 0.25 hz? I need to iterate many samples (174k with 44k samplerate). Just for one Harmonic. Do it "real time" would saturate the CPU :(
It's not really clear what you're trying to achieve, some more info might help.
Are your partials always strictly harmonics of the fundamental - i.e. are you working with a repeating composite waveform?
Are the phases and/or amplitudes varying in real time, or is this a one-off calculation?
Are you trying to normalise to prevent some kind of overflow, or to make things sound 'right' for some particular application?

Post

A solution to the frequency issue is to compute only the number of samples that will include the peak of all three frequencies. You know each cycle has two peaks -/+, so if you have frequency ratios like 1, 2, 3 you'll need:
1 = 2 samples
2 = 4 samples
3 = 6 samples
2 * 4 * 6 = 48 samples to find the peak

I'm using intuition here, so maybe someone can explain that more eloquently. Is it possible for the maxima to NOT include at least one peak of the contributing inputs? Is it possible for the min/max to NOT be equal magnitude?

These questions are answered in Fourier theory, are they not? = no, no.

It seems to me we're dealing with abstract sines here, not real discrete or continuous waveforms. So the actual frequency doesn't play any part whatsoever.
Free plug-ins for Windows, MacOS and Linux. Xhip Synthesizer v8.0 and Xhip Effects Bundle v6.7.
The coder's credo: We believe our work is neither clever nor difficult; it is done because we thought it would be easy.
Work less; get more done.

Post

If you use Desmos to plot sin(x)+sin(2x+1), for example, it's clear that (a) the max does not correspond with the max of either sine function, and (b) the max and min are very different magnitudes. If the harmonics have differing amplitudes then the effect is even more marked.

Post Reply

Return to “DSP and Plugin Development”