【发布时间】:2015-04-03 16:39:22
【问题描述】:
我有一段 Octave / Matlab 代码,我从 Andy 和团队那里得到了很多帮助。我现在遇到的问题是没有足够的内存来发出持续时间更长的信号。
我的解决方案是:
1) 将向量循环转换为 for 循环。 (这里有问题)
2) 让 for 循环将循环的每个段导出为 wav 文件,而不是执行向量代码所做的事情,即附加它。 (这里有问题)
3) 使用 sox 加入每个波形文件段。
大多数在线示例都是从 for 循环到矢量化循环,而不是相反,有什么想法吗?我也愿意接受其他建议来解决我的记忆问题。注意:我在 rasberry pi 2 上使用 1 gig 的 ram,它可以工作并且速度非常快,我只是想获得持续时间更长的信号,并且导出每个段应该允许为此。
我正在使用与 Matlab 兼容的 Octave。
请参阅下面的工作矢量化代码: 它基于此处找到的 Paul Nasca 拉伸算法 http://www.paulnasca.com/algorithms-created-by-me#TOC-PaulStretch-extreme-sound-stretching-algorithm
urlwrite('http://www.onewithall.net/rttmpfiles/3sec8000.wav','3sec8000.wav');
inputfn='3sec8000.wav' %change this to test another file
[d, fs, bps]=wavread(inputfn);
inputlen=rows (d)/fs;
printf ("Original duration of file in seconds = %.2f s\n", rows (d)/fs);
dur=60; %duration / length you want the file to be in seconds
stretch = dur/rows(d)*fs; %how much I need to stretch the file to get it to be the duration I want
windowsize = round (0.25 * fs);
step = round ((windowsize/2)/stretch);
## original window
fwin = @(x) (1-x.^2).^1.25;
win = fwin (linspace (-1, 1, windowsize));
#win = hanning (windowsize)';
## build index
ind = (bsxfun (@plus, 1:windowsize, (0:step:(rows(d)-windowsize))'))';
cols_ind = columns(ind);
## Only use left channel
left_seg = d(:,1)(ind);
clear d ind;
## Apply window
left_seg = bsxfun (@times, left_seg, win');
## FFT
fft_left_seg = fft (left_seg);
clear left_seg
#keyboard
## overwrite phases with random phases
fft_rand_phase_left = fft_left_seg.*exp(i*2*pi*rand(size(fft_left_seg)));
clear fft_left_seg;
ifft_left = ifft (fft_rand_phase_left);
clear fft_rand_phase_left;
## window again
ifft_left = bsxfun (@times, real(ifft_left), win');
## restore the windowed segments with half windowsize shift
restore_step = floor(windowsize/2);
ind2 = (bsxfun (@plus, 1:windowsize, (0:restore_step:(restore_step*(cols_ind-1)))'))';
left_stretched = sparse (ind2(:), repmat(1:columns (ind2), rows(ind2), 1)(:), real(ifft_left(:)), ind2(end, end), cols_ind);
clear ind2 ifft_left win;
left_stretched = full (sum (left_stretched, 2));
## normalize
left_stretched = 0.8 * left_stretched./max(left_stretched);
printf ("converted %s =%.2f(s) file to stretched.wav = %.2f(s)\n", inputfn, inputlen, rows (left_stretched)/fs);
wavwrite (left_stretched, fs, bps, "streched.wav");
我尝试通过在关键点放置 display('line') 来跟踪问题。它看起来像这条线
left_stretched = sparse (ind2(:), repmat(1:columns (ind2), rows(ind2), 1)(:), real(ifft_left(:)), ind2(end, end), cols_ind);
上面的行似乎只有在我用完 ram 时才有问题。 它说错误下标索引必须是正整数或逻辑数。请注意,只有当我通过设置 dur=60*1800 尝试使用长持续时间时内存不足时才会发生这种情况。如果我设置 dur=60*10 一切正常。
【问题讨论】:
-
你在哪里记错了?
bsxfun?fft?一个最小的工作示例会很棒。 -
@bla 上面的代码是一个工作示例,它甚至可以从网站中提取音频文件进行测试。我更新了问题并添加了问题开始的那一行。