Matlab work

Matlab Code


- In Afternoon.m

Fs1 = 44100; %Set Sampling Frequency
Fr = Fs1; %Set Reconstruction Frequency
Fs = Fs1; % The multiple of the sampling frequency to be used
T = 1/Fs; %Sampling Period

N = 0.2*Fs1; % Number of samples to achieve desired duration
L = 1; % default value for L (L=1 => no duration change)
n = @(L) 1:L*N; %the array, n, takes an integer multiplier, L, that can reduce or increase the duration of a note, i.e. 1/2 note, 1/4 note, etc
m = 0; %default input for oct, the octave shift function (using downsampling)

%Generate General Sinusoid
%m is the desired octave, which is transformed into the appropriate multiplier by the oct function
%L is the desired length of the note (in quarter notes)
%fN is the frequncy of the note 
note = @(m, L, fN) cos(2*pi*oct(m)*fN*T*n(L)); %standard note at fund. freq.

fA = 440.00; % Master Tuned to A 440
fGS = fA*2^(-1/12);
fG = fGS*2^(-1/12);
fFS = fG*2^(-1/12);
fF = fFS*2^(-1/12);
fE = fF*2^(-1/12);
fDS = fE*2^(-1/12);
fD = fDS*2^(-1/12);
fCS = fD*2^(-1/12);
fC = fCS*2^(-1/12);
fAS = fA*2^(1/12);
fB = fAS*2^(1/12);

namp = 1; % set the amplitude for the natural freq
hamp = 0.8; % set the amplitude for the overtones

%each note passes m and L to the note function above
%two overtones are added to each note
C = @(m, L) namp*note(m, L, fC) + hamp*note(m, L, 0.5*fC) + hamp*note(m, L, 2*fC);
CS = @(m, L) namp*note(m, L, fCS) + hamp*note(m, L, 0.5*fCS) + hamp*note(m, L, 2*fCS);
D = @(m, L) namp*note(m, L, fD) + hamp*note(m, L, 0.5*fD) + hamp*note(m, L, 2*fD);
DS = @(m, L) namp*note(m, L, fDS) + hamp*note(m, L, 0.5*fDS) + hamp*note(m, L, 2*fDS);
E = @(m, L) namp*note(m, L, fE) + hamp*note(m, L, 0.5*fE) + hamp*note(m, L, 2*fE);
F = @(m, L) namp*note(m, L, fF) + hamp*note(m, L, 0.5*fF) + hamp*note(m, L, 2*fF);
FS = @(m, L) namp*note(m, L, fFS) + hamp*note(m, L, 0.5*fFS) + hamp*note(m, L, 2*fFS);
G = @(m, L) namp*note(m, L, fG) + hamp*note(m, L, 0.5*fG) + hamp*note(m, L, 2*fG);
GS = @(m, L) namp*note(m, L, fGS) + hamp*note(m, L, 0.5*fGS) + hamp*note(m, L, 2*fGS);
A = @(m, L) namp*note(m, L, fA) + hamp*note(m, L, 0.5*fA)+ hamp*note(m, L, 2*fA);
AS = @(m, L) namp*note(m, L, fAS) + hamp*note(m, L, 0.5*fAS) + hamp*note(m, L, 2*fAS);
B = @(m, L) namp*note(m, L, fB) + hamp*note(m, L, 0.5*fB) + hamp*note(m, L, 2*fB);

% Base melody
base1 = [E(0,2) B(0,2) G(0,2) B(0,2)];
base2 = [D(0,2) B(0,2) G(0,2) B(0,2)];
base3 = [D(0,2) B(0,2) FS(0,2) B(0,2)];
base4 = [D(0,2) A(0,2) FS(0,2) A(0,2)];
base = [base1  base1 base2 base2 base3 base3 base4 base4];
bm = [base base base base base base base A(0,4) base base base base base base A(0,4) G(1,6) E(0,10)+E(-1,10)];

% melody
part1 = [E(1,4) E(1,4) E(1,4) E(1,4) D(1,4) D(1,4) D(1,4) D(1,4) D(1,4) D(1,4) D(1,4) D(1,4) D(1,4) D(1,4) D(1,4) D(1,4)];
part2 = [E(1,2) G(1,1) FS(1,1) G(1,2) B(1,1) C(2,1) B(1,4) E(1,4) D(1,2) FS(1,1) G(1,1) FS(1,2) G(1,1) A(1,1) G(1,4) D(1,4) D(1,2) FS(1,1) E(1,1) FS(1,2) B(1,1) C(2,1) B(1,4) D(1,4) D(1,2) FS(1,1) E(1,1) FS(1,4) D(1,4) D(1,4)];
part3 = [E(2,6) B(1,10) D(2,6) B(1,10) FS(2,6) B(1,10) FS(2,6) A(1,10)];
part4 = [G(2,6)+B(1,6) E(2,10)+G(1,10) G(2,6)+B(1,6) D(2,10)+G(1,10) FS(2,6)+B(1,6) D(2,10)+FS(1,10) FS(2,6)+A(1,6) D(2,10)+FS(1,10)];
part5 = [B(1,1) E(2,1) B(2,1) B(1,1) E(2,1) B(2,1) B(1,1) E(2,1) B(2,1) B(1,1) E(2,1) B(2,1) B(1,1) E(2,1) C(2,1) E(2,1) ...
         B(1,1) D(2,1) B(2,1) B(1,1) D(2,1) B(2,1) B(1,1) D(2,1) B(2,1) B(1,1) D(2,1) B(2,1) B(1,1) D(2,1) A(1,1) D(2,1) ...
         FS(1,1) B(1,1) FS(2,1) FS(1,1) B(1,1) FS(2,1) FS(1,1) B(1,1) FS(2,1) FS(1,1) B(1,1) FS(2,1) FS(1,1) B(1,1) G(1,1) B(1,1) ...
         A(1,1) D(2,1) A(2,1) A(1,1) D(2,1) A(2,1) A(1,1) D(2,1) A(2,1) A(1,1) D(2,1) A(2,1) A(1,1) A(2,1) G(1,1) G(2,1)];
