% Simplest Plucked String model: Karplus Strong Algorithm % This matlab function does a simple implemation of the Karplus Strong % Plucked String model. It uses the following difference equation. % y[n] = x[n] + y[n-N] + y[n-(N+1)] % % pluck(freqHz, iterations, fs) % % freqHz : frequency in Hz (rouding occurs when computing delay line!) % iterations : # of loops (duration of sound file) % fs : sampling frequency % % eg. signal = pluckedStringKS(200, 5000, 22050); % % Tae Hong Park, Jan 2001 % % Princeton Univ. % Music Department % park@silvertone.princeton.edu function signal = pluckedStringKS(freqHz, iterations, fs) % Echo some useful information % --------------------------------------------------------------------------- N = fs/freqHz; disp(sprintf('\nActual delay : \t%0.5g', N)) N = floor(N); disp(sprintf('Floored delay : \t%0.5g', N)) disp(sprintf('Frequency : \t%0.5g', freqHz)) disp(sprintf('duration (msec): \t%0.5g', 1000*iterations/fs)) disp(sprintf('fs : \t%0.5g', fs)) disp(sprintf('\n')) % make random signal -1 ~ 1 with DC Compensation % --------------------------------------------------------------------------- x = 2*rand(1,N); x = x - mean(x); % generate noise and init. delay line % make sure burst and delay line agree: burst >= delay line % --------------------------------------------------------------------------- y = [zeros(1,N+1)]; if iterations > length(x) diff = iterations - length(x); x = [x zeros(1,diff)]; end % Filtering % y[n] = x[n] + y[n-N] + y[n-(N+1)] % --------------------------------------------------------------------------- % init. out = 0; signal = 0; lengthYOffset = length(y)-1; for i=1:iterations out = x(i) + 0.5*(y(N) + y(N+1)); % filter signal y = [out, y(1:lengthYOffset)]; % update delay line signal = [signal out]; end % Play sound and plot % --------------------------------------------------------------------------- plot(signal), grid on, title('Simple 2 point KS Plucked String Algorithm') sound(signal, fs)