(SOLVED) Horzcat Dimensions error in joining .wav files?

Hello! Thanks for the help it is solved.
I am trying to join two .wav files together, one starting after the first. They are not the same file so they are different lengths and this keeps giving me an error.
Here are methods I have tried which have resulted in the same error:
1. sample1 = snd(START:END); sample2 = snd2(START:END2); samples = [sample1 ; sample2];
2. samples = [snd; snd2];
3. samples = [snd(START:END,:), snd2(START:END2,:)];
4. samples = [snd(START:END,:), snd2(START:END2)];
If you know a successful way of doing this, please let me know.

2 Comments

‘and this keeps giving me an error’
What is the error?
What are the dimensions of your files?
What are their sampling frequencies?
The fact that they are not the same length, Star Strider....... The error is "Error using vertcat Dimensions of matrices being concatenated are not consistent."

Sign in to comment.

Answers (2)

James - you should concatenate your samples along the singleton dimension. Try the following
[r1,c1] = size(sample1);
[r2,c2] = size(sample2);
Assuming that your sample1 and sample2 arrays are both either column or row vectors, then r1 and r2 will be one (for a row vector) or c1 and c2 will be one (for a column vector). If both are row vectors, then do a horizontal concatenation as
if r1==1 && r2==1
samples = [sample1 , sample2];
end
Else if both are column vectors, then do a vertical concatenation as
[r1,c1] = size(sample1);
[r2,c2] = size(sample2);
if r1==1 && r2==1
samples = [sample1 , sample2];
elseif c1==1 && c2==1
samples = [sample1 ; sample2];
else
error('cannot concatenate the two sample vectors!');
end
Try the above and see what happens!
EDIT changed [r2,c2] = size(sample2); to use sample2 and not sample1.

8 Comments

I get the error message from the above code.
James - what are the dimensions of your samples? Are they row or column vectors?
[snd, fs, bits] = wavread(s1) gives me these variables... snd = 162432x1 double, fs = 8000, bits = 8.
Extra variables that may be useful. snd2 = 1x324684 double fs2 = 22050
PS. I noticed the error from r2 c2 with sample2 instead of 1, still gave the else error.
So there are two problems with your sample vectors - snd is a row vector and snd2 is a column vector. This can be easily fixed by transposing one of them, say
snd2 = snd2';
The second problem is that each has a different sampling frequency. So for every one second of data, s1 has 8000 samples whereas s2 has 22050 samples. You probably want to downsample or decimate s2 to the sampling rate of s1.
Thanks I will try it! Didn't realise they would be so different wow.
When I transpose I notice that r1 r2 and c1 c2 are never the same. Without transposing c1=1, c2=2, r1=598528, r2=324864. So transposing will never fit the if loop you provided. Could this be part of the problem?
Does snd2 have two channels? In a previous comment, you said that snd2 is 1x324684. See Star Strider's comment below for a way to get around this problem.

Sign in to comment.

One is a column vector and one is a row vector. They won’t concatenate no matter what you do.
Unless you want them for some reason to sound weird (with the two different sampling frequencies), if you have the Signal Processing Toolbox, use the resample function to give both of them the same sampling frequency (resample one to the other’s sampling frequency), then transpose one of them and do the appropriate vertical or horizontal concatenation on both of them, depending on whether they are then both column vectors or row vectors, respectively.

1 Comment

Since ‘c1=1’, before concatenating, create a duplicate of the single column of ‘s1’ to make it a (598528x2) matrix by doing:
s1 = [s1 s1];
You should then be able to vertically concatenate them. Note that this operation will not affect the way you hear ‘s1’, since it is by definition monaural anyway.

Sign in to comment.

Categories

Find more on Descriptive Statistics and Insights in Help Center and File Exchange

Asked:

on 16 Nov 2014

Commented:

on 17 Nov 2014

Community Treasure Hunt

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

Start Hunting!