Spectrum by Similarity

Craig Stuart Sapp <craig@ccrma.stanford.edu>
Peabody Conservatory
14 November 2002

This Mathematica Notebook demonstrates the basic principle used to calculate a spectrum from a signal.  A spectrum is a plot of sinusoid frequencies on the x-axis versus the amplitude of that sinusoid displayed on the y-axis.  This notebook demonstrates how the amplitude of a sinusoid is calculated from any arbitrary signal.  The basic principle is this: (1) multiply the signal by a sinusoid at the desired frequency along the x-axis, and (2) sum the discrete elements in this multiplied signal together to determine the amplitude of the sinusoid present in the original signal.  This process is checking to see how similar the spectrum is to any given test sinusoid.

[Graphics:Images/index_gr_1.gif]

Part I: The Real World

First we define some variables to create a test signal.  The Amp variable stores the list of amplitudes for each sinusoid component in the signal.  Freq contains a list of the frequencies in the signal.  Periods indicates how many fundamental periods (of 100 Hz in this case) are to be displayed in the plots and used in the calculations.  Srate is the sampling rate of the signal.

[Graphics:Images/index_gr_2.gif]

The next cell defines a signal with the variable t representing time in seconds.

[Graphics:Images/index_gr_3.gif]

Here is a plot of signal[t] where the horizontal axis is in milliseconds:

[Graphics:Images/index_gr_4.gif]

[Graphics:Images/index_gr_5.gif]

The previous picture is the continuous picture of the signal, but if a computer is processing this sound, it has to do it in discrete steps.  The following command samples the signal at the sampling period (which is 1 millisecond in this case).

[Graphics:Images/index_gr_6.gif]
[Graphics:Images/index_gr_7.gif]

The following  plot shows the discrete samples just calculated in the above cell:

[Graphics:Images/index_gr_8.gif]

[Graphics:Images/index_gr_9.gif]

The following plot demonstrates that the sampled values really are derived from the continuous signal.  Note that the samples line up with the original continuous function.

[Graphics:Images/index_gr_10.gif]

[Graphics:Images/index_gr_11.gif]

Now that samples from the test signal have been generated, let's make a table of the individual sinusoids which we know are present in the signal.  The following cell makes a list of the samples for each sinusoid present in the signal.

[Graphics:Images/index_gr_12.gif]
[Graphics:Images/index_gr_13.gif]

Here is a plot of the individual frequencies.  Note that all of the amplitudes are one.  Our job is to figure out what were the original amplitudes of each sinusoid in the original signal.

[Graphics:Images/index_gr_14.gif]

[Graphics:Images/index_gr_15.gif]

[Graphics:Images/index_gr_16.gif]

[Graphics:Images/index_gr_17.gif]

[Graphics:Images/index_gr_18.gif]

In order to determine the amplitudes of the sinusoids in the original signal, you must do two steps (1) mulitply the signal by each test sinusoid, and (2) add up all of the elements in the resulting signal and then divied by the normalization factor.  These sets of number will be the original amplitudes of the sinusoids that we started out adding to create the original signal.
Below is the first step which involves multiplying the original signal by each of the test sinusoids:

[Graphics:Images/index_gr_19.gif]
[Graphics:Images/index_gr_20.gif]

Here is a plot of the data generated above.  These plots do not have any physical meaning, but are a step on the way to calculating the amplitudes of the sinewaves in the original signal:

[Graphics:Images/index_gr_21.gif]

[Graphics:Images/index_gr_22.gif]

[Graphics:Images/index_gr_23.gif]

[Graphics:Images/index_gr_24.gif]

[Graphics:Images/index_gr_25.gif]

Next, each of these multiplied signals need to be added, sample by sample:

[Graphics:Images/index_gr_26.gif]
[Graphics:Images/index_gr_27.gif]

Finally, normalize the values:

[Graphics:Images/index_gr_28.gif]
[Graphics:Images/index_gr_29.gif]

Now compare this result to the amplitudes of the sinusoids present in the original signal:

[Graphics:Images/index_gr_30.gif]
[Graphics:Images/index_gr_31.gif]

Part II: The Complex World

You saw in the previous section that the amplitudes of the original sinusoids can be extracted by multiplying the spectrum by the individual sinusoids that we want to find the amplitude of in the signal.  In practice, we will need a more comprehensive sinusoid, called the complex sinusoid.  This sinusoid can handle any phase shift of the real sinusoids found in the signal and will be demonstracted in the next section.
A complex sinusoid is a combination of a sinewave and a cosinewave which are at right angles to each other.  Here is a plot of a complex sinusoid:

[Graphics:Images/index_gr_32.gif]

[Graphics:Images/index_gr_33.gif]

