【问题标题】:converting a vector loop to a for loop because of memory limitations由于内存限制,将向量循环转换为 for 循环
【发布时间】: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 上面的代码是一个工作示例,它甚至可以从网站中提取音频文件进行测试。我更新了问题并添加了问题开始的那一行。

标签: matlab octave sox


【解决方案1】:

你还记得我吗?我是您发布的初始代码的作者。在代码下方为 for 循环。我已经用 800 秒的输出 len 对此进行了测试。

## based on http://hypermammut.sourceforge.net/paulstretch/
## https://github.com/paulnasca/paulstretch_python/blob/master/paulstretch_steps.png

more off
inputfn = "original.wav"
[d, fs, bps] = wavread (inputfn);
inputlen=rows  (d)/fs;

printf ("Original  duration of file in seconds = %.2f s\n", rows (d)/fs);

target_duration = 60; # in seconds
stretch = target_duration/inputlen;
# 1/4 s window len
windowsize = round (0.25 * fs);

# stepwidth between windows
step = round ((windowsize/2)/stretch);
numsteps = floor((rows(d)-windowsize)/step);

## restore the windowed segments with half windowsize shift
restore_step = floor (windowsize / 2);

## stetched duration
stretched_len = (numsteps*restore_step+windowsize)/fs;
printf ("Stretched duration of file in seconds = %.2f s\n", stretched_len);
stretched = zeros (numsteps*restore_step+windowsize, 1);
if (!exist ("out", "dir"))
  mkdir ("out");
endif

## Matrix which holds the freq of the maximum amplitude and the max. amplitude
chunks_stats = zeros (numsteps, 2);

## original window
fwin = @(x) (1-x.^2).^1.25;
win = fwin (linspace (-1, 1, windowsize));

## loop over all windows
for k = 1:numsteps
  if (! mod(k, 50))
    printf ("Calculating chunk %i of %i...\n", k, numsteps);
    fflush (stdout);
  endif

  ## Only use left channel
  s_ind = (k - 1) * step + 1;
  e_ind = s_ind + windowsize - 1;
  tmp = win' .* d(s_ind:e_ind, 1);

  ## FFT, overwrite phases with random phases and IFFT
  tmp = fft(tmp);
  [m, ind] = max (abs(tmp(1:numel(tmp)/2)));
  # Frequency in Hz
  chunks_stats(k, 1) = (ind-1)/windowsize*fs;
  # max Amplitude
  chunks_stats(k, 2) = m;
  printf ("Freq =  %.2f Hz, max = %.2f\n", chunks_stats(k, :));
  tmp = ifft (tmp .* exp(i*2*pi*rand(size(tmp))));

  ## window again
  tmp = win' .* real (tmp);
  fn = sprintf ("out/out_%04i.wav", k);
  wavwrite (tmp, fs, bps, fn);
  s_ind = (k - 1) * restore_step + 1;
  e_ind = s_ind + windowsize - 1;
  stretched (s_ind:e_ind) += tmp;
endfor

## normalize
stretched = 0.8 * stretched./max(stretched);
wavwrite (stretched, fs, bps, "stretched.wav");

如果您想编写多个 wav 稍后将它们连接起来,这会有点困难,因为重叠的窗口。但我认为这段代码在 BeagleBoneBlack 上运行良好。

编辑:添加了保存块到单独的文件,并将每个块的这个信号的最大振幅和频率收集到 chunk_stats。

【讨论】:

  • 是的,安迪,我记得你和你给我的伟大代码。我会尝试你的答案,看看我是否可以拼凑出一种将每个数组部分导出为波形文件的方法。再次感谢
  • 您想将块导出为 wav 并在之后将它们连接起来的原因是什么?
  • 我需要用 fft 分析每个 wav 文件/块,以获得它的频率、幅度和相位(我已经有了这个功能)。我将所有文件的频率、幅度和相位数据放入一个数组中,并根据我选择的频率、幅度和/或相位使​​用 sox 连接所选文件。
  • @RickT:老实说:我怀疑你理解我的代码或“paulstretch”算法。例如,“获取相位”是没有意义的,因为它被设置为随机值。另一点:如果您想分析每个块并“将频率...放入数组中”,那么您将再次受到相同的内存限制。如果你能解释一下你的最终目标是什么,我可以提供更好的帮助,但在这一点上,我真的很沮丧,帮助朝着可能错误的方向迈出了一小步
  • @RickT 如果已编辑 上面的代码将块写入分隔文件并收集最大振幅和匹配频率。
猜你喜欢
  • 1970-01-01
  • 2016-02-14
  • 2021-03-12
  • 2018-02-22
  • 2017-04-26
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多