From 25f6047f911d14329cc3bda454fc9042df9a827c Mon Sep 17 00:00:00 2001 From: Ben Zhang Date: Wed, 8 Dec 2021 19:51:07 -0800 Subject: [PATCH] Add lfo_sawtooth function --- src/lfo_sawtooth.m | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/lfo_sawtooth.m diff --git a/src/lfo_sawtooth.m b/src/lfo_sawtooth.m new file mode 100644 index 0000000..62b4709 --- /dev/null +++ b/src/lfo_sawtooth.m @@ -0,0 +1,37 @@ +function x = lfo_sawtooth(amplitude, frequency, phase, fs, duration, input) +% LFO_SAWTOOTH: modulates an input matrix to a sawtooth parameter + +% CONTRIBUTORS: +% Ben Zhang: Function Author + +% DOCUMENTATION: +% frequency is below 20Hz for people to hear the sound +% fs and duration should be same as input + + + % initialize local variables from input arguments + n = fs * duration; % number of samples (length of matrix) + dt = 1 / fs; % sampling period: time between two sample points + + % initialize lfo, which will be used to modulate the input + lfo = zeros(1, n); + period = 1 / frequency; % period of the wave + slope = 2 * amplitude / period; % the incline slope from start to amplitude + + % populate lfo matrix + for i = 1:n + t = i * dt; % time at the i'th sample + st = mod(frequency * t - phase, 1); % Progression through cycle + %part before the straght vertical line + if(st < period /2) + lfo(i) = amplitude * slope; + %part after the straght vertical line + else + lfo(i) = amplitude * (slope - 1); + + end + + % modulate input + x = lfo .* input; + +end \ No newline at end of file