MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: MATLAB to Mathematica Program Conversion Needed



Benjamin Tubb asked about translating a routine from MatLab to
Mathematica

Here is my attempt at a translation

****************************************************************************

sr=44100;time=2;freq={100,200,300,400,500};amp={1,1,1,1,1};
decay=0.003*Table[1,{i,Length[freq]}];filename="yoursound.aif";
pi2=2*Pi;
t=Table[i,{i,pi2,pi2*(1+time),pi2/sr}]; wave=Table[0,{i,Length[t]}];
Do[
    env=Exp[-Abs[decay[[i]]*Random[]/time]*Range[Length[wave]]];
    wave=wave+amp[[i]]*env*Cos[freq[[i]]*t+pi2*Random[]];,
{i,Length[freq]}
]

****************************************************************************

The command WRITESOUND must be special to the book you referenced, as I
am not familiar with it nor is it available in the help section in
Matlab Version 5.2.

-----Original Message-----
From: Benjamin Tubb <brtubb@cybertron.com> To: mathgroup@smc.vnet.net
Subject: [mg12307] [mg12260] MATLAB to Mathematica Program Conversion Needed


>Anyone who has the "luxury" of having MATLAB and Mathematica who can
>convert the following MATLAB code for Mathematica, please do so. It
>would be greatly appreciated. It is from the book "Tuning, Timbre,
>Spectrum, and Scale" by William A. Sethares (1998).
>
>MATLAB code version:
>--------------------
>%
>% Additive Synthesis
>%
>% sr = sampling rate
>% time = duration of wave to generate (seconds) % freq = frequencies of
>partials
>% amp = amplitudes of partials
>% decay = decay rate of partials, (big=fast  small=slow) % filename =
>saves to this filename, use filename.aif for %             mac and
>filename.wav for PC %
>sr=44100; time=2; freq=[100 200 300 400 500]; amp=[1 1 1 1 1];
>decay=.003*ones(size(freq)); filename = 'yoursound.aif';
>
>pi2=2*pi; t=pi2:pi2/sr:pi2*(1+time); wave=0*ones(size(t));
>for i=1:length(freq)
>  env=exp(-abs(decay(i)*rand/time)*[1:length(wave)]);
>  wave=wave+amp(i)*env.*cos(freq(i)*t+pi2*rand); end
>writesound(wave,sr,filename)
>--------------------
>
>Thanks in advance for any help.
>
>----------------
>Benjamin Tubb
>brtubb@cybertron.com
>http://home.cybertron.com/~brtubb
>
>
>




  • Prev by Date: Re: Matrix Multiplication
  • Next by Date: Re: Finding Optimal Integer Ratio for Any Decimal Fraction
  • Prev by thread: MATLAB to Mathematica Program Conversion Needed
  • Next by thread: Formating output for email