[英]Converting .m to .mex for Octave speed-up
I have a .m
script that runs rather slowly in Octave (about 2 minutes typically), and not quite so slowly in MATLAB (about 7 seconds), and I presume the performance would be enhanced by compiling the program to a .mex
file (especially for Octave). 我有一个
.m
脚本,它在Octave中运行得很慢(通常大约2分钟),而在MATLAB中却不太慢(大约7秒),并且我认为通过将程序编译为.mex
文件可以提高性能(特别是八度)。 Unfortunately, the MATLAB compiler function mcc
no longer supports the -x
option to generate a .mex
file. 不幸的是,MATLAB编译器函数
mcc
不再支持-x
选项来生成.mex
文件。
Is there a workaround for this? 有没有解决方法?
Edit: In response to the comments 编辑:回应评论
Below code's purpose is to smooth a spectrum while leaving high power regions mostly intact. 以下代码的目的是使频谱平滑,同时保留大部分高功率区域。 It does this by taking local averages with a window size that depends on the magnitude of the spectrum at a point.
它通过采用窗口大小的局部平均值来做到这一点,该窗口大小取决于一点上频谱的大小。
I do this with just a simple for
loop, which is why it's so slow (vectorizing won't work easily because it would require a matrix with so many elements as to exhaust the available memory. 我只用一个简单的
for
循环来做到这一点,这就是为什么它这么慢(向量化将不容易进行的原因,因为它需要一个包含大量元素的矩阵以耗尽可用内存。
I am working with sound vectors that have ~600,000
elements, so the matrix would need ~3*10^11
elements, which is even too big for a sparse matrix on my 32 bit computer. 我正在使用具有
~600,000
元素的声音矢量,因此矩阵将需要~3*10^11
元素,对于我的32位计算机上的稀疏矩阵而言,这甚至太大了。
function smoothsig=smoothnoise(sig,wins,method)
% Smooths a signal "sig", leaving hi power regions intact (mostly).
% "wins" is a window size for smoothing
% "method" determines the relative smoothing of various parts of the spectrum
% "method" can be a handle to any function <=1 on [0,1]
mx=max(abs(sig)); mn=min(abs(sig)); range=mx-mn;
len=(size(sig)); len=len(1);
if ~(exist('method','var'))
method=1;
end
winsize=round(wins* winfunction((abs(sig)-mn)/range,method) );
smoothsig=zeros(size(sig));
for k=1:wins
winsz=min(k-1,winsize(k));
smoothsig(k)=mean(sig(k-winsz:k+winsz));
end
for k=wins+1:len-wins
winsz=winsize(k);
smoothsig(k)=mean(sig(k-winsz:k+winsz));
end
for k=len+1-wins:len
winsz=min(len-k,winsize(k));
smoothsig(k)=mean(sig(k-winsz:k+winsz));
end
end
%-------------------------------------------------------------
function y=winfunction(x, method)
% Determines the window function
if isa(method, 'function_handle')
y=method(x);
elseif isscalar(method) & method==1 % Uniform window size
y=ones(size(x));
elseif isvector(method) & method(1)==2 % Sharp cutoff for window size
y=x<method(2);
elseif isvector(method) & method(1)==3 % Polynomial change
y=(1-x).^method(2);
end
end
I'm not very familiar with C++, so I would prefer to do this in a more automated way than re-coding by hand in another language, if possible. 我对C ++不太熟悉,因此,如果可能的话,我宁愿以一种更加自动化的方式进行操作,而不是用另一种语言进行手动重新编码。
I had not hear about .oct
files, so I'll investigate that, but also see the above remark (I'm not fluent with C++). 我没有听说过
.oct
文件,所以我将对此进行调查,但还会看到上面的备注(我不太懂C ++)。
如果您试图从MATLAB代码自动生成C代码,然后将其编译为.mex文件以提高速度,则此功能现在存在于MATLAB Coder产品中,而不是MATLAB Compiler中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.