Line 53: | Line 53: | ||
</code> | </code> | ||
− | [[ | + | [[Media:Doremiaudio.wav|Audio file of song]] |
Revision as of 12:00, 30 November 2018
Matlab Sound of Music
Matlab can be used to create signals that can mimic the sound of a musical note, which can be used to play the tune of a song. Each note of a song is created by individual sin waves at a frequencies for each note. The following line of music has three different notes, C D and E.
Each of the three notes in this piece have their own unique frequency. C has a frequency about 261.63 Hz. D and E notes are created by multiplying the C note frequency by a factor to create a new note. For D the factor is 9/8 and for E the factor is 5/4.
A sine wave when played without an envelope would create a constant volume sound. However, when striking a piano key the sound is at first loud and then fades as time goes on. To replicate this effect in matlab, the sine wave can be multiplied by a negative exponential coefficient. The following code creates the song above in matlab.
function doremi()
x = 261.625565; %frequency of C
delta = 1/ 8126; %step used for note vectors
notelength = 1; %how long each note is played
%creates vectors for note length intervals
tquarter = 0 : delta : 0.25*notelength;
thalf = 0 : delta : 0.5*notelength;
tthreefour = 0 : delta : 0.75*notelength;
ttwice = 0 : delta : 2*notelength;
%creating the sin waves for each note in the song
do = exp((-1)*tthreefour*3).*sin(2 * pi * x * tthreefour);
ad = exp((-1)*tquarter*3).*sin(2 * pi * (9 * x / 8) * tquarter);
deer = exp((-1)*tthreefour*3).*sin(2 * pi * (5 * x / 4) * tthreefour);
ac = exp((-1)*tquarter*3).*sin(2 * pi * x * tquarter);
fe = exp((-1)*thalf*3).*sin(2 * pi * (5 * x / 4) * thalf);
male = exp((-1)*thalf*3).*sin(2 * pi * x * thalf);
deer2 = fe;
%concatenating song to one vector
song = [do, ad, deer, ac, fe, male, deer];
%plays song
sound(song, 1/delta)