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.
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),...