# Mus320

## Contents

## HW 1 FAQ

- Question #1, etc

## Common Matlab Trickiness (Please feel free to add additional hints!)

- In matlab, vector and matrix indices start at 1 (not 0). For example, to access the first element in the vector x, use x(1). If you are familiar with other programming languages such as C or C++, this might cause some slight difficulties in the beginning.

- Sound I/O on matlab is pretty basic. For the linux machines, audio will sometimes be a problem if: a web browser is playing audio, JACK is running, some other audio program is locking the audio resources on your computer.

- When writing functions, the name of the saved file must be the same as the function name.

- When using built in functions such as min and max, be careful note to which dimension the operator is working on. Typically, matlab operators work on columns.

- The transpose operator ' (apostrophe) acts as a Hermitian transpose. The Hermitian transpose takes the complex conjugate as well as the normal transpose of a matrix. To take avoid taking the complex conjugate you can use .' (I'm guessing we will usually use the Hermitian transpose).

## [Home http://ccrma.stanford.edu/courses/320/]

## Matlab Demo Code

%% General matlab stuff

%% Scalars % scalars: real and imaginary

% angle, abs, sin, exp

% Simple Operators

% Simple math functions

%% vectors

% Functions of vectors: min, max, sum

% concatenate vectors with [] row wise of ; for column wise

% Simple generators of vectors: zeros, ones, randn

% How to make a sine wave: calling function

s440 = sinGen(440, fs, fs); sound(s440, fs);

% AM modulation-> using the .* % AM: phone call

am = sinGen(440, fs, fs).*sinGen(25, fs, fs); sound(am,fs)

% FM: more crazy -> using the .*

t = 0:2*fs; fc = sin(2*pi*200*t/fs); fm = sin(2*pi.*fc.*t/fs);

%% Matrix

m = [1 2 3; 4 5 6; 7 8 9];

% How to define matrices: stereo sine wave with two different frequecies

s880 = .8*sqrGen(880, fs, fs); sound(s880, fs); ss = [ s440 s880 ]; sound(ss, fs);

% Matrix multiplication: mixing the left and right signals together

mixer = [0 1; 1 0];

mixed = ss*mixer; sound(mixed, fs);

% Functions on vectors and matrices: operate on columns

% min and max, etc

%% Creating functions % -> take a look at a pre-made function (sinGen)

% -> make a function (sqrGen) using sign

% functions can return more than one parameter

% Plotting, Lets plot something more fun, a smiley face!

% Create the head theta = 0:.01:2*pi; r = 1.0; x = r*cos(theta); y = r*sin(theta);

plot(x, y)

% Create the eyes hold on; plot([-.5 .5], [.5 .5], 'o')

% Make eye's bigger plot([-.5 .5], [.5 .5], 'x', 'MarkerSize', 14, 'LineWidth', 5)

% Make smile t = -.5:.01:.5 smile = -cos(2*t)+.25; plot(t, smile, 'r', 'LineWidth', 5) Control Statements

t = -.5:.01:.5 % Animate the smile with if statement

dead = 0;

for i=1:30

plot(x, y, 'LineWidth', 5) hold on;

% Create the eyes if(dead==1) plot([-.5 .5], [.5 .5], 'x', 'MarkerSize', 14, 'LineWidth', 5) else plot([-.5 .5], [.5 .5], '+', 'MarkerSize', 14, 'LineWidth', 5) end

% Make smile smile = -.1*cos(2*t)-.25; plot(t,smile, 'r', 'LineWidth', 5)

pause(.2); hold off;

end

%% Loading Audio!

clear all; close all; clc;

% Load the wave file % make sure the file you load is in the same folder as its longer than 3 % seconds

[audioClip, fs] = wavread('nameOfAudioFile');

% Listen!

sound(audioClip,fs);

% Plot

plot(audioClip)

% Shorten the clip

shortClip = audioClip(1:2.75*fs);

% Listen!

sound(shortClip, fs);

% Listen Slow!

sound(shortClip);

% Plot

plot(shortClip)

% Create time axis

time = 0:length(shortClip)-1; time = time/fs;

% Plot with seconds as the axis

plot(time, shortClip)

xlabel('time (sec)') ylabel('amplitude') title('popping bottles, by Birdman feat. Lil Wayne') grid on;

% Put amplitude envelop

% ramp up for 1/2 second

rampUp = linspace(0, 1, fs/2);

% ramp down for 1 second

rampDown = linspace(1, 0, fs);

% Transpose ramps

rampUp = rampUp'; rampDown = rampDown';

% create envelop signal

env = [rampUp ; ones(length(shortClip)-length(rampUp)- length(rampDown), 1); rampDown];

% apply envelop

sig = shortClip.*env;

% Plot both!

plot(sig) hold on; plot(env, 'r') legend('Signal', 'Env')

% There's a swear word, so we need to bleep it out

% create a sine wave

fo = 440; t = 1:fs/3; bleep = sin(2*pi*fo*t/fs);

sound(bleep,fs)

% "Clean" the signal

clean = sig; clean(1.2*fs:(1.2*fs + length(bleep) -1)) = bleep;

sound(clean, fs)

% bleep is too loud, scale it down

clean(1.2*fs:(1.2*fs + length(bleep) -1 )) = .5*bleep; sound(clean, fs)