MATLAB play notes for different duration's as specified by

13 次查看(过去 30 天)
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
end
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);

回答(1 个)

Geoff Hayes
Geoff Hayes 2017-4-18
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 个评论
sanbadgerdude
sanbadgerdude 2017-4-18
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by