Music 320 Assignment 0 Matt Wright 10/1/3 *** Warming up: "intro" is a demo script that seems to come with matlab: >> which intro /amd/cm-home/sys/ccrma/package/matlabR12/toolbox/matlab/demos/intro.m It discusses basic matlab syntax, plotting, matrix operations, roots of polynomials, eigenvalues, etc. *** Basic operations in Matlab: After evaluating "sin(pi/4)", ans is sqrt(1/2). After evaluating "ans^2", ans is 2 "who" lists all variables, which at this point is just {ans}. After defining "x=4", "who" also lists x as a defined variable. "conj" is the complex conjugate: the result of replacing i with -1 in a complex number. "abs" is the absolute value (i.e., the magnitude of the complex number in polar coordinates) "angle" is theta (i.e., the angle of the complex number in polar coordinates) "real" is the real part, i.e., the result of replacing i with 0 in a complex number. *** Plotting in Matlab xx is a 1x5 matrix, so xx*xx (matrix multiplication) is not defined I can "fix" the problem by assuming the desire is pointwise multiplication, which would be expressed like this: yy=xx.*xx-3*xx; The same assumption and change result in zz=yy.*yy-3*yy; These commands plot yy and zz (each against xx) in one figure: subplot(2,1,1), plot(xx,yy) subplot(2,1,2), plot(xx,zz) *** Listening in Matlab xpsound is another built-in demo script: >> which xpsound /amd/cm-home/sys/ccrma/package/matlabR12/toolbox/matlab/demos/xpsound.m I note that Matlab's UI is unresponsive while a sound is playing. Matlab provides various time-domain and frequency-domain displays for sound. To create a sinusoid of 2 seconds long at 300 Hz, with sample rate 22050: dur =2.0; fs = 22050; tt =0:(1/fs):dur; xx =sin(2*pi*300*tt); "wavwrite" is the matlab procedure for writing sound to a WAV file. This command writes a 16-bit sound file with the name "sine-300hz.wav": wavwrite(xx, fs, 16, 'sine-300hz.wav') From the shell I can see that the file is indeed the desired file format: [cmn15 matt] ~> file sine-300hz.wav sine-300hz.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 22050 Hz *** Creating scripts and functions in Matlab I created the file mysound.m with these contents: function y=mysound dur =2.0; fs = 22050; tt =0:(1/fs):dur; xx =sin(2*pi*300*tt); sound(xx); wavwrite(xx, fs, 16, 'sine-300hz-again.wav') Now I can type "mysound" at the matlab prompt to hear another sinusoid and write another useless sound file to my home directory. I created the file cosinewave.m with these contents: function y=cosinewave(f, a, dur, fs) % generate a cosine wave tt =0:(1/fs):dur; %% units of seconds y =a*cos(2*pi*f*tt); I can use it like this: cos1 = cosinewave(100, 0.1, 0.1, 44100); plot(cos1) *** Experimenting with vectors and waveforms I chose to write a function to synthesize a band-limited approximation of a square wave with additive synthesis. I added a "npartials" argument to determine how many sinusoids will be added together. In my opinion, a procedure that creates a signal should just return the signal, so that other parts of the program can use the signal as they like, including plotting or playing the sound. But since the assignment demands "The function must be able to plot the waveform and listen to it", I added arguments "shouldPlot" and "shouldListen" to let the user request either of these behaviors. I created the file "blsquarewave.m" with these contents: function y=blsquarewave(f, a, npartials, dur, fs, shouldPlot, shouldListen) % generate a band-limited squarewave-like waveform with additive synthesis tt =0:(1/fs):dur; %% units of seconds % Here's what makes it a square wave: freqmultiples = ((0:npartials-1) * 2) + 1; amplitudemultiples = 1./freqmultiples; frequencies = f * freqmultiples; amplitudes = a * amplitudemultiples; result = zeros(size(tt)); for i=1:npartials result = result + amplitudes(i) * sin(2*pi*frequencies(i)*tt); end if shouldPlot plot(result); end if shouldListen sound(result, fs); end y = result; I'd like to be able to express the additive synthesis strictly with matrix operations instead of the "for" loop, but I don't know enough Matlab yet to do this.