数组太大,无法在 matlab 中进行除法,但不能 python
Array too big for division in matlab but not python
我遇到的问题是 Matlab 中的数组太大。数组数据来自音频文件。我想得到脉冲响应。
我首先对原始和录制的音频进行 FFT。然后按原始记录的划分。最后逆 FFT 得到脉冲响应。那是我计划做的,但我被困在了部门部分。
坚持使用 Matlab,我发现 python code 可以很好地完成它。我将代码重写到 Matlab 中,问题又回来了。代码不全,但足以说明问题
希望得到多多指教和批评。谢谢
计划做但失败了,所以转到下一个代码
[y_sweep,Fs] = audioread('sweep.wav');
[y_rec,Fs] = audioread('edit_rec_sweep_laptop_1.2.wav');
fft_y1 = abs(fft(y_rec(:,1)));
fft_y2 = abs(fft(y_rec(:,2)));
fft_x = abs(fft(y_sweep));
fft_h1 = fft_y1/fft_x;
% fft_h2 = fft_y2/fft_x;
% fft_h = [fft_h1,fft_h2];
% h1 = ifft(fft1_h);
'Translated' 来自 python 的代码但仍然失败因此来到这里
[a,fs] = audioread('sweep.wav'); % sweep
[b,fs] = audioread('rec.wav'); % rec
a = pad(a,fs*50,fs*10);
b = pad(b,fs*50,fs*10);
[m,n] = size(b);
h = zeros(m,n);
for chan = 1:2
b1 = b(:,1);
ffta = abs(fft(a));
fftb = abs(fft(b1));
ffth = fftb / ffta;
end
pad.m函数(翻译自python但应该是正确的)
function y = pad(data, t_full, t_pre)
[row_dim,col_dim] = size(data);
t_post = t_full - row_dim - t_pre;
if t_post > 0
if col_dim == 1
y = [zeros(t_pre,1);data;zeros(t_post,1)];
% width = [t_pre,t_post];
else
y1 = [zeros(t_pre,1);data(:,1);zeros(t_post,1)];
y2 = [zeros(t_pre,1);data(:,2);zeros(t_post,1)];
y = [y1,y2];
% width = [[t_pre,t_post],[0,0]];
end
else
if col_dim == 1
y = [zeros(t_pre,1);data(t_full - t_pre:end,1)];
% width = [t_pre,0];
else
y = [zeros(t_pre,1);data(t_full - t_pre:end,1)];
% width = [[t_pre,0],[0,0]];
end
end
end
错误
Error using \
Requested 4800000x4800000 (171661.4GB) array exceeds
maximum array size preference. Creation of arrays
greater than this limit may take a long time and
cause MATLAB to become unresponsive. See array size
limit or preference panel for more information.
Error in impulseresponse (line 13)
ffth = fftb / ffta;
在 MATLAB 中,mrdivide()
. This is for solving systems of linear matrix equations. What I think you want is rdivide
的正斜杠是 shorthand,表示为 ./
。
c = a/b
仅当 b
是标量时才等效于标准除法。
c = a./b
是按元素划分,其中a
的每个元素除以b
的对应元素。
[1 2 3] ./ [2 4 9]
>> ans = [0.5, 0.5, 0.3333]
所以您的 "planned to do" 代码的最后一行变为
fft_h1 = fft_y1 ./ fft_x;
我遇到的问题是 Matlab 中的数组太大。数组数据来自音频文件。我想得到脉冲响应。
我首先对原始和录制的音频进行 FFT。然后按原始记录的划分。最后逆 FFT 得到脉冲响应。那是我计划做的,但我被困在了部门部分。
坚持使用 Matlab,我发现 python code 可以很好地完成它。我将代码重写到 Matlab 中,问题又回来了。代码不全,但足以说明问题
希望得到多多指教和批评。谢谢
计划做但失败了,所以转到下一个代码
[y_sweep,Fs] = audioread('sweep.wav');
[y_rec,Fs] = audioread('edit_rec_sweep_laptop_1.2.wav');
fft_y1 = abs(fft(y_rec(:,1)));
fft_y2 = abs(fft(y_rec(:,2)));
fft_x = abs(fft(y_sweep));
fft_h1 = fft_y1/fft_x;
% fft_h2 = fft_y2/fft_x;
% fft_h = [fft_h1,fft_h2];
% h1 = ifft(fft1_h);
'Translated' 来自 python 的代码但仍然失败因此来到这里
[a,fs] = audioread('sweep.wav'); % sweep
[b,fs] = audioread('rec.wav'); % rec
a = pad(a,fs*50,fs*10);
b = pad(b,fs*50,fs*10);
[m,n] = size(b);
h = zeros(m,n);
for chan = 1:2
b1 = b(:,1);
ffta = abs(fft(a));
fftb = abs(fft(b1));
ffth = fftb / ffta;
end
pad.m函数(翻译自python但应该是正确的)
function y = pad(data, t_full, t_pre)
[row_dim,col_dim] = size(data);
t_post = t_full - row_dim - t_pre;
if t_post > 0
if col_dim == 1
y = [zeros(t_pre,1);data;zeros(t_post,1)];
% width = [t_pre,t_post];
else
y1 = [zeros(t_pre,1);data(:,1);zeros(t_post,1)];
y2 = [zeros(t_pre,1);data(:,2);zeros(t_post,1)];
y = [y1,y2];
% width = [[t_pre,t_post],[0,0]];
end
else
if col_dim == 1
y = [zeros(t_pre,1);data(t_full - t_pre:end,1)];
% width = [t_pre,0];
else
y = [zeros(t_pre,1);data(t_full - t_pre:end,1)];
% width = [[t_pre,0],[0,0]];
end
end
end
错误
Error using \
Requested 4800000x4800000 (171661.4GB) array exceeds
maximum array size preference. Creation of arrays
greater than this limit may take a long time and
cause MATLAB to become unresponsive. See array size
limit or preference panel for more information.
Error in impulseresponse (line 13)
ffth = fftb / ffta;
在 MATLAB 中,mrdivide()
. This is for solving systems of linear matrix equations. What I think you want is rdivide
的正斜杠是 shorthand,表示为 ./
。
c = a/b
仅当b
是标量时才等效于标准除法。c = a./b
是按元素划分,其中a
的每个元素除以b
的对应元素。[1 2 3] ./ [2 4 9] >> ans = [0.5, 0.5, 0.3333]
所以您的 "planned to do" 代码的最后一行变为
fft_h1 = fft_y1 ./ fft_x;