Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB song... I am trying to play a string of notes, each for a different durat

ID: 2082124 • Letter: M

Question

MATLAB song... I am trying to play a string of notes, each for a different duration. The following is what I have come up with but can not seem to get it to work properly. If I can get this to work correctly I will add more notes to finish my song. Any help will be greatly appreciated. Code belo...

clc
clear all

fs = 16000;
t1=[0:1/fs:0.3];
t2=[0:1/fs:0.6];
t3=[0:1/fs:0.9];

C2 = 65.4064;
D2 = 73.4162;
F2 = 87.3071;

staff1 = [C2 D2 F2];
time1 = [t1 t2 t3];

play1 = sin(2*pi*staff1*time1);

sound(play1,fs);

Explanation / Answer

Error in your Code :

The matrix dimension is not correct.

Correct code:

Fs=8000;
Ts=1/Fs;
t=[0:Ts:0.3];
F_A = 440; %Frequency of note A is 440 Hz
F_B = 493.88;
F_Csharp = 554.37;
F_D = 587.33;
F_E = 659.26;
F_Fsharp = 739.9;
notes = [F_A ; F_B; F_Csharp; F_D; F_E; F_Fsharp];
x = cos(2*pi*notes*t);
sig = reshape(x',6*length(t),1);
soundsc(sig,1/Ts)

OUTPUT: Listen to the music