How can i make the length of two vectors of two sound files(wav) equal to each other?
5 views (last 30 days)
Show older comments
I want to add (operation +) two audio files in Matlab, but the console shows me an error, saying the lengths of the two files are different. How can I fix this? This is the code that I'm implementing to do this:
clc
clear all
global Fs1;
%llamar archivo
[s Fs]=wavread('avemaria.wav'); %s = señal, fs = frecuencia de muestreo
[s1 Fs1]=wavread('sint1.wav');
%Tiempo de avemaria
tiempo=size(s,1)/Fs;
x=0:1/Fs:tiempo;
%Tiempo de Sintetizador1
tiempo1=size(s1,1)/Fs1;
x1=0:1/Fs1:tiempo1;
%Suma de los dos audios
sum=s+s1;
%Reproducir la suma de los dos audios
sound(sum, Fs);
To know the length of the two sounds I did this:
>> length(s)
ans =
882011
>> length(s1)
ans =
882438
And the console shows me the error:
Error using +
Matrix dimensions must agree.
Error in pruebas (line 18)
sum=s+s1;
How can I fix this problem?
I appreciate your help!!
0 Comments
Answers (2)
dpb
on 19 Oct 2013
First, don't use sum as a variable name; it's an internal Matlab function you'll not want to alias...
Second, to fix the lenght problem,
L=min(length(s),length(s1)); % the shorter length irrespective of which
stot = s(1:L) + s1(1:L); % sum the two commensurate subsections
You could alternatively, of course, truncate the longer to the shorter...then you need to tell which is which w/ a test
3 Comments
Image Analyst
on 22 Apr 2017
You forgot to show your code. Post it, plus the entrie error message (ALL the red text) in a new question.
See Also
Categories
Find more on Audio I/O and Waveform Generation 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!