Merge pull request #52 from ltcptgeneral/neelay_functions

Neelay's LFOs for Pitch
This commit is contained in:
neelay-j 2021-12-10 17:55:57 -08:00 committed by GitHub
commit 65c70216bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 0 deletions

38
src/lfo_freq_saw.m Normal file
View File

@ -0,0 +1,38 @@
function output = lfo_freq_saw(amplitude, frequency, phase, fs, duration, input)
%LFO_FREQ_SAW Modulates the frequency of an input with a saw LFO
% CONTRIBUTORS:
% Neelay Joglekar: Original author
% SOURCES:
% Code inspired by Benjamin Liou's lfo functions
% Code also inspired by Darren and Anne's pitch envelopes
% Source for frequency modulation equation:
% https://dsp.stackexchange.com/questions/2349/help-with-algorithm-for-modulating-oscillator-pitch-using-lfo
% DOCUMENTATION:
% Frequency is modulated by saw wave with given amplitude (in octaves, not Hz)
% 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
% create output array
output = zeros([1, n]);
x = 0.0; % the theoretical input index if the input were not discrete
% populate output
for i = 1:n
t = i * dt; % time at the i'th sample
% Increment x based off of saw wave output
omega_ratio = 2 ^ (amplitude * (2 * mod((t - phase / (2*pi)) * frequency, 1) - 1));
x = mod(x + omega_ratio, n);
% Linearly interpolate the actual indicies adjacent to x
% to get an output value
x_0 = floor(x) + 1;
x_1 = mod(x_0 + 1, n) + 1;
output(i) = (input(x_1) - input(x_0)) * (x - x_0) + input(x_0);
end
end

39
src/lfo_freq_sine.m Normal file
View File

@ -0,0 +1,39 @@
function output = lfo_freq_sine(amplitude, frequency, phase, fs, duration, input)
%LFO_FREQ_SINE Modulates the frequency of an input with a sine LFO
% CONTRIBUTORS:
% Neelay Joglekar: Original author
% SOURCES:
% Code inspired by Benjamin Liou's lfo functions
% Code also inspired by Darren and Anne's pitch envelopes
% Source for frequency modulation equation:
% https://dsp.stackexchange.com/questions/2349/help-with-algorithm-for-modulating-oscillator-pitch-using-lfo
% DOCUMENTATION:
% Frequency is modulated by sine wave with given amplitude (in octaves, not Hz)
% 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
% create output array
output = zeros([1, n]);
x = 0.0; % the theoretical input index if the input were not discrete
% populate output
for i = 1:n
t = i * dt; % time at the i'th sample
% Increment x based off of sine wave output
omega_ratio = 2 ^ (amplitude * sin(2 * pi * frequency * t - phase));
x = mod(x + omega_ratio, n);
% Linearly interpolate the actual indicies adjacent to x
% to get an output value
x_0 = floor(x) + 1;
x_1 = mod(x_0 + 1, n) + 1;
output(i) = (input(x_1) - input(x_0)) * (x - x_0) + input(x_0);
end
end

38
src/lfo_freq_square.m Normal file
View File

@ -0,0 +1,38 @@
function output = lfo_freq_square(amplitude, frequency, phase, fs, duration, input)
%LFO_FREQ_SQUARE Modulates the frequency of an input with a square LFO
% CONTRIBUTORS:
% Neelay Joglekar: Original author
% SOURCES:
% Code inspired by Benjamin Liou's lfo functions
% Code also inspired by Darren and Anne's pitch envelopes
% Source for frequency modulation equation:
% https://dsp.stackexchange.com/questions/2349/help-with-algorithm-for-modulating-oscillator-pitch-using-lfo
% DOCUMENTATION:
% Frequency is modulated by square wave with given amplitude (in octaves, not Hz)
% 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
% create output array
output = zeros([1, n]);
x = 0.0; % the theoretical input index if the input were not discrete
% populate output
for i = 1:n
t = i * dt; % time at the i'th sample
% Increment x based off of square wave output
omega_ratio = 2 ^ (amplitude * (2 * round(mod((t - phase / (2*pi)) * frequency, 1)) - 1));
x = mod(x + omega_ratio, n);
% Linearly interpolate the actual indicies adjacent to x
% to get an output value
x_0 = floor(x) + 1;
x_1 = mod(x_0 + 1, n) + 1;
output(i) = (input(x_1) - input(x_0)) * (x - x_0) + input(x_0);
end
end