adding of signals
Mostrar comentarios más antiguos
Hi, When we add two images, we need to maintain same dimension to add. I need to add two wave signals. How to alter their dimensions, so that i can add.
Respuesta aceptada
Más respuestas (3)
Richard Brown
el 26 de Abr. de 2012
0 votos
Also you can use interp1 if you don't have the image processing toolbox
Sk Group
el 8 de Feb. de 2021
0 votos
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;
GULLA
el 1 de Sept. de 2024
0 votos
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;
1 comentario
Image Analyst
el 1 de Sept. de 2024
You could do that simpler by simply setting the last element to zero to make it match the size of the longer vector.
% Add two vectors together, padding the shorter one
% with 0's, if needed, to make them the same size.
function [y, n] = sigadd(x1, n1, x2, n2)
if n1 > n2 % x1 is longer so pad x2
x2(n1) = 0; % Pad out with zeros.
elseif n1 < n2 % x2 is longer so pad x1
x1(n2) = 0;
end
y = x1 + x2;
n = numel(y);
This assumes both x1 and x2 are vectors (monochrome audio signals, not 2-D stereo audio signals). See this Tricks and Tips item:
Categorías
Más información sobre Simulation, Tuning, and Visualization en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!