Synthetic Music
You can create synthetic music using MATLAB by creating and playing a sequence of short bursts of sinusoids. Use the table below for a reference of the notes.
Note | ||||||||
Frequency (Hz) |
For example, the following MATLAB code generates and plays the scale from middle C. Click here to hear the scale.
fs = 8000; % sets the sampling frequency
t = 0:1/fs:0.25; % length of each note
tspace = .05; % length of pause between notes
fr = 2^(1/12); % the frequency ratio between neighboring keys
A4 = 440; % set this as the reference key to determine other keys
B4 = A4*fr^2;
C5 = A4*fr^3;
C4 = A4*fr^(-9);
D4 = A4*fr^(-7);
E4 = A4*fr^(-5);
F4 = A4*fr^(-4);
G4 = A4*fr^(-2);
xspace = zeros(size(tspace)); % set pause
x = [cos(C4*2*pi*t),xspace, cos(D4*2*pi*t),xspace,cos(E4*2*pi*t),...
xspace,cos(F4*2*pi*t),xspace,cos(G4*2*pi*t),xspace,...
cos(A4*2*pi*t),xspace,cos(B4*2*pi*t),xspace,cos(C5*2*pi*t)];
sound(x,fs);
No comments:
Post a Comment