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

C

D

E

F

G

A

 B

 C
 Frequency (Hz)

 262

 294

 330

 349

 392

 440

 494

 523

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);