In an assignment A(:) = B, the number of elements in A and B must be the same.
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Here I am trying to synthesis a song with the following sets of code but I am getting the error above at (line 62)
song(n1:n2) = song(n1:n2) + tone; %<------- Insert the note into the vector
%create the song
theStrings(1).noteNumbers = [57 59 59 61 61 59 57 59 64 64 62 61 57 54 50 52 57 59 59 61 61 59 57 59 64 64 61 64 66 64 59 59 57 52 47 45 45 45 45 44 45 45 45 44 45 47 49 47 45 45 45 45 44 45 45 40 40 50 52 54 56 57 57 57 57 56 57 57 56 57 59 61 59 57 57 57 57 56 57 57 52 52 0 57 57 0 0 59 59 0 52 64 62 61 59 61 62 61 59 57 56 57 56 54 54 0 57 57 0 0 59 59 0 52 64 62 61 59 61 62];
theStrings(1).durations = [0.5 0.5 0.5 1.5 1 0.5 0.5 0.5 1.5 1 0.5 0.5 1 2 1 3 0.5 0.5 0.5 1.5 1 0.5 0.5 0.5 1.5 1 0.5 0.5 2 2 2 0.5 0.5 0.5 0.5 1.5 0.5 1 1 1 2 0.5 0.5 1 2 1 2 2 1.5 0.5 1 1 1 2 1 4 2 0.5 0.5 0.5 0.5 1.5 0.5 1 1 1 2 1 1 2 1 2 2 1.5 0.5 1 1 1 2 1 4 3 1 2 0.5 0.5 1 2 0.5 0.5 1 2 1 1 2 1 1 2 1 1 1 2 1 4 3 1 2 0.5 0.5 1 2 0.5 0.5 1 2 1 1 2 1 1];
theStrings(2).noteNumbers = [0 49 47 45 45 44 42 44 47 49 52 56 54 54 52 52 56 57 57 52 52 47 44 40 40 44 40 49 44 40 44 40 40 38 42 44 45 47 0 45 44 45 40 45 44 45 0 45 44 45 40 45 44 45 38 45 40 45 38 40 45 44 41 0 45 44 45 42 45 44 45 0 45 44 45 40 45 44 45 38 45 40 45 38 40 42 44 45 47 49 49 0 0 52 52 0 0 54 54 54 52 52 52 54 49 49 47 49 47 49 47 45 51 0 49 49 0 0 52 52 0 0 54 54 54 52 52 52];
theStrings(2).durations = [1 4 4 4 1.5 0.25 0.25 0.5 0.5 0.5 0.5 2 2 2 1 0.5 0.5 2 2 2 0.5 0.5 0.5 0.5 4 4 4 2 2 4 4 2 2 2 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 2 1 1 1 1 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 1 1 1.5 0.5 1 1 2 0.5 0.5 1 2 0.5 0.5 1 2 1 1 2 1 1 2 1 1 1 2 1 4 3 1 2 0.5 0.5 1 2 0.5 0.5 1 2 1 1 2 1 1];
theStrings(3).noteNumbers = [0 44 45 42 45 44 45 42 42 44 40 44 42 44 40 40 42 38 42 40 42 38 40 35 40 35 28 30 30 28 26 28 28 28 33 33 33 28 33 33 33 33 28 28 21 21 28 28 26 26 28 29 30 30 25 25 26 26 28 30 30 0 0 28 28 0 0 26 26 28 30 25 18 25 25 26 26 26 28 30 25 18 0 28 23 16 0 26 26 28 30 25 18 25 25];
theStrings(3).durations = [1 0.5 0.5 0.5 0.5 0.5 0.5 1 0.5 0.5 0.5 0.5 0.5 0.5 1 0.5 0.5 0.5 0.5 0.5 0.5 1 0.5 0.5 0.5 0.5 2 3 1 4 4 1.5 0.5 2 4 4 4 4 4 4 2 2 3 1 3 1 3 1 3 1 2 2 3 1 3 1 4 3 1 2 0.5 0.5 1 2 0.5 0.5 1 2 2 4 1 1 1 1 4 4 1.5 0.5 2 1 1 1 1 1 1 1 1 2 2 4 1 1 1 1 4];
theStrings(4).noteNumbers = [0 45 44 42 40 33 40 45 45 45 40 33 33 40 45 45 45 40 33 33 40 45 45 45 40 33 33 40 45 45 45 40 35 37 40 38 40 40 37 40 38 38 33 40 37 35 37 42 40 37 37 35 38 38 40 42 44 42 33 37 42 45 44 32 35 40 44 45 45 45 44 42 44 45 37 44 42 40 40 40 40 33 38 40 42 38 40 42 44 42 33 37 42 45 44 32 35 40 44 45 45];
theStrings(4).durations = [1 4 4 4 4 0.5 0.5 0.5 0.5 0.5 0.5 1 0.5 0.5 0.5 0.5 0.5 0.5 1 0.5 0.5 0.5 0.5 0.5 0.5 1 0.5 0.5 0.5 0.5 0.5 0.5 1 4 4 4 2 2 4 4 2 2 2 2 4 2 2 4 2 2 4 4 4 1.5 0.5 1 1 2 0.5 0.5 0.5 0.5 2 0.5 0.5 0.5 0.5 2 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 0.5 0.5 0.5 0.5 2 0.5 0.5 0.5 0.5 2 0.5 0.5 0.5 0.5 2 2];
fs = 44100;
spp = 0.25;% seconds per pulse, theVoices is measured in pulses with 4 pulsesper beat
% myMax = theVoices(1).startPulses(length(theVoices(1).startPulses)) + theVoices(1).durations(length(theVoices(1).startPulses));
% for i = 1:length(theVoices)
% if ((theVoices(i).startPulses(length(theVoices(i).startPulses))+theVoices(i).durations(length(theVoices(i).startPulses))) > myMax)
% myMax = theVoices(i).startPulses(length(theVoices(i).startPulses))+theVoices(i).durations(length(theVoices(i).startPulses));
% end
% end
% Create a vector of zeros with length equal to the
% total number of samples in the entire song
n1 = 1;
spp = 0.25;% seconds per pulse, theVoices is measured in pulses with 4 pulsesper beat
myMax = theStrings(1).durations(length(theStrings(1).durations));
for i = 1:length(theStrings)
if ((theStrings(i).durations(length(theStrings(i).durations))) > myMax)
myMax = theStrings(i).durations(length(theStrings(i).durations));
end
end
song = zeros(1, myMax*spp*fs);
% Then add in the notes
for i = 1:length(theStrings) % Cycle through each set of notes
% Convert data arrays to appropriate units
for j = 1:length(theStrings(i).noteNumbers) % Cycle through each note in a set
keynum = theStrings(i).noteNumbers(j);
note = key2note(keynum,theStrings(i).durations(j)*spp);% create sinusoid of correct length to represent a single note
% % Create the envelope
% A = linspace(0, 0.2, (length(note)*0.15)); %rise 15% of signal
% D = linspace(0.2, 0.3, ceil(length(note)*0.05)); %drop of 5% of signal
% S = linspace(0.3, 0.8,(length(note)*0.6)); %delay of 60% of signal
% R = linspace(0.8, 0,(length(note)*0.2)); %drop of 20% of signal
% ADSR = [A D S R];
%
% tone = note .* ADSR; %"Enveloping" the note
tone = note .* hanning(length(note));
n2 = n1 + length(tone) - 1;
song(n1:n2) = song(n1:n2) + tone; %<------- Insert the note into the vector
n1 = n2 + 1;
% song(locstart:locend) = song(locstart:locend) + tone;
end
end
soundsc(song,fs);
%function to generate the note
function xx = key2note(keynum, dur)
% KEY2NOTE Produce a sinusoidal waveform corresponding to a
% given piano key number
% usage: xx = key2note (X, keynum, dur)
% xx = the output sinusoidal waveform
% X = complex amplitude for the sinusoid, X = A*exp(j*phi).
% keynum = the piano keyboard number of the desired note
% dur = the duration (in seconds) of the output note
fs = 44100;
tt = (1/fs):(1/fs):dur;
freq = 440*(2^((keynum -49)/12));%Frequency formula given in lab doc
xx = 0;
harmonic.amp = [0.1155 0.3417 0.1789 0.1232 0.0678 0.0473 0.0260 0.0045 0.0020];
harmonic.phase = [-2.1299 1.6727 -2.5454 0.6607 -2.0390 2.1597 -1.0467 1.8581 -2.3925];
for i = 1:9
H = harmonic.amp(i) * exp(j*harmonic.phase(i));
xx = xx + real(H * exp(j*2*pi*freq*tt*i));%Formula to create a sinusoid
end
% xx = real(exp(j*2*pi*freq*tt));%Formula to create a sinusoid
end
0 commentaires
Réponses (1)
Image Analyst
le 9 Fév 2018
hanning returns a column vector while note is a row vector. Try using a single quote at the end to transpose it:
tone = note .* hanning(length(note))';
3 commentaires
Image Analyst
le 10 Fév 2018
Try
n2 = n1 + length(tone) - 1;
if n2 > length(song)
n2 = length(song);
end
lastToneIndex = n2 - n1 + 1;
song(n1:n2) = song(n1:n2) + tone(1 : lastToneIndex);
Voir également
Catégories
En savoir plus sur Subspace Methods dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!