Octave is a freeware, open-source version of MATLAB distributed by GNU. It has many but not all of the functions of MATLAB. There are versions of Octave for Linux, UNIX, Windows, and Mac OS X.
If you try to do the above exercise in Octave, most of the functions are the same. The fplot function is the same in Octave as in MATLAB except that for colors, you must put a digit 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