move add_sine, lfo_sine to src folder
This commit is contained in:
34
src/add_sine.m
Normal file
34
src/add_sine.m
Normal file
@@ -0,0 +1,34 @@
|
||||
function x = add_sine(amplitude, fundamental, harmonics, fs, duration)
|
||||
% ADD_SINE: Additive sine wave synthesis
|
||||
|
||||
%CONTRIBUTORS:
|
||||
%Benjamin Liou: Original author
|
||||
|
||||
% DOCUMENTATION:
|
||||
% harmonics should ideally be a 1D matrix of:
|
||||
% overtones: positive integers
|
||||
% undertones: 1/ positive integers
|
||||
% example: [1/2, 1, 2, 3]
|
||||
% NOTE: pitch of the fundamental frequency will still be perceived even
|
||||
% when the fundamental itself is missing. ex. [4,5,6]
|
||||
|
||||
% NOTE: it seems like when MATLAB's built in sound() takes in values,
|
||||
% magnitudes over 1 get distorted.
|
||||
|
||||
|
||||
|
||||
% initialize local variables from input arguments
|
||||
n = fs * duration; % number of samples (length of matrix)
|
||||
|
||||
% initialize a one dimensional zero matrix to be populated
|
||||
x = zeros(1, n);
|
||||
|
||||
% populate matrix by adding sine waves
|
||||
for harmonic = harmonics
|
||||
x = x + generate_sine(1, fundamental * harmonic, 0, fs, duration);
|
||||
end
|
||||
|
||||
% scale to amplitude
|
||||
scalar = max(abs(x));
|
||||
x = x / scalar * amplitude;
|
||||
end
|
29
src/lfo_sine.m
Normal file
29
src/lfo_sine.m
Normal file
@@ -0,0 +1,29 @@
|
||||
function x = lfo_sine(amplitude, frequency, phase, fs, duration, input)
|
||||
% LFO_SINE: modulates an input matrix
|
||||
|
||||
% CONTRIBUTORS:
|
||||
% Benjamin Liou: Original author
|
||||
|
||||
% DOCUMENTATION:
|
||||
% frequency is typically below 20Hz (according to wikipedia)
|
||||
% 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);
|
||||
|
||||
% populate lfo matrix
|
||||
for i = 1:n
|
||||
t = i * dt; % time at the i'th sample
|
||||
lfo(i) = amplitude * sin(2 * pi * frequency * t - phase);
|
||||
end
|
||||
|
||||
% modulate input
|
||||
x = lfo .* input;
|
||||
|
||||
end
|
Reference in New Issue
Block a user