Fall 2004

# Mathematics Math21b Fall 2004

## Linear Algebra and Differential Equations

Office: SciCtr 434
Email: knill@math.harvard.edu

# Soundmanipulation and Linear Algebra

Analoguously to image manipulation, we can also do sound manipulation with Mathematics and Mathematica.

A piece of music is just a function f(t). Alternatively, given a function f, you can play it. Mathematica allows to do that with the command "Play".

 ```f[x_]:=Sin[3000 2Pi x] Play[f[x],{x,0,10}] ```

for example, plays a 3000 Herz sound for a few seconds.

 A Scherzo of Chopin: a function f Mixture of the two pieces: the function (f+g)/2. Accordeon Musik. A function g.

 The mixture of the two sound pieces was obtained with the following Mathmematica commands: (The Mathematica code is a bit more complicated then anticipated because by building the song vector (a list of sampled amplitudes), the left and right sound channel are built differently). ```a=Import["scherzo.wav"]; b=Import["wirke.wav"]; type=a[[1,2]]; n=Length[a[[1,1,1]]]; m=Length[b[[1,1,1]]]; nm=Min[n,m]; song={ Table[(a[[1,1,1,k]]+b[[1,1,1,k]])/2,{k,nm}], Table[(a[[1,1,2,k]]+b[[1,1,2,k]])/2,{k,nm}] }; c=Sound[SampledSoundList[song,type]]; Export["mix.wav",c,"WAV"] ``` Here are the files: you first have to convert the mp3 into .wav files. We did not include the .wav files due to size. You can convert it easily. in linux for example, type at the commandline ```mpg123 -w scherzo.wav scherzo.mp3 mpg123 -w wirke.wav wirke.mp3 ``` The conversion from .wav to .mp3 is done in linux with ```lame mix.wav ``` For Macs or PC's you can do the conversion using standard sound manipulation programs.

## Music inversion

 An other example: we take a function f(t) and apply the linear transformation T(f)(t) = f(-t). Lets do that with the Scherzo: ```a=Import["scherzo.wav"]; type=a[[1,2]]; n=Length[a[[1,1,1]]]; song={ Table[a[[1,1,1,n-k]]/2,{k,0,n-1}], Table[a[[1,1,2,n-k]]/2,{k,0,n-1}] }; c=Sound[SampledSoundList[song,type]]; Export["scherzo_reversed.wav",c,"WAV"] ``` Here are the files:

## Speed manipulation

 An other example: we take a function f(t) and apply a linear transformation T(f)(t) = f(g(t)). Lets do that with an Enya song "adiemus" ```a=Import["adiemus.wav"]; type=a[[1,2]]; n=Length[a[[1,1,1]]]; song={ Table[a[[1,1,1,Floor[k+(n/10) Sin[Pi*k/n]] ]]/2,{k,0,n-1}], Table[a[[1,1,2,Floor[k+(n/10) Sin[Pi*k/n]] ]]/2,{k,0,n-1}] }; c=Sound[SampledSoundList[song,type]]; Export["rap.wav",c,"WAV"] ``` Here are the files:

## A simple synthesizer

 ( More details in the Fourier exhibit). ```PlaySong[hull_,tune_,name_,ground_]:=Module[{}, u=ToCharacterCode[name]; soundfilename=FromCharacterCode[Join[u,{46,119,97,118}]]; scale[n_]:=ground*2^(n/12); beatlength=1/5; songlength=Length[tune]*beatlength; frequency[x_]:=tune[[1+Floor[x/beatlength]]]; song[t_]:=hull[scale[frequency[t]]*t]; P=Play[song[t],{t,0,songlength}]; Export[soundfilename,P,"WAV"]; ] t1={0,0,4,4,7,7,5,4,2,2,0,4,7,4,0,0,0,0,0,2,0}; f1[x_]:=Sin[x]; n1="sin"; g1=2000; PlaySong[f1,t1,n1,g1] ```

Back to the main page