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.

 채택된 답변

Image Analyst
Image Analyst 2012년 4월 26일

0 개 추천

The imresize in your Image Processing Toolbox will also work on one dimensional signals.
% Make wave 2 the same length as wave 1.
resizedWave2 = imresize(wave2, length(wave1));

댓글 수: 12

Sivakumaran Chandrasekaran
Sivakumaran Chandrasekaran 2012년 5월 3일
Hi Image Analyst, Just now I checked your coding and its not working for my application.
Regards,
Siva
Image Analyst
Image Analyst 2012년 5월 3일
OK, what does this say
length(wave1)
length(wave2)
length(resizedWave2)
Tell me what those lengths are, or else tell me what "not working" means to you (such as an error message or something).
Sivakumaran Chandrasekaran
Sivakumaran Chandrasekaran 2012년 5월 3일
Hi Image Analyst,
My question is ,
x1=wavread('speech.wav');
x2=wavread('speech_noise.wav');
v=x1-x2
I am getting the error as
??? Error using ==> minus
Matrix dimensions must agree.
Error in ==> Untitled at 3
v=x1-x2
Image Analyst
Image Analyst 2012년 5월 3일
Yes, I know -- they're different lengths so you can't subtract them. But show me where you tried the code I gave you to make them the same length.
Walter Roberson
Walter Roberson 2012년 5월 3일
You do not appear to have used imresize() there as Image Analyst indicated.
Sivakumaran Chandrasekaran
Sivakumaran Chandrasekaran 2012년 5월 3일
Hi walter,
are you saying that i should not use the command "imresize" to change the dimension of a signal. Am i right
Walter Roberson
Walter Roberson 2012년 5월 3일
No, I am saying you omitted imresize() where you needed to have it.
Walter Roberson
Walter Roberson 2012년 5월 3일
newsize = min(size(x1), size(2));
x1 = imresize(x1, newsize);
x2 = imresize(x2, newsize);
v = x1 - x2;
What will you do in the places where the result would be out of range for sounds? For example, -32768 for x1 and +32767 for x2 would try to subtract to -65535 but that is probably going to be out of range.
Sivakumaran Chandrasekaran
Sivakumaran Chandrasekaran 2012년 5월 3일
Thanks walter. I shall check it and let you know.
Image Analyst
Image Analyst 2012년 5월 3일
It IS possible though that resizing doesn't make sense. Maybe it does for you because they are so close in length, like if one was 30 seconds long and the other was 31 seconds long. But would it make sense to subtract a sound signal that was originally 15 minutes long from one that is only 10 seconds long? What is the interpretation/meaning of that?
meziane madani
meziane madani 2017년 5월 4일
편집: meziane madani 2017년 5월 4일
hello; i tried to do what you said, but i have this error :imresize: IM must be a grayscale or RGB image, so i don't think that we can applicate it from a song
Image Analyst
Image Analyst 2017년 5월 4일
It can be a song. Here's proof for two stereo waveforms:
[y1, fs1] = audioread('C:\Users\Public\Music\Sample Music\Sleep Away.mp3'); % Stereo
[y2, fs2] = audioread('C:\Users\Public\Music\Sample Music\Maid with the Flaxen Hair.mp3'); % Stereo
% [y2, fs2] = audioread('guitartune.wav'); % Mono signal
whos y1 fs1
% Name Size Bytes Class Attributes
%
% fs1 1x1 8 double
% y1 8845056x2 141520896 double
whos y2 fs2
% Name Size Bytes Class Attributes
%
% fs2 1x1 8 double
% y2 7483391x2 119734256 double
[rows1, columns1] = size(y1)
[rows2, columns2] = size(y2)
% Make wave 2 the same length as wave 1. Code if both are stereo.
resizedWave2 = imresize(y1, [rows2, columns2]);
whos resizedWave2 % Proves y1 same size as y2.
It can get a little tricky if one is stereo and one is mono. Is that what you had?

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Richard Brown
Richard Brown 2012년 4월 26일

0 개 추천

Also you can use interp1 if you don't have the image processing toolbox
Sk Group
Sk Group 2021년 2월 8일

0 개 추천

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
GULLA 2024년 9월 1일

0 개 추천

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

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:

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Simulation, Tuning, and Visualization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by