Merge pull request #11 from ltcptgeneral/bliou000-patch-2

additive sine wave synthesis
This commit is contained in:
bliou000 2021-12-05 09:16:38 -08:00 committed by GitHub
commit e4580cf56a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

34
add_sine.m Normal file
View 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