# Fourier Synthesis & Fourier Analysis of waves.

Here a pure 440 Hz sine wave was generated for 3 seconds.

This time, it is a three second voice-sample.

## Got your attention?

If you are interested in the mathematics of periodic functions, the mathematics needed is really only simple trig, 'tho. knowledge of complex numbers makes for neater symbolic treatment. However, by programming illustrations yourself you end with a really good grasp of the forward and backward Fourier transforms, ie analysis & synthesis. The programs described assume 8 bit mono 11025 Hz sampled wav files. They could easily be modified to read other wav file formats. The programs are very definitely not 'real time'. This has the side effect that they run slowly enough to see what is going on!
I recommend also the NCH software for creating sound samples- eg pure tones, sweeps, etc- and converting them between formats.

## Statement 1. Fourier Synthesis

Any repetitive waveform can be synthesised by adding sinusoidal components of varied frequency, amplitude and phase. ( Incidentally other component shapes also work) A sine wave of phase other than zero is equivalent to a mixture of a sin wave and a cos wave at that frequency, but of different amplitudes. Using separate sin and cos components means the arithmetic does not need 'complex number' calculations. Often we are only interested in amplitudes, which we can get from the square root of the sum of the sin and cosine components; the phase if needed is atn( RealComponent/ImagComponent).

Imagine a series of slider controls which can fade up and down a whole series of pure tones at a series of frequencies from the lowest you can hear (below 30 Hz) to the highest (say 20 kHz if you are young!).

By trial and error you can manipulate them until the output matches any supplied tone. That's good enough for the ear, which detects frequencies but not phases. To match them as displayed on an oscilloscope, you have to be able to shift the phase of each component, or, as above, use both sin and cos components.

## Statement 2. Fourier Analysis

The frequency, amplitude and phase of the components can be extracted from any repetitive wave.

In the real world, the signal- perhaps an audio sample- will be digitally sampled. It will keep changing, so we have to keep checking, requiring short sample periods. But short sample periods mean you will not be able to detect low frequencies. The digital processes will also generate false signals- 'aliasing'- if your sampling and signal frequencies aren't multiples of each other. And you've got to be careful not to sample signals at above half their highest frequency component- the 'Nyquist criterion'.
In 'frequency domain' we are interested in how much of each component frequency is present in a 'signal' wave.

## Making waves.

In 'time domain' we simply draw the sound amplitude as a graph against frequency. It looks like a classic 'oscilloscope'. Built my first, valve & crt based, in about 1965. Nowadays I'd use data-grabbing software. The software just calculates what the various components add up to, and saves the data as a wav file which it then plays. The wav file needs a header- which is supplied as a file containing the necessary parameters, viz specifying 8-bit, 11 kHz, mono, length 3 seconds.

If you are happy with sin waves, it is easy to generate a single pure tone; one which gives a decaying amplitude tone; a swept-frequency constant amplitude sample; an amplitude-modulated tone; 'beats'; a square or triangular wave; etc.

Given the data on frquencies and amplitudes over time, you can synthesise a plucked string or a human voice speaking... I've even had fun generating waveforms that look like a human face profile on an oscilloscope display!

## Breaking waves

In 'frequency domain' we are interested in which frequencies appear in a chosen section of a wave, and with what amplitude. My program implements this in BASIC, using the definition of Fourier Analysis. At each frequency of interest, the wav amplitude is multiplied at each sample time point by the value of the sine wave frequency at that same time, and the total of these products is accumulated. This gives for each frequency a number showing how much of that frequency sin wave was contained. You do this again for the 90 degree shifted cosine components. The result is a graph of how much of each frequency there was in the sample.

In BASIC the multiple loops take ages. It can be shortened ( the 'Fast Fourier Transform') but then it is harder to see what is happening. More practically, you'd use a pre-compiled fft dll ( the 'Fastest Fourier Transform in the West'- fftw.dll.) However, programming the transform in terms of its definition, and applying it to real waves, is a great way to get real understanding.

If you choose to install the SOX project software ( free download) you get a command-line ability to do all sorts of audio processing and transforming, including producing (Fourier) spectrograms. I'll put up a proper LB frontend to this soon... Typical result below confirms my slow Fourier results- but is produced MUCH quicker.

Fourier transformation is a magic idea and well worth investigating for its implications and applications in sound, images, etc. You can do things like pass-band filtering; frequency up and down converting; voice analysis.
It's extension into 2D allows fascinating work in image analysis, and in 3D in tomography. For images, mix ImageMagick with LB and you have the needed high speed. For 3D, you might wait several days unless you can write machine-level code on a fast machine!!

There are excellent WikiPedia articles on Fourier Transforms, Phonograms, Voiceprints and Spectrum Analysis. I also recommend Wolfram MathWorld
Download some of my other programs for Liberty /Just BASIC here.