0 Favourites

# [SOLVED] Irregular Sine Waves

• 21 posts
• A brain teaser for all mathematical educated:

You see a sine wave (green). Yellow and red are the same wave, but with, let's say, its center moved to the sides. Note that the basic conditions don't change. The peaks are still at -1 and +1, the width is still 2.

There must be a formula for this, one that allows for a variable v that alters the shape similar to the image. It should be a compact formula, so that it can be applied in realtime.

I'm trying to implement this for an audio function, not for graphics. But the principles are the same, so a formula that works on graphics would be a super start for audio optimized code.

I failed at this task, so I really hope you can help me

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• You mean like an RMS?

sqrt((n1^2+n2^2)/2)

I think

• Out of curiosity, could you share more details on its purpose?

• sin(2x)? or sin(x/2), depends on what you need.

http://www.wolframalpha.com/input/?i=sin%28x%29 <- sin x (default sinus)

http://www.wolframalpha.com/input/?i=sin%282x%29 <- sin(2x) (sinus with halved longitude)

http://www.wolframalpha.com/input/?i=sin%28x%2F2%29 <- sin(x/2) (sinus with 2x longer longitude)

as you can notice, to reduce the "length" you need to increase 2x part (change 2 for the number you need depending on size), for prolonging waves you need to increase x/2 (instead of 2 .. put your numbers there)

• Out of curiosity, could you share more details on its purpose?

Since you are a musician, I will keep it short. An easy way to create rich harmonics from just one sine osc. The behaviour is similar to pwm for square waves.

Thanks a lot for the help, but this is not what I'm looking for. I'm trying to calculate the wave with the same characteristica than the normal one. It needs to be constant over x (for example plotted with 512 points with exact same spacing). Trying to add two half-circles with two different multiplicators doesn't lead to the desired result and is not smooth on the transients, unfortunately.

• Boids applied to harmonics would be interesting.

• Well you can't build the function you're talking about out of sin/cos, because by definition both are even/odd functions with a special relationship to geometry, while your function would be asymmetrical and has no real physical significance. This kind of hints any attempt at building them from a sum/series of sin/cos harmonics (a Fourier series) would require A LOT of harmonics to approximate the shape, and to begin with you'd need a way to define your function.

What I would suggest is to build a function with cubic splines. I don't really have time to work out the math but it's not difficult.

Basically set it up so that you define the points (-1,0),(m,0),(1,0) which are the "left","center","right" of the wave (all the points where it crosses the X axis). m is a parameter you control to set where the "middle" will be where m is somewhere in the interval (-1,1), with m=0 giving something akin to a sine wave, and any values different than 0 shifting the point left or right. Then you set up your function S(x) as a piece-wise function

S(x):

if(x<m) S0(x)=a0+b0(x)+c0(x)^2+d0(x)^3

if(x>=m) S1(x)=a1+b1(x-m)+c1(x-m)^2+d1(x-m)^3

then you'd need to solve for all 8 parameters a0,a1,b0,b1,c0,c1,d0,d1

you'd do this by adding constraints onto S(x), like the following to get 8 equations in 8 unknowns (note the ' and '' indicate 1st and 2nd derivatives)

S(-1)=0

S(m)=0

S(1)=0

S0(m)=S1(m)

S0'(m)=S1'(m)

S0''(m)=S1''(m)

with additional constraints to ensure the function is smooth when it's periodic

S0'(-1)=S1'(1)

S0''(-1)=S1''(1)

you could also just set S0'(-1)=S1'(1)=K instead of the condition i wrote last, allowing you to explicitly set the slope to K at the endpoints.

OR

maybe if you want to mimic sine waves you could set S0'(m)=S1'(m)=-S0'(-1), but this could be weird. Generally playing with the periodic constraints will help you control things nicely.

Note that you need to give the function x values in the range [-1,1], or else you'll get weird output since this is basically just cubic polynomials that are infinitely increasing or decreasing past a range. So if you want to pass in a value that behaves periodically you have to remap it periodically to the [-1,1] range, similar to how an angle like 720 deg.=360 deg.=0 deg. That part should be easy to figure out.

Obviously you'll need to work out all the math to efficiently set all the 8 parameters for the function based on any given m.

This is the best idea I have to offer, I've never seen any function like the one you want, and this one wont perfectly reconstruct a sine wave for m=0, but it'll be something close and fast to evaluate, as-well as offers the capability to control the derivatives with additional parameters.

To be honest i have no idea what it'll sound like so if you work it out i'd be interested to hear I assume it'll just start to exemplify strange harmonics as you shift the center, since this is what it'll be doing to the Fourier series

• Quansi's idea is probably the ideal way to go about it, since it's known to work.

I came up with an iterative way here:

https://dl.dropboxusercontent.com/u/542 ... g_sin.capx

