Last active
March 23, 2018 07:16
-
-
Save rebeccajae/1ed982aca9e6395658972d829893ee1e to your computer and use it in GitHub Desktop.
Crude thing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function p = rootPitch(key, note) | |
| notes = {'c', 'cs', 'd', 'ef', 'e', 'f', 'fs','g','gs','a','bf', 'b'}; | |
| I = find(strcmp(notes, note)); | |
| I = I-1; | |
| shift = 12*key; | |
| idx = shift + I; | |
| a = 2^(1/12); | |
| f0 = 16.35; | |
| p = f0*a^idx; | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| clear variables; | |
| Fs = 44100; % Sampling Frequency | |
| Ts = 1/Fs; % Sample Time | |
| T = 2; % Length of output | |
| Px = 0.9999; % Fade Out Modulus | |
| Ap = 0.4; % Amplitude Modulus | |
| PitchRoot = rootPitch(4, 'a'); % Root Pitch | |
| Pn = ceil(Fs/(2*PitchRoot)); % Number of partials to use | |
| % This can be calculated. | |
| % The human ear can only hear ~22050 Hz. We find how many partials we need | |
| % to achieve such that we don't exceed that by much. Thus, since Fs is | |
| % double the human hearing, and you'd run into issues with sampling if | |
| % you exceed Fs/2, we divide it by two then divide it further by | |
| % the root pitch. Think such that Pn * PitchRoot = Fs/2. Solve for Pn. | |
| t=0:Ts:T; | |
| y = zeros(Pn, Fs*2+1); | |
| %Generate partials | |
| for i = 1:Pn | |
| partialF = PitchRoot*i; | |
| amp = Ap^i; | |
| temp = amp*sin(2*pi*partialF*t); | |
| y(i,:) = temp; | |
| end | |
| % Composite them | |
| partials = zeros(size(temp)); | |
| for i = 1:Pn | |
| temp = y(i,:); | |
| partials = partials + temp; | |
| end | |
| %Apply a fade out. | |
| for i = 1:numel(partials) | |
| partials(i) = partials(i)*Px^i; | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment