Now let's look at an example of how you can manipulate complex numbers in a database. It's a theoretical idea whose practical implementations haven't been completely tested yet. This example deals with data representing a sound wave. You can store the data representing the sound wave in a SQL Server table with each row representing a sample of a sound level at a certain point in time (spatial domain). Storing the data this way is more convenient for generating a sound wave file from the table's data. The script to create the SoundWave table might look like this:
CREATE TABLE SoundWave ( time_index int NOT NULL PRIMARY KEY, sound_level decimal(19, 9) NOT NULL )
Suppose this table holds data for a 5-minute CD-quality track. With a sample rate of 44.1 kHz, the table will hold about 13,230,000 rows (44,100 samples per second x 300 seconds). We want to produce a filtered sound wave (the original sound with certain sound effects or equalizing effects) based on a filter that exists in a table called Filter, and store the sound wave in the table FilteredSoundWave. The code to create the Filter table might look like this:
CREATE TABLE Filter ( frequency_index int NOT NULL PRIMARY KEY, filter_vector complex NOT NULL )
The problem with this setup is that the Filter table holds data in the frequency domain, whereas the SoundWave table holds data in the spatial domain. Before applying the filter to the sound wave, you need to transform the sound wave data to the frequency domain. For this, you can use Fourier complex transformations, which are mathematical algorithms that perform such transformations. I won't discuss Fourier transformations in detail, but let's pretend you have access to functions that implement Fourier transformations. You apply Fourier transformations to the base data and store the results in the table TransformedSoundWave, which you can create with the following code:
CREATE TABLE TransformedSoundWave ( frequency_index int NOT NULL PRIMARY KEY, signal_vector complex NOT NULL )
Each row in the TransformedSoundWave table holds the frequency index and a complex number. The complex number's vector size represents the signal's magnitude, and the angle of the vector represents the signal's phase shift. Now, applying the filter to the sound wave is simple: You just perform complex multiplication between the filter's vectors and the sound wave's vectors. Suppose you want to store the filtered sound wave in the frequency domain in the TransformedFilteredSoundWave table, which has the same structure as the TransformedSoundWave table. You can issue the query in Listing A to store the sound wave.
Now all you need to do is apply Fourier's inverse transformation to transform the filtered sound wave in the frequency domain back to a sound wave in the spatial domain. Suppose you store the results in the FilteredSoundWave table, which has the same structure as the SoundWave table. Besides saving all the round-trips between the client and the server, using this storage method lets you store and manage your sound data centrally in the database server. Later, you can execute a simple process to download the sound wave to a .wav file.
This simple example shows how you can use complex functions in your database. In practice, you might not need the intermediate tables that I presented here because you can also perform certain calculations by embedding functions in your queries instead of storing the intermediate results in tables. You can apply a process similar to the one I described here to image processing, where graphic filters are used to produce image effects.