How to concentate multiple audio signals in to one instance

Hello there,
I am instanciating several instances of a class called Signal and want to merge them into one instance. I know there's "cat" which helps, but I am struggling to find the correct syntax to make it happen for an array of instances of any given size:
signal1 = Signal(1,281.6,-pi/2,1,fa); % Amplitude, Frequence, Phase, Duration, Sample Rate
signal2 = Signal(1,200.6,Phase,1,fa); % second signal
signalvec = [signal1, signal2];
now I could go:
concattedsignal = cat(2,signalvec(1),signalvec(2);
but it doesn't help me if the array gets bigger. I tried:
for i = 1 : length(signalvec)
concattedsignal = cat(2,signalvec(i));
end
but this will always just give me the last instance of the array. What can I do to concentate every instance of an signal into one new signal?
Thanks alot!

Answers (1)

The loop only gives you the last signal because you overwrite concattedsignal each time. You need to use concattedsignal in the loop and add on to it. Try something like this:
concattedsignal = [];
for i = 1 : length(signalvec)
concattedsignal = cat(2,concattedsignal,signalvec(i));
end

3 Comments

Thanks for the quick response! Unfortunately, this only gives me back the last signal of the array as well.
I know how to solve this problem in Java but I just can't do it in Matlab. There must be a better way than: concattedsignal = cat(2.signalvec(1),signalvec(2),signalvec(3)...) and so on.
OK. How about making signalvec a cell array:
signalvec = {signal1, signal2};
concattedsignal = [signalvec{:}];
Actually, regardless of that, I'm not sure what the purpose is here because you have this already:
signalvec = [signal1, signal2];
which does exactly the same thing as this:
signalvec = cat(2,signal1,signal2);
So if you are going to concatenate elements of an array in a loop using cat, but in order to do so, first you need to concatenate those same elements into an array using [], then what is the point?
Perhaps a mat-file of data with a few actual signals (I mean the variables signal1, signal2, etc.) would help to clarify.
You are absolutely right and I thought of the same thing before, but wasn't too sure about it. I will leave it like this for now, thank you. Maybe you can help me with a simpler problem, I'm still having a hard time with the semantic of Matlab unfortunately.
Im still having an array of Signals and want to create an Interference. A signal is constructed like this:
y = Amplitude * cos(2*pi*frequency*timeVector * -pi/2);
y would be a total of 11025 values, because of the sample rate of 11025.
an Interference would be the sum of two signals. So far so good.But if I am having an array of Signals, which only differ in frequencies, I can't add them up in a loop for sum reason. I have tried your syntax:
signalvec = [signal1, signal2, signal3];
ys = [];
for i = 1 : length(signalvec)
y = Amplitude * cos(2*pi*signal(i).frequency*t * -pi/2);
ys = [ys y];
end
This should save all the values of every different y in ys. So why does the following not work:
for i = 1 : length(ys)
y = y + ys(i)
end
I am gettin back just 1 value insteand of multiple of 11025. On the other hand, if i just go:
y = Amplitude * cos(2*pi*signal(1).frequency*t * -pi/2);
y2 = Amplitude * cos(2*pi*signal(2).frequency*t * -pi/2);
y3 = y + y2;
This works perfectly fine and creates the interference.
As you might have guessed, I'm completely new to Matlab and this topic itself, so your help is already greatly appreciated.
Thanks

Sign in to comment.

Asked:

on 19 Dec 2021

Commented:

on 21 Dec 2021

Community Treasure Hunt

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

Start Hunting!