Arrays have incompatible sizes for this operation.

4 次查看(过去 30 天)
i have the following code which mixes two audios as the user enters the number he wants to mix
freq = str2double(get(handles.edit3,'string'));
scnd = str2double (get(handles.edit10,'string'));
[y,Fs] = audioread("Sound and "+freq+" hz.wav");
[x,Fs] = audioread(scnd+".wav");
mix = y+x;
soundsc(mix,sample);
Nsamps = length(mix);
t = (1/Fs)*(1:Nsamps);
y_fft = abs(fft(mix));
y_fft = y_fft(1:Nsamps/2);
f = Fs*(0:Nsamps/2-1)/Nsamps;
axes(handles.axes3);
plot(f, y_fft)
xlim([0 5000])
ylim([0 5000])
end
some values seem to work while others dont and i get Arrays have incompatible sizes for this operation.

采纳的回答

Mathieu NOE
Mathieu NOE 2022-8-29
hello
in case the two files have different number of samples, either you truncate the longest one or you padd the shortest one (with zeros);
I opted for the second solution :
freq = str2double(get(handles.edit3,'string'));
scnd = str2double (get(handles.edit10,'string'));
[y,Fs] = audioread("Sound and "+freq+" hz.wav");
[x,Fs] = audioread(scnd+".wav");
%%
x = x(:); % make x col vector
y = y(:); % make y col vector
lx = numel(x);
ly = numel(y);
if ly>lx
x = [x; zeros(ly-lx,1)];
elseif ly<lx
y = [y; zeros(lx-ly,1)];
end
%%
mix = y+x;
% soundsc(mix,sample); % ?? 2nd argument should be Fs ?
soundsc(mix,Fs); % please double check 2nd argument
Nsamps = length(mix);
t = (1/Fs)*(1:Nsamps);
y_fft = abs(fft(mix));
y_fft = y_fft(1:Nsamps/2);
f = Fs*(0:Nsamps/2-1)/Nsamps;
axes(handles.axes3);
plot(f, y_fft)
xlim([0 5000])
ylim([0 5000])
  2 个评论
Mohamed Turkmani
Mohamed Turkmani 2022-9-2
i tried it by the way it works but the audio at the end repeats for couple of seconds
Mathieu NOE
Mathieu NOE 2022-9-2
hmm
I wonder if your files have one or more channels , so what are dimensions of x and y just after your read them
[y,Fs] = audioread("Sound and "+freq+" hz.wav");
[x,Fs] = audioread(scnd+".wav");
maybe your "echoes" are related to these lines I introduced , have to change that for multi channel audio (if that is the case) :
x = x(:); % make x col vector
y = y(:); % make y col vector

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile 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