part6 = [B(1,1) E(2,1) B(2,1) B(1,1) E(2,1) B(2,1) B(1,1) E(2,1) B(2,1) B(1,1) E(2,1) B(2,1) B(1,1) E(2,1) C(2,1) E(2,1) ...
         B(1,1) D(2,1) B(2,1) B(1,1) D(2,1) B(2,1) B(1,1) D(2,1) B(2,1) B(1,1) D(2,1) B(2,1) B(1,1) D(2,1) A(1,1) D(2,1) ...
         FS(1,1) B(1,1) FS(2,1) FS(1,1) B(1,1) FS(2,1) FS(1,1) B(1,1) FS(2,1) FS(1,1) B(1,1) FS(2,1) FS(1,1) B(1,1) G(1,1) B(1,1) ...
         A(1,1) D(2,1) A(2,1) A(1,1) D(2,1) A(2,1) A(1,1) D(2,1) A(2,1) A(1,1) D(2,1) A(2,1) A(1,2) D(2,2)];
partm1 = [B(1,14) C(2,2) B(1,14) A(1,2) FS(1,14) G(1,2) A(1,14) G(1,2)];
partm2 = [B(1,14) C(2,2) B(1,14) A(1,2) FS(1,14) G(1,2) A(1,16)];
part7 = [A(2,4)];
part8 = [E(1,2) G(2,1) FS(2,1) G(2,2) B(2,1) C(3,1) B(2,4) E(1,4) D(1,2) FS(2,1) G(2,1) FS(2,2) G(2,1) A(2,1) G(2,4) D(1,4) D(1,2) FS(2,1) E(2,1) FS(2,2) B(2,1) C(3,1) B(2,4) D(1,4) D(1,2) FS(2,1) E(2,1) FS(2,4) D(1,4) D(1,4)];
part9 = [E(3,6) B(2,10) D(3,6) B(2,10) FS(3,6) B(2,10) FS(3,6) A(2,10)];
part10 = [G(3,6)+B(2,6) E(3,10)+G(2,10) G(3,6)+B(2,6) D(3,10)+G(2,10) FS(3,6)+B(2,6) D(3,10)+FS(2,10) FS(3,6)+A(2,6) D(3,10)+FS(2,10)];
part11 = [B(2,1) E(3,1) B(3,1) B(2,1) E(3,1) B(3,1) B(2,1) E(3,1) B(3,1) B(2,1) E(3,1) B(3,1) B(2,1) E(3,1) C(3,1) E(3,1) ...
         B(2,1) D(3,1) B(3,1) B(2,1) D(3,1) B(3,1) B(2,1) D(3,1) B(3,1) B(2,1) D(3,1) B(3,1) B(2,1) D(3,1) A(2,1) D(3,1) ...
         FS(2,1) B(2,1) FS(3,1) FS(2,1) B(2,1) FS(3,1) FS(2,1) B(3,1) FS(3,1) FS(2,1) B(2,1) FS(3,1) FS(2,1) B(2,1) G(2,1) B(2,1) ...
         A(2,1) D(3,1) A(3,1) A(2,1) D(3,1) A(3,1) A(2,1) D(3,1) A(3,1) A(2,1) D(3,1) A(3,1) A(2,1) A(3,1) G(2,1) G(3,1)];
part12 = [B(2,1) E(3,1) B(3,1) B(2,1) E(3,1) B(3,1) B(2,1) E(3,1) B(3,1) B(2,1) E(3,1) B(3,1) B(2,1) E(3,1) C(3,1) E(3,1) ...
         B(2,1) D(3,1) B(3,1) B(2,1) D(3,1) B(3,1) B(2,1) D(3,1) B(3,1) B(2,1) D(3,1) B(3,1) B(2,1) D(3,1) A(2,1) D(3,1) ...
         FS(2,1) B(2,1) FS(3,1) FS(2,1) B(2,1) FS(3,1) FS(2,1) B(2,1) FS(3,1) FS(2,1) B(2,1) FS(3,1) FS(2,1) B(2,1) G(2,1) B(2,1) ...
         A(2,1) D(3,1) A(3,1) A(2,1) D(3,1) A(3,1) A(2,1) D(3,1) A(3,1) A(2,1) D(3,1) A(3,1) A(2,2) D(3,2)];
part13 = [A(3,4)];
part14 = [G(2,6) E(2,10)+G(1,10)];
test = [part4];
melody = [part1 part2 part2 part3 part4 part5+partm1 part6+partm2 part7 part8 part8 part9 part10 part11+partm1 part12+partm2 part13 part14];

%song
song = [bm + melody];

%Output song reconstructed at Fr
soundsc(5*song, Fr); % the multiplier in front of song sets the master volume

%Output the wave of the melody
time = [0:length(song)-1]/Fr;
plot(time,song);
xlabel('time (sec)');

%save the music
wavwrite(song, Fr, 32, 'in afternoon.wav');

- oct.m

function y = oct(m)

if m >= 0
    y = 2^m;
end
if m < 0
    y = 1/2^(-m);
end

No comments:

Post a Comment