xoxosvst

dsp blog for xoxos.net

Archive for May 2011

mass-springs 2

with 2 comments

addendum to the previous post 🙂

following a discussion at kvraudio with practiced persons such as mr. dsp himself (duncan s. parsons), further examination of this algorithm has been made.

calibration of decay time is achieved via mr. parsons keen observation that

gain at sample n = gain ^ n

go figure 🙂 as a “do things with numbers guy” (sort of the INT version of a more erudite developer) i have avoided even the simplest use of decibels. i managed to deduce that the decay time to a peak of 1/10th the initial can be calculated by:

gain = pow(0.01, 1 / samples);

lacking the methods to make observations at keener precision, i found more elementary ways to adapt this to a more serviceable decay amplitude, availing the exponential curve of the decay.

it was also posed that the oscillator form would also be useful if initialised at zero instead of in cosine form. investigation prompted me to conclude that a gain conforming sine can be derived by using an initial velocity instead of an initial position, and a cursory observation of the relation of angular frequency and peak amplitude. thereafter, arbitrary phase is trivial. i will leave the derivation to the gentle reader 🙂

..if that reader is as gentle as i (not more and not less) they will observe that the most straightforward way of modifying the initial position and initial velocity to produce arbitrary phase once more affects the amplitude of the oscillation:

0, 90, 180, 270° seem to adhere to strict unity gain.

45° offsets slightly increase gain, and the intermittent multiples of 30° i tried slightly decreased gain. in both cases by less than 1/10th, so suitability for audio applications may not be strongly affected. without further analysis i of course expect that this modulation of gain between the 90° cardinal points has some kind of continuous response, which, for the time being, escapes my procedural consideration.

Written by xoxosvst

May 31, 2011 at 10:31 pm

Posted in Uncategorized

mass-springs

leave a comment »

..the pride of my dsp retinue :p

mass-springs, as they are known, are elementary to physics simulation and so to acoustic modeling. a simple manifestation of hooke’s law:

F = -kx

where F is restoring force, k is a spring constant, and x is displacement in the spring.

my first implementation was probably at the age of twelve, making simple games and simulations on a tandy coco. no physics, only a simple application of elementary mathematics for making things bounce on the screen. the first applications in audio dsp were for a reed model and for a simple oscillator i called ‘gravity’. the mass-spring also correlates to the helmholtz resonator, or resonant volume of air found inside the body of a guitar, violin, or bottle when you blow across the top.

i was proud that the implementation i had muddled together was better than other examples i was finding online, for instance i had managed to devise a brutal and tragic way of calculating the resonant frequency. in retrospect these adjectives do apply. i will not go to the trouble of resuscitating the algorithm i published in my EPM pdf in order to comparatively illustrate the merits of the presented form.

so, you’re reading this because you searched for mass-springs and dsp and c++, or because my dsp journey fascinates you, or because you are a stalker. i address the first circumstance.

if you nose around in audio dsp you are bound to encounter the concept of angular frequency, annotated as a little omega, or lower case w.

w = 2 * pi * Hz / fs

Hz is the resonant frequency, fs is one of several notations for sampling frequency. it’s really nice in dsp when people say what the symbols mean, that doesn’t always happen.

first, a note about efficiency if your application modulates the resonant frequency: you can factor the 2 * pi out by creating a constant used for calculations of angular frequency.

initialise:   fsw = fs / 6.283185307179586476925286766559

calculate:   w = Hz / fsw

wonderfully, the resonant frequency of a mass-spring, or k, is w * w. especially given the mess i had previously, it took a while to elicit that. isn’t sharing wonderful?

so.. the cause of this blog is a wonderful experience yesterday where i reexamined my preexistent algorithm (after years of implementations). if you have used it, you are aware that the dynamic response varied with frequency. i had cleverly (again, over duration) tailored my mess to decay at a uniform rate. higher frequencies had a higher dynamic response i hadn’t been able to tame. now we are past these tribulations.

also, lets get that negative out of hooke’s law, so our resonant frequency coefficient k = –w * w

i am neither physicist nor mathematician, and cannot profess the vernacular.. so my algorithm uses two variables. i am sure there are more informed choices. i use:

mp, or position of the mass-spring..

