diff --git a/src/LFO/lfo_freq_square.m b/src/LFO/lfo_freq_square.m new file mode 100644 index 0000000..f413e6c --- /dev/null +++ b/src/LFO/lfo_freq_square.m @@ -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 \ No newline at end of file