Fourier Synthesis- mouse driven.

Fourier Synthesis

You can click on any one of the coloured sine/cos components, drag sideways or up/down and it redraws with the new phase and amplitude.

The right hand side updates with the curve you get from the sum of the terms.

To be added- button or menu to offer some of the standard Fourier series like square wave, triangular, etc.


    nomainwin

    WindowWidth  =1080: WindowHeight = 700

    graphicbox #w.g, 0, 0, 1088, 698

    open "Fourier Synthesis- mouse driven." for window as #w

    #w "trapclose quit"

    handleg  =hwnd( #w.g)
    calldll  #user32, "GetDC", handleg as ulong, hDC as ulong

    col$( 0) ="120 120 120":  col$( 1) ="255 0 0":       col$( 2) ="0 255 0"
    col$( 3) ="50 50 255"  :  col$( 4) ="0 255 255":     col$( 5) ="255 0 255"
    col$( 6) ="255 255 0":    col$( 7) ="160 160 160":   col$( 8) ="220 200 150"

    pi       =4 *atn( 1)

    #w.g "size 4 ; color white"
    #w.g "when leftButtonDown [update1]"
    #w.g "when leftButtonUp   [update2]"

    #w.g "down ; size 2 ; fill 10 10 50"
    #w.g "up ; color white ; goto 10  80 ; down ; goto 510  80"
    #w.g "up ;               goto 10 130 ; down ; goto 510 130"
    #w.g "up ;               goto 10 180 ; down ; goto 510 180"
    #w.g "up ;               goto 10 230 ; down ; goto 510 230"
    #w.g "up ;               goto 260 10 ; down ; goto 260 560"

    for n =0 to 8
        amp( n) = 0
        phi( n) = pi /2

        #w.g "color "; col$( n)
        #w.g "size 8"

        for j =0 -pi to pi step 0.005
            x =260 +250 *j /pi
            y =80 +n *50 -amp( n) *Fourier( n, 0, j)
            #w.g "set "; int( x); " "; int( y)
            scan
        next j

        #w.g "flush"
    next n

    wait    '   ___________________________________________________________

  [update1]
    #w.g "flush"
    initX   =MouseX
    initY   =MouseY
    calldll #gdi32, "GetPixel", hDC as ulong, initX as long, initY as long, pixcol as ulong
    bl      =int(  pixcol /( 2^16)): gr = int( (pixcol -bl *2^16) / 256): re = int(  pixcol -bl *2^16 -gr *2^8)
    co$     =str$( re) +" " +str$( gr) +" " +str$( bl)
    wait

  [update2]
    newX    =MouseX
    newY    =MouseY

    #w.g "cls ; fill 10 10 50 ; size 2"
    '#w.g "up ; goto "; newX +10; " "; newY +20
    #w.g "up ; color white ; goto 10  80 ; down ; goto 510  80"
    #w.g "up ;               goto 10 130 ; down ; goto 510 130"
    #w.g "up ;               goto 10 180 ; down ; goto 510 180"
    #w.g "up ;               goto 10 230 ; down ; goto 510 230"

    for n =0 to 8
    col$( 0) ="120 120 120":  col$( 1) ="255 0 0":       col$( 2) ="0 255 0"
    col$( 3) ="50 50 255"  :  col$( 4) ="0 255 255":     col$( 5) ="255 0 255"
    col$( 6) ="255 255 0":    col$( 7) ="160 160 160":   col$( 8) ="220 200 150"

        select case co$
            case "120 120 120"
                selected =0
            case "255 0 0"
                selected =1
            case "0 255 0"
                selected =2
            case "50 50 255"
                selected =3
            case "0 255 255"
                selected =4
            case "255 0 255"
                selected =5
            case "255 255 0"
                selected =6
            case "160 160 160"
                selected =7
            case "220 200 150"
                selected =8
            case else
                selected =10
        end select

        if selected <>10 then
            phi( selected)     =( 260 -newX) /250 *pi *selected
            amp( selected)     =80 + selected *50 -newY
            #w.g "backcolor darkblue ; color white"
        end if

        #w.g "size 6"

        for j =0 -pi to pi step 0.005
            x   =260 +250 *j /pi
            y   = 80 +n *50 -amp( n) *Fourier( n, phi( n), j)
            #w.g "color "; col$( n)
            #w.g "set "; int( x); " "; int( y)
            scan
        next j

        #w.g "size 2 ; color white"
        for j =0 -pi to pi step 0.005
            x   =780 +250 *j /pi
            FourierSum =0

            for m =0 to 8
                FourierSum =FourierSum +amp( m) *Fourier( m, phi( m), j)
            next m

            y   = 300 -FourierSum *2
            #w.g "set "; int( x); " "; int( y)
            scan
        next j

    next n

    wait

    sub quit h$
        close #w
        callDll #user32, "ReleaseDC", handleg as ulong, hDC as ulong, result as ushort
        end
    end sub

    function Fourier( n, phi, x)
        Fourier =cos( phi +n *x)
    end function