Digital Sound & Music: Concepts, Applications, & Science, Chapter 2, last updated 6/25/2013
38
from 0 to 7 in single quotes rather than use the name of the color. The linspace function is the
same in Octave as in MATLAB. To play the sound, you need to use the playsound function
rather than wavplay. You also can use a wavwrite function (which exists in both MATLAB and
Octave) to write the audio data to an external file. Then you can play the sound with your
favorite media player.
There is no square or sawtooth function in Octave. To create your own sawtooth, square,
or triangle wave in Octave, you can use the Octave programs below. You might want to
consider why the mathematical shortcuts in these programs produced the desired waveforms.
function saw = sawtooth(freq, samplerate)
x = [0:samplerate];
wavelength = samplerate/freq;
saw = 2*mod(x, wavelength)/wavelength-1;
end
Program 2.1 Sawtooth wave in Octave
function sqwave = squarewave(freq, samplerate)
x = [0:samplerate];
wavelength = samplerate/freq;
saw = 2*mod(x, wavelength)/wavelength-1; %start with sawtooth wave
sawzeros = (saw == zeros(size(saw))); %elminates division by zero in next
step
sqwave = -abs(saw)./(saw+sawzeros); %./ for element-by-element division
end
Program 2.2 Square wave in Octave
function triwave = trianglewave(freq, samplerate)
x = [0:samplerate];
wavelength = samplerate/freq;
saw = 2*mod(x, wavelength)/wavelength-1; %start with sawtooth wave
tri = 2*abs(saw)-1;
end
Program 2.3 Triangle wave in Octave
2.3.6 Transforming from One Domain to Another
In Section 2.2.3, we showed how sound can be represented graphically in two ways. In the
waveform view, time is on the horizontal axis and amplitude of the sound wave is on the vertical
axis. In the frequency analysis view, frequency is on the horizontal axis and the magnitude of
the frequency component is on the vertical axis. The waveform view represents sound in the
time domain. The frequency analysis view represents sound in the frequency domain. (See
Figure 2.18 and Figure 2.19.) Whether sound is represented in the time or the frequency domain,
it's just a list of numbers. The information is essentially the same it's just that the way we look
at it is different.
Previous Page Next Page