mv, or velocity of the mass-spring..

i’m sure position is an appropriate use of the term. people with an education may smile at the use of the term velocity. there are often different equations used by educated people to describe eg. force and velocity in modeling, to me it’s just making sounds with numbers, dude 🙂 so be aware of that when continuing with this topic.

i’m so excited. this code drops a multiply on what i was using. it also has uniform dynamic response.. if you initialise with a displacement of mp =1, you get the same amplitude contour at every frequency 🙂

mv = k * mp + gain * mv;
mp += mv;

a note about this – squaring angular frequency means bad things at high values. i haven’t got there yet but i think you’re okay below fs/4, so oversample a bit if you’re using high resonant frequencies.

one of my reads during revision was http://gafferongames.com/game-physics/spring-physics/ which will reveal a super useful way to add an offset to the oscillation, so that you can ensure your mass-spring doesn’t pass a certain point on one side. lovely! 🙂 and/or, if you prefer, a collision modification. both of these methods are more efficient and elegant than the separate concoctions i was using before (eg. a 1/n softlimiter).

but wait, there’s more!

one of the reasons why it is preferable not to share source (and i hardly ever share source.. i’ll happily have lengthy, unpaid discussions about method, but other peoples source is a pita, and i imagine mine is as well.. i like one or two character variable names..) is because there are real benefits to doing all the steps yourself.

one of my failures in acoustic modeling is the 2d mesh. i’ve addressed it at at least three discrete periods. i’m not julius smith (his policy of documentation, alongside his accomplishments, establishes him as a luminary in audio dsp and acoustic modeling.. his texts are challenging without a practiced knowledge of calculus).. i don’t think like him and despite his prolific notes, i’ve never managed to make a 2d mesh that didn’t explode.

..revision of my m-s algo is intended to optimise a snare model i’ve been working on. development is good when you aren’t on a clock.. after finding my optimisation was tamed, i modeled an acoustic string. this took some wrangling due to the bounds.. the way i had of computing the force acting on each segment produced a gradual dc build. after a while, i nailed it and thought, well, that’s 1d, let’s try 2d.

the 2d also worked wonderfully. the crucial thing here is that by using mass-springs i was able to observe the building dc by using low resonant frequencies and correct my implementation. explosion in the 2d mesh happens within a dozen samples or so instead of over several minutes..

so in one 24 hour period i’ve fixed my mass-springs for ideal calibration, increased its efficiency and derived a bounded plane solution. this is a really good day in dsp.

but wait.. there’s more.

(first, straggling notes on the above material. the gain for the mass-spring has to be _very_ close to 1.0 in order to see much happen at audio rates. and also, arranging mass-springs in a line or grid with an audio rate resonant frequency.. are simply expensive ways to produce a sine 🙂 the grid may make a great looking flag, but at audio rates, flags aren’t very exciting.

so, what was the “more”..

with a gain of 1.0 (and eliminating one multiply!) the mass-spring algorithm is an efficient pure cosine oscillator – one assign faster than the fastest ‘quick sine osc’ i’ve seen elsewhere. its limitation is that amplitude changes with frequency modulation, however it has an advantage that no scaling operation is necessary for gain beyond the initial offset of the mp coefficient. there’s an additive solution in there somewhere.. perhaps percussion.

neighbor, that’s a dsp gonga :p

Written by xoxosvst

May 29, 2011 at 4:17 am

Posted in Uncategorized

Tagged with , , ,

introduction to xoxosvst blog

with one comment

thank you to sean costello of valhalladsp.com for inspiration to blog.

i have made some steps in the past to share my audio dsp gleanings. a few years ago i published a pdf, ‘elements of physical modeling,’ which amalgamated generic algorithms for the convenience of programmers burgeoning with more ideas than techniques, sort of a mini musicdsp.org.

since that publication, i’ve improved techniques that i’d thought were presented in their most elementary form, and while i have been stockpiling algorithms for a second publication, i am not eager to solute it. to an extent i am prohibiting developers from repeating my mistakes. a blog conveniently allows me to embarass myself and mislead hopeful developers without the hassle of editing or aspiring for certitude.

so, commence xoxos dsp blog.

Written by xoxosvst

May 29, 2011 at 2:09 am

Posted in Uncategorized