[SOLVED] Irregular Sine Waves

Discussion and feedback on Construct 2

Post » Sun Nov 01, 2015 11:04 am

A brain teaser for all mathematical educated:

Image

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 :ugeek:
Last edited by tulamide on Sun Nov 15, 2015 6:56 pm, edited 1 time in total.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Sun Nov 01, 2015 12:30 pm

You mean like an RMS?
sqrt((n1^2+n2^2)/2)
I think
Image ImageImage
B
169
S
50
G
169
Posts: 8,285
Reputation: 108,214

Post » Sun Nov 01, 2015 12:58 pm

Out of curiosity, could you share more details on its purpose?
My professional Royalty Free Music at Scirra Assets Store
--------------------------------
Specs: i5 2500, 16gb of ram, gtx 770, win 7, Focusrite Scarlett 8i6, Mackie mr8mk2, Alesis 320, browsing the net on chrome.
B
89
S
30
G
22
Posts: 1,985
Reputation: 20,099

Post » Sun Nov 01, 2015 1:12 pm

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)
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
42
S
14
G
12
Posts: 624
Reputation: 9,421

Post » Sun Nov 01, 2015 2:33 pm

megatronx wrote: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.


@saiyadjin
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.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Post » Sun Nov 01, 2015 3:07 pm

Boids applied to harmonics would be interesting.
Image ImageImage
B
169
S
50
G
169
Posts: 8,285
Reputation: 108,214

Post » Sun Nov 01, 2015 9:55 pm

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
B
77
S
13
G
8
Posts: 1,973
Reputation: 9,891

Post » Sun Nov 01, 2015 11:20 pm

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.
B
92
S
32
G
106
Posts: 5,273
Reputation: 69,457

Post » Mon Nov 02, 2015 12:00 am

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

Are you using the sine behavior?
B
29
S
8
G
4
Posts: 767
Reputation: 5,985

Post » Mon Nov 02, 2015 11:54 am

@QuaziGNRLnose
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.
dropbox link to prototype.rar
(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.
Image
B
24
S
8
G
10
Posts: 1,821
Reputation: 8,279

Next

Return to Construct 2 General

Who is online

Users browsing this forum: AndreasR and 5 guests