[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[sc-users] Converting Physical Model Code from Matlab



Hello,

I'm trying to convert some code for a physically based model in Matlab to an SC synth def. The model below represents an oscillating air bubble in water. In Matlab it would be possible to synthesise an array of pressure values which would be automatically scaled. If I read data into a buffer in SC, is it possible to do the same?? 

I'm also getting an error in the last line of the loop in the code below, maybe a syntax thing to do with the beta value?? Im not so sure about mathematical syntax in SC, I don't know how to multiply arrays with each other ("dot" product), hence the need for the loop, I can't work out why I'm getting an error though...

Thanks,

Patrick

Code:

 SynthDef(\bubble2,{
var sr,c,bit,x,f2,r,r2,pres,pres2,rho,gamma,eps,f,gth,deltaTh,deltaRad,deltaTot,beta,xi;
    sr = 44100;
    //output - 1 sec
    x=FloatArray.new(sr);
    //frequency array - 1 sec 
    f2=FloatArray.new(sr);
    //Speed of sound 
    c=1497;
    bit =32;
 
    r=0.015;
    r2=FloatArray.new(sr);
 
    //Minneart
    pres=100000;
    pres2=FloatArray.new(sr);
    rho=1000;
    gamma=1.4;
    eps=0.05;
 
    f=(1/(2*pi))*sqrt(3*gamma*pres/(rho*(r**2)));
 
    //Vertical velocity
    xi=0.1;
 
    //Damping constant
    gth=1.6*10**6;
 
    //Thermal Damping
    deltaTh=sqrt(9*(gamma-1)**2/(4*gth)*f);
 
    //Radial Damping
    deltaRad=sqrt(f*r/c);
 
    //Total Damping
    deltaTot=abs(deltaTh)+deltaRad;
 
    //Decay rate due to damping
    beta=pi*f*deltaTot;
    
 for(1,sr,{arg i;
    //Array of subsequent frequencies
    f2(i)=f*(1+xi*beta*(i)/sr);
 
    //Resulting pressure wave emitted from bubble
    x(i)=eps*r*sin(2*pi*f2(i))*(i/sr)*exp(-beta*i/sr);
    
 });
 }).load(s);
 
 )





The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.