The horizontal axis represent the real numbers, while the vertical axis represents the Imaginary numbers.  The origin of the complex sinusoid is on the left, and you can see that the green projection of the sinusoid creates a cosine.  The blue projection of the complex sinusoid (onto the imaginary axis) creates a sinewave.  Thus the mathematical equation for the complex sinusoid is:    csin(x) = cos(x) + i sin(x), where i is the square root of negative one, and csin is the complex sinusoid.
The complex sinusoid itself can be represented as an imaginary power of the number e (approximately equal to 2.71828): e^(i x).  Here is a demonstration that there are two equal ways of calculating the value of a complex sinusoid (Euler's Identity):

[Graphics:Images/index_gr_34.gif]
[Graphics:Images/index_gr_35.gif]
[Graphics:Images/index_gr_36.gif]
[Graphics:Images/index_gr_37.gif]

Since the difference between ans1 and ans2 is zero, the methods are equal.  Try it for any other value of x to verify that the calculations are still equal.

Now let us use the complex sinusoids to measure the amplitudes of the original sinusoids used to make the signal.  Here is the original signal:

[Graphics:Images/index_gr_38.gif]
[Graphics:Images/index_gr_39.gif]

Analygous to part I, create a set of test sinusoids which will be used to examine the signal:

[Graphics:Images/index_gr_40.gif]
[Graphics:Images/index_gr_41.gif]
[Graphics:Images/index_gr_42.gif]
[Graphics:Images/index_gr_43.gif]
[Graphics:Images/index_gr_44.gif]
[Graphics:Images/index_gr_45.gif]

Finally, normalize:

[Graphics:Images/index_gr_46.gif]
[Graphics:Images/index_gr_47.gif]

Now compare this to the amplitudes of the sinusoids present in the original signal:

[Graphics:Images/index_gr_48.gif]
[Graphics:Images/index_gr_49.gif]

These amplitudes also exactly match the amplitudes found by using only real sinusoids.

Part III: Phase and why the complex world is better than the real world

The complex sinusoid is much more accurate at finding the original sinusoid amplitudes.  This section will demonstrate this fact.  A problem occurs when you try to find the amplitude of a sinusoid component in a signal which has an unknown phase.  In part I, the phase was exactly 0 (or 90 degrees if you are thinking of sinewaves).  But what happens if the phase of each sinusoid is random?  Real sinusoids cannot extract the full amplitude of the original sinusoid in the signal, and this amplitude will more than likely be underreported.
Lets test this theory by creating a signal which contains the same sineusoids as in the previous signal which also have the same amplitudes, but have arbitrary phases:

[Graphics:Images/index_gr_50.gif]

The next cell defines a signal with the variable t representing time in seconds.

[Graphics:Images/index_gr_51.gif]

Here is a plot of signal[t] where the horizontal axis is in milliseconds:

[Graphics:Images/index_gr_52.gif]

[Graphics:Images/index_gr_53.gif]

Notice that the above signal with different phases for each sinusoid  looks different from the original signal:

[Graphics:Images/index_gr_54.gif]

[Graphics:Images/index_gr_55.gif]

The previous picture is the continuous picture of the signal, but if a computer is processing this sound, it has to do it in discrete steps.  The following command samples the signal at the sampling period (which is 1 millisecond in this case).

[Graphics:Images/index_gr_56.gif]
[Graphics:Images/index_gr_57.gif]

The following  plot shows the discrete samples just calculated in the above cell:

[Graphics:Images/index_gr_58.gif]

[Graphics:Images/index_gr_59.gif]

The following plot demonstrates that the sampled values really are derived from the continuous signal.  Note that the samples line up with the original continuous function.

[Graphics:Images/index_gr_60.gif]

[Graphics:Images/index_gr_61.gif]

[Graphics:Images/index_gr_62.gif]
[Graphics:Images/index_gr_63.gif]
[Graphics:Images/index_gr_64.gif]
[Graphics:Images/index_gr_65.gif]

Finally, normalize (divide) by the number of samples per fundamental frequency (10 samples from 100 Hz):

[Graphics:Images/index_gr_66.gif]
[Graphics:Images/index_gr_67.gif]

Now compare this to the amplitudes of the sinusoids present in the original signal:

[Graphics:Images/index_gr_68.gif]
[Graphics:Images/index_gr_69.gif]

Notice that the amplitudes this time are not equal to the amplitudes of the sinusoids.  Depending on the phase of the sinusoids, the measured amplitudes will range from the full amplitude to the negative full amplitude.

Measuring the correct original amplitude

The only way to extract the original amplitudes from the signal is to use complex sinusoids which resist the effects of a change in phase.  This section demonstrates that you can measure the correct original amplitudes of the sinusoids from the original signal.

[Graphics:Images/index_gr_70.gif]
[Graphics:Images/index_gr_71.gif]
[Graphics:Images/index_gr_72.gif]

The amplitude is taken as the magnitude of the complex number, which is also the absolute value of the complex number:

[Graphics:Images/index_gr_73.gif]
[Graphics:Images/index_gr_74.gif]

Finally, normalize (divide) by the number of samples per fundamental frequency (10 samples from 100 Hz):

[Graphics:Images/index_gr_75.gif]
[Graphics:Images/index_gr_76.gif]

Now compare this to the amplitudes of the sinusoids present in the original signal:

[Graphics:Images/index_gr_77.gif]
[Graphics:Images/index_gr_78.gif]

These amplitudes also exactly match the amplitudes of the arbitrary-phased signal, while using the real test sinusoids did not give the correct amplitudes.

That is not all -- you can also determine the phase of the orignal sinusoids in the signal:

[Graphics:Images/index_gr_79.gif]
[Graphics:Images/index_gr_80.gif]

Here is the original phase, which matches exactly:

[Graphics:Images/index_gr_81.gif]
[Graphics:Images/index_gr_82.gif]

Part IV: Symmetry

Even and Odd functions

The problem with using only a real sinsoid is that you have to choose a sinusoid which will not correctly identify the amplitude of another sinusoid with the same frequency.  This section demonstrates this fact.

[Graphics:Images/index_gr_83.gif]

[Graphics:Images/index_gr_84.gif]

[Graphics:Images/index_gr_85.gif]

[Graphics:Images/index_gr_86.gif]

If you multiply the sine and the cosine functions together, you will get a sinewave with twice the frequency of the original:

[Graphics:Images/index_gr_87.gif]

[Graphics:Images/index_gr_88.gif]

This implies that there is no Cosine quality in a Sine and no Sine in a Cosine.  However, if you multiply a sine by a sine, all of the function will be positive:

[Graphics:Images/index_gr_89.gif]

[Graphics:Images/index_gr_90.gif]

[Graphics:Images/index_gr_91.gif]
[Graphics:Images/index_gr_92.gif]
[Graphics:Images/index_gr_93.gif]
[Graphics:Images/index_gr_94.gif]

Thus, a sinewave cannot "see" a cosinewave and a cosinewave cannot "see" a sinewave.  This is related to the symmetry of each function.  A cosine was has even symmetry because it looks the same on each side of the line x=0 if you place a mirror on that line.  A sine function has odd symmetry because it can be rotated 180 from the point (x,y) = (0,0) and still look the same.   All functions can be broken down into two pieces: one which has even symmetry, and one which has odd symmetry.

An arbitrary phased sinusoid which is neither a sinewave or a cosine wave can be broken down into two pieces, one which is symmetric and one which is antisymmetric.  The symmetric piece corresponds to a cosine and the antisymmetric piece corresponds to a sine.  The amplitudes of the cosine and sine add up to the amplitude of the original arbitrary-phased sinusoid.

For example, try a phase of Pi/4 which is half-way between a cosine with 0 phase and a sine with 0 phase (or a cosein with Pi/2 phase):

[Graphics:Images/index_gr_95.gif]

[Graphics:Images/index_gr_96.gif]

This sinusoid can be broken down into two sinusoids: one sinewave and one cosine which can be added together to create the original phased sinusoid:

[Graphics:Images/index_gr_97.gif]

[Graphics:Images/index_gr_98.gif]

Check to see that the functions are equal by plotting their difference:

[Graphics:Images/index_gr_99.gif]

[Graphics:Images/index_gr_100.gif]

Here is a function which will extract the sine and cosine of any sinusoid:

[Graphics:Images/index_gr_101.gif]

[Graphics:Images/index_gr_102.gif]

[Graphics:Images/index_gr_103.gif]

[Graphics:Images/index_gr_155.gif]

With one sine and one cosine, you can extract the amplitude of a sinusoid with any phase in a signal.  Remember that the complex sinusoid is a cosine on the real axis and a sine on the imaginary axis, so you now know why the complex sinusoid can extract the amplitude of a sinusoid which has any phase.

V Spectrum plots

The spectrum is usually given as a plot of amplitude v frequency of sinusoids, but there is also the phase plot which is the other part of the spectrum.

[Graphics:Images/index_gr_156.gif]
[Graphics:Images/index_gr_157.gif]
[Graphics:Images/index_gr_158.gif]

[Graphics:Images/index_gr_159.gif]

[Graphics:Images/index_gr_160.gif]
[Graphics:Images/index_gr_161.gif]
[Graphics:Images/index_gr_162.gif]

[Graphics:Images/index_gr_163.gif]

Here are functions to display the sinusoid frequencies, amplitudes and phases all in one spectral plot:

[Graphics:Images/index_gr_164.gif]
[Graphics:Images/index_gr_165.gif]

[Graphics:Images/index_gr_166.gif]

It is interesting to view the continuous version of the spectrum in 3D:

[Graphics:Images/index_gr_167.gif]
[Graphics:Images/index_gr_168.gif]

[Graphics:Images/index_gr_219.gif]


Converted by Mathematica      November 23, 2002