## Really (potentially) awesome discovery.

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Zamaster
Posts: 1024
Joined: Jun 20, 2005 21:40
Contact:

### Really (potentially) awesome discovery.

So first, let me say that I dissappeared for a week due to a certain deadline for a certain work related project and then took the weekend off for a holiday. During the time away, I thought about designing a old school analog synth employing a harmonic editor to create the single cycle wave forms(square waves, sine waves, saws etc...).

Of course, to keep something like this old school it would all have to be done with knobs and sliders, so I figured the max amount of harmonics I could jam onto a semi portable box was 16. The problem with this however was accuracy of the finished wave.

I decided that I wouldnt mind the slight curvature of any large jumps in amplitude, but I would mind the Gibbs phenomenon screwing up those jumps as well. So i decided I would try and beat many years of mathematicions and figure out a harmonic synthesis technique without any ugly artifacts. I also thought about faster ways to do do such synthesis.

I finally arrived that instead of using additive synthesis on sine waves to combine harmonics, I might try a diffrenet basic wave, even more basic then sine, a square wave. At first, using square waves to synthesize a single cycle wave forms seemed riddiculous to me until I started experimenting with a piece of graph paper. I quickly thought about the basic non-square wave forms (saws, sines, triangles) and gave a shot at synthesizing them with only square waves. I soon found out that I was able to do both triangles and saws before it was time for my dinner reservation with added sqaure waves.

I got VERY excited by this for a few reasons. First of all, there were NO artifacts at the visible convergence point of the waves. Second of all, there were much simpler rules governing the behavior of the waves(e.g. the amplitude and frequency of consecutively added sqaures by n^2(n in iterations) creates a saw wave. The offset is left alone for a saw.). Also, digital square waves are much much faster to create than sines.

I have not however found an equivelent for the Fouier transform for this sqaure method which until I find renders this method virtually useless. But in the event that I do(which looks rather promising), I may be onto something very, very cool!

Heres a VERY basic example to help get the gist of what im talking about here, its missing many things but it shows how a saw wave is synthesized out of square waves:

Code: Select all

`Const Length as integer = 64000CONST Steps  as integer = 13CONST DivY   as integer = 100CONST DivX   as integer = 100function Sqd(x as integer, md as integer) as integer    Return (int(x/md) mod 2) * 2 - 1 end functionscreenres 640,480,8Dim as integer i, a, b, x, y, dx, dy, ox, oyFor x = 1 to Length    y = 0: a = 1    For i = 1 to Steps        y -= Sqd(x,a) * (a-1)        a *= 2    Next i    dx = x/DivX: dy = y/DivY+240    Line (ox,oy)-(dx,dy)    ox = dx: oy = dyNext xsleepend`
counting_pine
Posts: 6180
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs
(EDIT: Apologies to people who stumble across this post, most of it is very wrong...)

If you're using square waves, I think your basis will consist of the following set of functions:

Code: Select all

`a[0](x) = 1, 'a constant functiona[n][x0](x) = { 1 | floor( (x - x0) * 2^n ) even } 'square wave functions              {-1 | floor( (x - x0) * 2^n ) odd  } '(assuming a period of 1)`

Apart from the constant, there's a 2-dimensional set of functions to work with, all of them mutually orthogonal (I think).
n (log2 of the frequency of the wave) would be an integer, 1 or greater.
x0 (the phase of the wave), I guess would be in the range [ 0, 1/2^(n+1) ), technically continuous, but effectively limited by the precision you allow yourself for the x value.

I'd say the problem with square waves is that you have to work with two dimensions of functions, rather than one.
Sine waves don't have this problem because if you shift the phase, it's still representable with sin and cos:

Code: Select all

`sin(x - X0) = cos(X0) * sin(x) - sin(X0) * cos(x)`

(the X0 terms are constant)
Last edited by counting_pine on Aug 27, 2007 22:35, edited 1 time in total.
Zamaster
Posts: 1024
Joined: Jun 20, 2005 21:40
Contact:
So using mod to be the base definition of the period 1 wave(wave that oscilates at every integer) is a bad idea?

The way I decided to structure future work with this is using a 2^32 different possible waves. Each one Oscillating at a period defined by its value and the shifting of phases is done simply by adding the shift value when the wave is looked up and wrapping the rest that goes over the 2^32 limit.