Matlab based music generation program
In this project I used MATLAB to generate discrete sinusoids of one octave of musical notes using the sampling frequency of my choice. I then played back the octave at different multiples of the sampling frequency and observed the change in pitch. I continued by adding commands to control note duration and produce octave shifts without changing the sampling or reconstruction frequency. Lastly, overtones were added to each note, which I used to play back a short piece of music. Save the following matlab codes in the given names and run the file music.m from matlab. The famous "Jingle Bells" written by James Lord Pierpont(1822–1893) and published under the title "One Horse Open Sleigh" in the autumn of 1857 is the music that i have used for demonstration.
File 1 - music.m
Fs = 8000; % sampling frequency N = 0.2*Fs; % Number of samples in 0.2 sec n = @(L) 1:L*N; %L --> reduce/increase note duration %Generate Sinusoid %m represent octave L length of note f frequncy of the note note = @(m, L, f) sin(2*pi*oct(m)*f*(1/Fs)*n(L)); A = 440.00; % Master Tuned to A 440 the order is A GS G FS F E DS D CS C AS B GS = A*2^(-1/12); G = GS*2^(-1/12); FS = G*2^(-1/12); F = FS*2^(-1/12); E = F*2^(-1/12); DS = E*2^(-1/12); D = DS*2^(-1/12); CS = D*2^(-1/12); C = CS*2^(-1/12); AS = A*2^(1/12); B = AS*2^(1/12); %generating each note %two overtones are added to each note NT = @(m,L,F) note(m, L, F) + 0.8*(note(m, L, 0.5*F) + note(m, L, 2*F)); %Define Rests rs = @(val) zeros(1,val*N); % val = 0.125 for eigth rest 0.25 for quarter rest 1 for whole rest %Jingle Bells SEQ1 = [NT(0,1,A) rs(0.25) NT(0,1,A) rs(0.25) NT(0,2,A) rs(0.25)]; SEQ2 = [NT(0,1,A) rs(0.25) NT(1,1,C) rs(0.25) NT(0,1,F) rs(0.25) NT(0,1,G) rs(0.25) NT(0,4,A) rs(0.25)]; SEQ3 = [NT(0,1,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,0.5,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,A) rs(0.25) NT(0,1,A) rs(0.25) NT(0,0.5,A) rs(0.125) NT(0,0.5,A) rs(0.25) NT(0,1,A) rs(0.25) NT(0,1,G) rs(0.25) NT(0,1,G) rs(0.25) NT(0,1,A) rs(0.25) NT(0,2,G) rs(0.25) NT(1,2,C) rs(0.25)]; SEQ4 = [NT(0,1,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,A) rs(0.25) NT(0,1,A) rs(0.25) NT(0,0.5,A) rs(0.125) NT(0,0.5,A) rs(0.25) NT(1,1,C) rs(0.25) NT(1,1,C) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,G) rs(0.25) NT(0,4,F)]; SEQ5 = [NT(0,1,C) rs(0.25) NT(0,1,A) rs(0.25) NT(0,1,G) rs(0.25) NT(0,1,F) rs(0.25) NT(0,3,C) rs(0.25) NT(0,0.5,C) rs(0.125) NT(0,0.5,C) rs(0.25) NT(0,1,C) rs(0.25) NT(0,1,A) rs(0.25) NT(0,1,G) rs(0.25) NT(0,1,F) rs(0.25) NT(0,4,D) rs(0.25)]; SEQ6 = [NT(0,1,D) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,A) rs(0.25) NT(0,1,G) rs(0.25) NT(0,4,E) rs(0.25) NT(1,1,C) rs(0.25) NT(1,1,C) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,G) rs(0.25) NT(0,4,A) rs(0.25)]; SEQ7 = [NT(0,1,C) rs(0.25) NT(0,1,A) rs(0.25) NT(0,1,G) rs(0.25) NT(0,1,F) rs(0.25) NT(0,4,C) rs(0.25) NT(0,1,C) rs(0.25) NT(0,1,A) rs(0.25) NT(0,1,G) rs(0.25) NT(0,1,F) rs(0.25) NT(0,4,D) rs(0.25)]; SEQ8 = [NT(0,1,D) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,A) rs(0.25) NT(0,1,G) rs(0.25) NT(1,1,C) rs(0.25) NT(1,1,C) rs(0.25) NT(1,1,C) rs(0.25) NT(1,0.5,C) rs(0.125) NT(1,0.5,C) rs(0.25) NT(1,1,D) rs(0.25) NT(1,1,C) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,G) rs(0.25) NT(0,1,F) rs(1) NT(1,2,C) rs(0.25)]; SEQ9 = [NT(0,1,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,A) rs(0.25) NT(0,1,A) rs(0.25) NT(0,0.5,A) rs(0.125) NT(0,0.5,A) rs(0.25) NT(1,1,C) rs(0.25) NT(1,1,C) rs(0.25) NT(0,1,AS) rs(0.25) NT(0,1,G) rs(0.25) NT(0,4,F) rs(0.25) ]; %combining all sequences jbells = [SEQ1 SEQ1 SEQ2 SEQ3 SEQ1 SEQ1 SEQ2 SEQ4 SEQ5 SEQ6 SEQ7 SEQ8 SEQ1 SEQ1 SEQ2 SEQ3 SEQ1 SEQ1 SEQ2 SEQ3 SEQ9]; sound(0.1*jbells);
File 2 - oct.m
function y = oct(m) if m >= 0 y = 2^m; end if m < 0 y = 1/2^(-m); end