17.01.2013 Views

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

musicdsp.org source code archive - WSInf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

DSF (super-set of BLIT) (click this to go back to the index)<br />

Type : matlab <strong>code</strong><br />

References : Posted by David Lowenfels<br />

Notes :<br />

Discrete Summation Formula ala Moorer<br />

computes equivalent to sum{k=0:N-1}(a^k * sin(beta + k*theta))<br />

modified from Emanuel Landeholm's C <strong>code</strong><br />

output should never clip past [-1,1]<br />

If using for BLIT synthesis for virtual analog:<br />

N = maxN;<br />

a = attn_at_Nyquist ^ (1/maxN); %hide top harmonic popping in and out when sweeping frequency<br />

beta = pi/2;<br />

num = 1 - a^N * cos(N*theta) - a*( cos(theta) - a^N * cos(N*theta - theta) ); %don't waste time on beta<br />

You can also get growing harmonics if a > 1, but the min statement in the <strong>code</strong> must be removed, and the scaling will be weird.<br />

Code :<br />

function output = dsf( freq, a, H, samples, beta)<br />

%a = rolloff coeffecient<br />

%H = number of harmonic overtones (fundamental not included)<br />

%beta = harmonic phase shift<br />

samplerate = 44.1e3;<br />

freq = freq/samplerate; %normalize frequency<br />

bandlimit = samplerate / 2; %Nyquist<br />

maxN = 1 + floor( bandlimit / freq ); %prevent aliasing<br />

N = min(H+2,maxN);<br />

theta = 2*pi * phasor(freq, samples);<br />

epsilon = 1e-6;<br />

a = min(a, 1-epsilon); %prevent divide by zero<br />

num = sin(beta) - a*sin(beta-theta) - a^N*sin(beta + N*theta) + a^(N+1)*sin(beta+(N-1)*theta);<br />

den = (1 + a * ( a - 2*cos(theta) ));<br />

output = 2*(num ./ den - 1) * freq; %subtract by one to remove DC, scale by freq to normalize<br />

output = output * maxN/N; %OPTIONAL: rescale to give louder output as rolloff increases<br />

function out = phasor(normfreq, samples);<br />

out = mod( (0:samples-1)*normfreq , 1);<br />

out = out * 2 - 1; %make bipolar<br />

Comments<br />

from : David Lowenfels<br />

comment : oops, there's an error in this version. frequency should not be normalized until after the maxN calculation is done.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!