Creating and Using A Wavetable in MATLAB
Creating and Using A Wavetable in MATLAB
MATLAB EXERCISE
1
2
3
128
0.0490676743274180
0.098017140329561
0.146730474455362
-2.449293598294706e16
wavetable
MATLAB EXERCISE
Page 1
This material is based on work supported by the National Science Foundation under CCLI Grant DUE 0717743, Jennifer Burg PI, Jason Romney, Co-PI.
The wavetable is actually the list of values on the left, stored in memory. The figure simply
illustrates the fundamental waveform that these values correspond to.
A wavetable such as the one shown can be used as the basis of more than its
fundamental frequency. By varying the rate at which values are output, harmonic
frequencies can be produced. For example, reading every other value results in a
waveform of twice the frequency of the fundamental. Reading each value twice (or adding
one in between each two by interpolation) results in a waveform of half the frequency of
the fundamental.
The phase of the waveform can also be varied by starting at an offset from the
beginning of the wavetable. To start at a phase offset of
radians, you would start
reading at index . For example, to start at an offset of
in the wavetable of Error!
Reference source not found., you would start at index
.
To generate a waveform that is not a harmonic of the fundamental frequency, it's
necessary to move from one index to another in the table by adding the correct increment.
This increment i depends on the desired frequency f, the table length N, and the sampling
rate r, defined by
. For example, to generate a waveform with frequency 750 Hz
using the wavetable of Error! Reference source not found. and assuming a sampling rate
of 48000 Hz, you would need an increment of
.
We've chosen an example where the increment is an integer, which is good because
the indexes into the table have to be integers. What if you wanted a frequency of 390 Hz?
Then the increment would be
, which is not an integer. In cases where
the increment is not an integer, interpolation must be used. For example, if you want to go
an increment of 1.04 from index 1, that would take you to index 2.04. Assuming that our
[ ]
[ ]
[ ] .
wavetable is called table, you want a value equal to
This is a simple way to do interpolation. More complex, curve-shaping forms are also
possible.
The Assignment
Your assignment is to write the following MATLAB functions:
[table, N] = wavetable(f, r), which generates a wavetable called table that contains one cycle
of a sinusoidal waveform of frequency f with a sampling rate of r. The number of samples
in the table, N, is also returned.
[ i, waveform ] = makeSoundWithWavetable( table, r, freqDesired, s) sends in a wavetable table,
its sampling rate r, the desired frequency of a sound to generate freqDesired, and the desired
length of the sound in seconds s. The function plays the sound of the desired waveform within
the function and sends back the vector of values in waveform and the increment used to get these
values i. Use the simple interpolation method explained above. Have your function plot one
cycle of waveform.
MATLAB EXERCISE
Page 2
This material is based on work supported by the National Science Foundation under CCLI Grant DUE 0717743, Jennifer Burg PI, Jason Romney, Co-PI.