(SOLVED) Horzcat Dimensions error in joining .wav files?
Show older comments
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
Star Strider
on 16 Nov 2014
‘and this keeps giving me an error’
What is the error?
What are the dimensions of your files?
What are their sampling frequencies?
James
on 16 Nov 2014
Answers (2)
Geoff Hayes
on 16 Nov 2014
Edited: Geoff Hayes
on 16 Nov 2014
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
James
on 16 Nov 2014
Geoff Hayes
on 16 Nov 2014
James - what are the dimensions of your samples? Are they row or column vectors?
James
on 16 Nov 2014
James
on 16 Nov 2014
Geoff Hayes
on 16 Nov 2014
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.
James
on 16 Nov 2014
James
on 16 Nov 2014
Geoff Hayes
on 17 Nov 2014
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.
Star Strider
on 16 Nov 2014
1 vote
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
Star Strider
on 16 Nov 2014
Edited: Star Strider
on 16 Nov 2014
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.
Categories
Find more on Descriptive Statistics and Insights in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!