MATLAB play notes for different duration's as specified by

13 vues (au cours des 30 derniers jours)
sanbadgerdude
sanbadgerdude le 15 Avr 2017
Commenté : sanbadgerdude le 18 Avr 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
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);

Réponses (1)

Geoff Hayes
Geoff Hayes le 18 Avr 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 commentaire
sanbadgerdude
sanbadgerdude le 18 Avr 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by