signals addition

5 views (last 30 days)
Sivakumaran Chandrasekaran
Answered: Sk Group on 8 Feb 2021
Hi, I have the following code.
x1=wavread('speech.wav'); x2=wavread('speech_noise.wav');
I have to do x2-x1
I am getting the error as Dimension mismatch. How to rectify it?

Accepted Answer

Richard Brown
Richard Brown on 3 May 2012
Assuming they correspond to the same sampling rates, truncate one of them to be of the same length as the smaller one:
n1 = length(x1);
n2 = length(x2);
% Assuming n1 < n2
x2 = x2(1:n1)
Or maybe you want to interpolate
x2 = interp1(1:n2, x2, linspace(1, n2, n1));
Or maybe you want to refer back to your other question: http://www.mathworks.com/matlabcentral/answers/36587-adding-of-signals
  4 Comments
Richard Brown
Richard Brown on 3 May 2012
It depends - is x2 a modified version of x1? And if so, why is it a different size?
Sivakumaran Chandrasekaran
i am speaking inside a room ... x1
i am speaking the same content in a crowd(noises)....x2

Sign in to comment.

More Answers (1)

Sk Group
Sk Group on 8 Feb 2021
MATLAB CODE:
function [y n] = sigadd(x1,n1,x2,n2)
if n1(1)< n2(1)
a = n1(1)
x1 = [zeros(1,abs(n1(1)-n2(1))) x1]
else
a = n2(1)
x1 = [zeros(1,abs(n1(1)-n2(1))) x1]
end
if n1(end)>n2(end)
b = n1(end)
x2 = [x2 zeros(1,abs(n1(end)-n2(end)))]
else
b = n2(end)
x2 = [x2 zeros(1,abs(n1(end)-n2(end)))]
end
n = a:b;
y = x1+x2;
MATLAB CODE:
function [y n] = sigadd_another_method(x1,n1,x2,n2)
n = min(min(n1),min(n2)):max(max(n1),max(n2));
y1 = zeros(1,length(n));
y2 = y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1;
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
y = y1+y2;

Categories

Find more on Modeling 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!