Fourier Analysis and Synthesis.

Fourier Synthesis

This states that we can create a periodically repeating wave form by adding sin/cos terms, each of different amplitude and frequency. ( This can be replaced by the equivalent adding of exp( i theta) terms, since a sine and cos wave of the same frequency amount to a phase-shifted cos wave.) The first component id DC, ie zero frequency, and the following terms are at f, 2f, 3f,.... The more terms used, the more accurately a waveform can be approximated.

Fourier Analysis

Of course, to synthesize a wave of shape that we want, we need the reverse process, ie given our chosen waveform- say a triangle wave- what components are needed to make it?

Demonstration program

The left hand side of the image below shows Fourier synthesis. The image was generated by starting with some amplitude data for four cos waves, showing their size against a background of unity amplitude for that component, then showing the sum.

The right hand side shows this ( approximately triangular) wave being taken as source, and the needed amplitudes are re-generated from it.



    '   Fourier
    '   nomainwin

    WindowWidth  =1000
    WindowHeight = 680

    open "Demo of Fourier analysis/synthesis" for graphics_nsb as #wg

    #wg "trapclose quit"

    #wg "down ; size 1 ; fill darkblue"

    dim fn( 200)

    k0 = 0.2:   k1 = 0.9:   k2 = 0:   k3 = 0.333

    col$( 0) ="red":  col$( 1) ="cyan":   col$( 2) ="yellow":   col$( 3) ="255 60 255":   col$( 5) ="black"

    print " Synthesis & Analysis with Fourier Coefficients"
    print
    print "  Term n     calc'd fraction  v/  spec'd fraction"

    #wg "color red"

    for n =0 to 3
        #wg "color "; col$( n)
        #wg "up   ; goto 20 "; 120 +n *100; " ; down ; goto 950 "; 120 +n *100
    next n

    for j =-1 to 1 step 0.01    '   Display four Fourier components and the totalled function.
        indx      =int( 100 +j *100)

        t0        =k0 *Fourier( 0, j)
        #wg "color white ; up ; goto "; 220 +int( j *200); " "; 120 -Fourier( 0, j) *50
        #wg "down      ; goto "; 220 +int( j *200); " ";        120
        #wg "down ; color red ; goto "; 220 +int( j *200); " "; 120 -t0 *50

        t1        =k1 *Fourier( 1, j)
        #wg "color white ; up ; goto "; 220 +int( j *200); " "; 220 -Fourier( 1, j) *50
        #wg "down      ; goto "; 220 +int( j *200); " ";        220
        #wg "down ; color red ; goto "; 220 +int( j *200); " "; 220 -t1 *50

        t2        =k2 *Fourier( 2, j)
        #wg "color white ; up ; goto "; 220 +int( j *200); " "; 320 -Fourier( 2, j) *50
        #wg "down      ; goto "; 220 +int( j *200); " ";        320
        #wg "down ; color red ; goto "; 220 +int( j *200); " "; 320 -t2 *50

        t3        =k3 *Fourier( 3, j)
        #wg "color white ; up ; goto "; 220 +int( j *200); " "; 420 -Fourier( 3, j) *50
        #wg "down      ; goto "; 220 +int( j *200); " ";        420
        #wg "down ; color red ; goto "; 220 +int( j *200); " "; 420 -t3 *50

        fn( indx)   =t0 +t1 +t2 +t3
        #wg "up   ; goto "; 220 +int( j *200); " "; 540 -fn( indx) *50
        #wg "down ; goto "; 220 +int( j *200); " "; 540
    next j

    for n =0 to 3

        a( n) =0:   b( n) =0

        for j =-1 to 1 step 0.01  '   integrate up the product of my function fn( j) and Fourier( n, j) at each j.
            a( n)   =a( n) +fn( 100 +j *100) *Fourier( n, j)
            b( n)   =b( n) +                  Fourier( n, j)^2 '   what the integral would be if e() was just this Fourier term...
        next j

        if b( n) <>0 then frac( n) =( a( n) /b( n)) else frac( n) =0

        #wg "color "; col$( n)
        #wg "up   ; goto 20 "; 120 +n *100; " ; down ; goto 950 "; 120 +n *100

        for j =-1 to 1 step 0.01
            fn( 100 +j *100)     =fn( 100 +j *100) -frac( n) *Fourier( n, j)
            #wg "color "; col$( n)
            #wg "up   ; goto "; 720 +int( j *200); " "; 120 +100 *n -int( frac( n) *Fourier( n, j) *50)
            #wg "down ; goto "; 720 +int( j *200); " "; 120 +100 *n
        next j

        print "  "; n, using( "##.######", frac( n)),,
        select case n
            case 0
                print k0
            case 1
                print k1
            case 2
                print k2
            case 3
                print k3
        end select

    next n

    #wg "size 2"

    for j =-0.999 to 1 step 0.01
        n       =int( 100 +j *100)
        fn( n)   =frac( 0) *Fourier( 0, j) +frac( 1) *Fourier( 1, j) +frac( 2) *Fourier( 2, j) +frac( 3) *Fourier( 3, j)
        #wg "color white"
        #wg "up   ; goto "; 720 +int( j *200); " "; 540 -int( fn( n) *50)
        #wg "down ; goto "; 720 +int( j *200); " "; 540
    next j

    #wg "flush"

    wait

    function Fourier( n, x)
        Fourier =cos( 3.14159265 *n *x)
    end function

    sub quit h$
        close #wg
        end
    end sub