function [Output ModSignal] = vibrato_mod(Input, FS , ModFreq, Width, Q)
% function [Output ModSignal] = delay_mod(Input, FS, ModFreq, Width, Q)
%
% This vibrato function applies a pitch modulated vibrato effect to
% an audio signal by putting the signal through a modulating delay system.
% The shape of the delay modulation signal is generated from random
% fractal noise that has been shaped to the desired frequency
% by means of filtering.
%
% Input = Input Audio Signal
% FS = Sampling Frequency of the Input Signal in Hz
% ModFreq = Modulation Frequency of the Vibrato in Hz
% Width = Maximum Width of Vibrato in seconds
% Q = Quality Factor; determines how strictly the vibrato
% adheres to the specified ModFreq parameter
%
% Using the proclpc function, the input signal is separated into its
% source excitation and resonant components.
%
% Fractal noise is generated, using the 'frand64' function. The
% fractal noise is then filtered by the 'get_LP_filtered_modJL'
% function to obtain a modulating delay signal of the frequency specified
% by the ModFreq parameter. The extent to which this signal
% resembles a periodic sine wave is governed by the Q parameter. The
% higher the Q value the more the delay signal will resemble a periodic
% sine wave.
%
% This delay signal is scaled by the Width parameter and then applied
% to the source excitation of the input signal. An all pass
% interpolation process is then performed to account for any fractionally
% delayed samples.
%
% This pitch modulated source excitation signal is then resynthesised
% with its resonances, using the synlpc function, and the result is
% outputted as a vector. The modulating delay signal is also outputted
% as a vector along with a graphic representation of the delay signal.
%
% This function works for input signals that are less than 10
% seconds in length. Please ensure that the input signal is a
% column vector.
%
% Example: [Output ModSignal] = vibrato_mod(Input, 44100, 5, 0.001, 50)
%
% The modulating delay function was sourced from Zölzer's "DAFX -
% Digital Audio Effects"
%
% ‘frand64’, ‘get_LP_filtered_mod_JL’, ‘M_fq2coef’, ‘normsig’, ‘proclpc’,
% ‘randsig’ and ‘synlpc’ functions were all sourced from William Martens.
ya_alt=0;
%--------- Source Filtering -----------------------------------------------
[aCoeff, resid, pitch, G, parcor, stream] = proclpc(Input,FS);
stream = stream'; % filtering function that seperates the input signal into
% its source excitation (stream variable) and its resonant components
% Note that Delay = Width, the basic delay of input sample in seconds
DELAY = round(Width*FS); % number os samples in the basic delay
WIDTH = round(Width*FS); % number of samples in the modulation width
L = 2 + DELAY + WIDTH*2; % length of the entire delay
Delayline = zeros(L,1); % memory allocation for delay
y = zeros(size(stream)); % memory allocation for output vector
LEN = length(stream-1); % number of samples in the filtered source excitation
% of the input signal
%-- Generate Filtered Fractal Noise to represent Modulating Delay Signal --
Mod_sig = get_LP_filtered_modJL(ModFreq, Q); % calls get_LP_filtered_modJL
% function to generate fractal noise filtered at the specified vibrato
% modulation frequency and the desired Q factor for the filter
Mod_sig = Mod_sig';
Mod_sig = Mod_sig(1:LEN,1); % makes the filtered fractal noise signal the same
% length as the source excitation signal
[ModSignal] = Mod_sig;
%----- Applying Modulating Delay Signal to Source Excitation Signal -------
for n = 1 :(LEN-1)
ZEIGER = 1 + DELAY + WIDTH*Mod_sig(n);
i = floor(ZEIGER);
frac = ZEIGER-i;
Delayline = [stream(n);Delayline(1:L-1)];
%-------- Allpass Interpolation--------------------------------------------
y(n,1) = (Delayline(i+1) + (1-frac)*Delayline(i) - (1-frac)*ya_alt);
ya_alt = y(n,1);
end
figure(1), plot(Mod_sig);
title('Delay Modulation Signal')
xlabel('Time (s)');
set(gca, 'XTick', 0:FS:10*FS)
set(gca, 'XTickLabel',{'0','1','2','3','4','5','6','7','8','9','10'})
ylabel('Modulation Signal');
set(gca, 'Ylim', [-1 1]);
set(gca, 'YTick', [-1 0 1]);
Output = synlpc(aCoeff,y,FS,G); % resynthesises the pitched modulated source
% excitation with its resonances
sound(Output,FS);