MATLAB play notes for different duration's as specified by

10 views (last 30 days)
sanbadgerdude on 15 Apr 2017
Commented: sanbadgerdude on 18 Apr 2017
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. I am new to this and trying to understand. My intent is to have an array of notes then an array of times that each note will play. I may play the same note but hold it for a different duration of time.
if true
% code
clear all
fs = 16000;
C2 = 65.4064;
D2 = 73.4162;
F2 = 87.3071;
staff1 = [C2 D2 F2];
time1 = [t1 t2 t3];
play1 = sin(2*pi*staff1*time1);

Answers (1)

Geoff Hayes
Geoff Hayes on 18 Apr 2017
sanbadgerdude - when I run your above code, I observe the following error
Error using *
Inner matrix dimensions must agree.
play1 = sin(2*pi*staff1*time1);
This is because the dimensions of staff1 is 1x3 whereas the dimensions for time1 is 1x28803, and so multiplying the two together is going to fail. What you can do instead is multiply each time intervals by a frequency prior to passing this in to the sine function. Try
ft = [t1*C2 t2*D2 t3*F2];
play1 = sin(2*pi*ft);
  1 Comment
sanbadgerdude on 18 Apr 2017
Thank you! I have been beating myself up over this. I had started to write out a long drawed out code when I saw your answer. Very much appreciated! Such a simple overlooked resolution.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!