Changing the k parameter from 0 to 1 controls the center position. It has the desired shape, but it's not in a useful format yet.

It still needs to be offset to align it, and it needs to be scaled to be the correct ranges.

My guess is there may be a way to manipulate the equations to be in the form of one function that gives a y for an x. I may have a go at that later.

• Just change the time period, the humps will stay in the same place, only spaced out.

Are you using the sine behavior?

• Thank you so much! Your in-depth explanations, help me a huge part of the way. And that it doesn't exist yet is the reason why I want to realize it. I already heard a prototype (which was building from zero crossing sines that are halved and shifted around to match) done for me by another musician and it convinced me to try it.

(move "zero point" knob)

@ROJOhound

I'm not sure if it really does the correct thing. If I set k to e.g. 0.25 there's a spike where a curve should be. (convex instead of concave, or the other way round, I always confuse them)

@jojoe No, I'm not. It is for a function that will be used for an audio signal.

• @QuaziGNRLnose

I had a go at quazi's idea here:

https://dl.dropboxusercontent.com/u/542 ... ordan.capx

It was getting to tedious to get a nice formula from the equations, so I ended up putting all the formulas into a matrix and used guass-jordan to solve as a system of equations. Every time I change "m" I use that to recalculate the coefficients.

Using an x^3 polynomial wasn't working right. It looked good when "m" was centered, but the amplitude would get crazy otherwise and the shape would lean. It ended up needing a x^4 polynomial, or 10 coefficients. For 10 unknowns I needed 10 equations: Three equations per piece (S0(x) and S1(x)) for the points. Two for the single derivatives between the pieces and two more for the double derivatives. That mess of formulas is in the bottom of js.txt.

Anyways hat's off to quazi for the idea. Testing it it seems the limit for m is 0.5 to 1.5, beyond that the shape gets crazy.

• @QuaziGNRLnose

ROJO you nailed it! The wave behaves exactly as it should inbetween the limits. I will now take a closer look at js.txt

Thank you both, ROJO and Quazi, for your effort and a solution to a more complex problem than I thought.

I did a Spline class once for Ruby (an interpreter oop language), and I can replicate the behaviour only to a degree with it (unless I change the controls). Unfortunetly I did it with cubic interpolation from spot to spot instead of polynomials (which I never really got used to), so this is really, really helping me a lot!

• To be honest i have no idea what it'll sound like so if you work it out i'd be interested to hear I assume it'll just start to exemplify strange harmonics as you shift the center, since this is what it'll be doing to the Fourier series

@QuaziGNRLnose and R0J0hound

It was such a joy when I heard the oscillator playing for the first time! I made a quick mp3. No pre-editing, no post-editing, no mastering. What you hear is just one SWM-Sine-Osc, whose "center" is modulated by hand as well as by two serial LFOs. No Filters, no effects, nothing but pure Osc-Sound. The modulation does exactly what Rojo demonstrated in his capx (although the underlying dsp-code is somewhat different).

So much smoother and musically way better controllable than, for example, FM, and on my 7 year old pc the cpu load was between 0.3 and 0.7% during the sequence you hear in the mp3. Awesome! I'm in love with that oscillator.

Currently started working on a VSTi to see what else I can get out of those OSCs...

First impression.mp3

btw, SWM stands for Slope Width Modulation. But that doesn't perfectly describe it. If anyone has a better name for it, please tell!

(And sorry that this has gone beyond C2)

• > To be honest i have no idea what it'll sound like so if you work it out i'd be interested to hear I assume it'll just start to exemplify strange harmonics as you shift the center, since this is what it'll be doing to the Fourier series

>

It was such a joy when I heard the oscillator playing for the first time! I made a quick mp3. No pre-editing, no post-editing, no mastering. What you hear is just one SWM-Sine-Osc, whose "center" is modulated by hand as well as by two serial LFOs. No Filters, no effects, nothing but pure Osc-Sound. The modulation does exactly what Rojo demonstrated in his capx (although the underlying dsp-code is somewhat different).

So much smoother and musically way better controllable than, for example, FM, and on my 7 year old pc the cpu load was between 0.3 and 0.7% during the sequence you hear in the mp3. Awesome! I'm in love with that oscillator.

Currently started working on a VSTi to see what else I can get out of those OSCs...

First impression.mp3

btw, SWM stands for Slope Width Modulation. But that doesn't perfectly describe it. If anyone has a better name for it, please tell!

(And sorry that this has gone beyond C2)

Yo,

I

m bit confused now: do you mean you've created synth inside c2?

• Hey that sounds pretty cool.

@megatronx

I guess he could by generating it on the fly with webauido, but from the sounds of it he's using something else to program directly with a dsp chip on the soundcard.

• 21 posts