Need help abt error.

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 个评论

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)

请先登录,再进行评论。

 采纳的回答

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 个评论

I should note than in this example I knew that treb contained the longest vectors because of your _dur vector.

请先登录,再进行评论。

更多回答(7 个)

moonman
moonman 2011-9-28

0 个投票

Thanks King i am posting the same code again along with the function The code works fine if alto_dur and treb_dur is same. If for anykey lets suppose for key no 1, i give 2*t to alto and 3*t to treb, i get error. My actually code is having 300 keys, i am just writing 5 keys for showing purpose.
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{:});
**********Now the function*****************************
function [tone]=adsr_note(keynum,dur)
fs=8500; tt=0:(1/fs):dur; freq = 440 *(2^((keynum-49)/12)); y=sin(2*pi*freq*tt);
A = linspace(0, 1, (length(y)*0.18)); D = linspace(1, 0.7,(length(y)*0.08)); S = linspace(0.7, 0.7,(length(y)*0.40)); R = linspace(0.7, 0,(length(y)*0.34));
ADSR = [A D S R] ;
w = zeros(size(y)); w(1:length(ADSR)) = ADSR;
tone=y.* w;
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
King at present this code is ok if u change the alto_dur to
alto_dur=[ 3*t t 2*t t 5*t]; % i have changed first duration
then u will get error as treb_dur first element id 2*t and alto_dur first element is 3*t
how to overcome this problem b/c i want to give different timing to keys of alto and treble
Thanks a lot
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{:});
moonman
moonman 2011-9-28

0 个投票

Yes u r right can u write those few lines for me as i am totally exhasuted now it will be great help for me
thanks
moonman
moonman 2011-9-28

0 个投票

Thats great help King, now the code is running perfect but the condition is that the treb vector of duration is always more than alto. But if i make any duration of treb less than alto, then it gives error
Lets suppose alto first key is having 3*t and treb first key is having duration 2*t, then it gives error..
How to go abt that
b/c in piano, any key can have higher timing
Sorry i m bothering u again and again
Thanks a lot King, u again saved my precious time. I modified ur code and now it can handle both situations, key time is higher or lower,it will work
THANKS A LOT WAYNE KING
for kk = 1:length(treb)
if (length(treb{kk})==length(alto{kk}))
alto{kk} = alto{kk};
else if (length(treb{kk})>length(alto{kk}))
alto{kk} = [alto{kk}, zeros(1,length(treb{kk})-length(alto{kk}))];
else
treb{kk} = [treb{kk}, zeros(1,length(alto{kk})-length(treb{kk}))];
end
end
end

标签

Community Treasure Hunt

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

Start Hunting!

Translated by