Need help abt error.
4 次查看(过去 30 天)
显示 更早的评论
Here is my code. till the time the alto_dur and teb_dur are exactly same vectors, then there is no problem but if i make
alto_dur = [ 2*t t 2*t t 5*t]; and
treb_dur=[*t* t 2*t t 5*t]; I get error
??? Error using ==> plus Matrix dimensions must agree.
Error in ==> test at 38 final_song = cellfun(@plus, alto, treb, 'Uniform', 0); % Alto Tones and Treble tones are added
The code is here fs=8500; t=.18; % this t is used in duration as a multiple or single identity % Key Number and duration of each note %************************Alto************ alto_keys = [ 46 46 0 46 0]; alto_dur = [ 2*t t 2*t t 5*t]; %************************************
%******************Treeble*********************************** % It is high or acute part of the musical system
treb_keys=[56 56 0 56 0 ]; % Duration of ech note treb_dur=[2*t t 2*t t 5*t]; %********************************************
alto = cell(1, length(alto_keys)); % It will creat a cell array having one row and % columns equal to number of keys for i = 1:length(alto_keys) % The for loop continues till number of Keys alto{i} = adsr_note(alto_keys(i), alto_dur(i)); % Function adsr_note is called which calculates %tone for each key for the specified duration and apply ADSR Envolop on it. This tone is stored in % alto{i} end
treb = cell(1, length(treb_keys)); % It will creat a cell array having one row and % columns equal to number of keys for k = 1:length(treb_keys) % The for loop continues till number of Keys treb{k} = adsr_note(treb_keys(k), treb_dur(k)); % Function adsr_note is called which calculates %tone for each key for the specified duration and apply ADSR Envolop on it. This tone is stored in % treb{i} end
final_song = cellfun(@plus, alto, treb, 'Uniform', 0); % Alto Tones and Treble tones are added % Option Uniform or UniformOutput set to 0 (or false) means, that output from function will be stored in cell array
% Concatinating Tone Vectors tone = cat(2, final_song{:}); % It will concatinate arrays of final_song in a row. 2 means %row. If we will use 1, it will concatinate in a column
1 个评论
Wayne King
2011-9-28
Hi, it's hard to read your code above. If the error is occurring at
cellfun(), please give us the sizes of alto and treb
size(alto), size(treb)
采纳的回答
Wayne King
2011-9-28
Sorry, I just noticed I hadn't changed something. Here is a quick hack with a for loop. I'm sure there is a more elegant way if I had time. Basically you have to figure out which vectors are longer in alto or treb and then pad the shorter vector.
fs=8500; t=.18;
alto_keys = [ 46 46 0 46 0];
alto_dur=[ 3*t t 2*t t 5*t];
treb_keys=[56 56 0 56 0 ];
treb_dur=[3*t t 3*t t 5*t];
alto = cell(1, length(alto_keys));
for i = 1:length(alto_keys) alto{i} = adsr_note(alto_keys(i), alto_dur(i));
end
treb = cell(1, length(treb_keys));
for k = 1:length(treb_keys) treb{k} = adsr_note(treb_keys(k), treb_dur(k)); end
for kk = 1:length(treb)
if (length(treb{kk})==length(alto{kk}))
alto{kk} = alto{kk};
else
alto{kk} = [alto{kk}, zeros(1,length(treb{kk})-length(alto{kk}))];
end
end
final_song = cellfun(@plus, alto, treb, 'Uniform', 0);
tone = cat(2, final_song{:});
1 个评论
Wayne King
2011-9-28
I should note than in this example I knew that treb contained the longest vectors because of your _dur vector.
更多回答(7 个)
Wayne King
2011-9-28
Hi, I don't have any problem running this code:
fs=8500; t=.18;
alto_keys = [ 46 46 0 46 0];
alto_dur = [ 2*t t 2*t t 5*t];
treb_keys=[56 56 0 56 0 ];
treb_dur=[2*t t 2*t t 5*t];
alto = cell(1, length(alto_keys));
for i = 1:length(alto_keys) alto{i} = adsr_note(alto_keys(i), alto_dur(i));
end
treb = cell(1, length(treb_keys));
for k = 1:length(treb_keys) treb{k} = adsr_note(treb_keys(k), treb_dur(k)); end
final_song = cellfun(@plus, alto, treb, 'Uniform', 0);
tone = cat(2, final_song{:});
with your adsr_note function. Maybe you have something left over in your workspace that is causing an error? Or maybe you're using a differnt version of adsr_note (perhaps you saved it in a different folder). Can you clear your workspace and try it?
Does
>>which adsr_note
return what you think it should?
Wayne
0 个评论
Wayne King
2011-9-28
Can you just pad the shorter vectors with zeros to make them equal length with the longer ones. That way the vector addition will be defined.
fs=8500; t=.18;
alto_keys = [ 46 46 0 46 0];
alto_dur = [ 3*t t 3*t t 5*t];
treb_keys=[56 56 0 56 0 ];
treb_dur=[3*t t 3*t t 5*t];
alto = cell(1, length(alto_keys));
for i = 1:length(alto_keys) alto{i} = adsr_note(alto_keys(i), alto_dur(i));
end
treb = cell(1, length(treb_keys));
for k = 1:length(treb_keys) treb{k} = adsr_note(treb_keys(k), treb_dur(k)); end
alto = cellfun(@(x) [x zeros(1,length(treb{1})-length(alto{1}))], alto,'UniformOutput',false);
final_song = cellfun(@plus, alto, treb, 'Uniform', 0);
tone = cat(2, final_song{:});
fs=8500; t=.18;
alto_keys = [ 46 46 0 46 0];
alto_dur = [ 3*t t 3*t t 5*t];
treb_keys=[56 56 0 56 0 ];
treb_dur=[3*t t 3*t t 5*t];
alto = cell(1, length(alto_keys));
for i = 1:length(alto_keys) alto{i} = adsr_note(alto_keys(i), alto_dur(i));
end
treb = cell(1, length(treb_keys));
for k = 1:length(treb_keys) treb{k} = adsr_note(treb_keys(k), treb_dur(k)); end
alto = cellfun(@(x) [x zeros(1,length(treb{1})-length(alto{1}))], alto,'UniformOutput',false);
final_song = cellfun(@plus, alto, treb, 'Uniform', 0);
tone = cat(2, final_song{:});
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Octave 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!