我有一些 GNU Octave/Matlab 代码,我想将其转换为 C 或 C++。我可以处理大部分翻译,但我不知道 x1=0:1:pts-1;
行在 C 代码中会翻译成什么。如果我理解正确的话,它是 Octave 中的 Range 类型,但我不确定 C 或 C++ 中的哪种数据类型支持相同的功能。
完整的脚本是:
pkg load signal
fs = 48000;
fc=18300;
rlen=10;
ppiv=100;
beta=9.0;
apof=0.9;
apobeta=0.7;
pts = ppiv*rlen+1;
x1=0:1:pts-1;%this line here!!!!
x2=rlen*2*(x1-(pts-1)/2 +0.00001)/(pts-1); % and the the usage of x1 in this line
x3=pi*fc/fs*x2;
h=sin(x3)./x3;
w=kaiser(pts,beta);
g=w.*h;
aw = 1-apof*kaiser(pts,apobeta);
g=aw.*g;
g=g/max(g);
figure(1);
subplot(1,2,1);
plot(x2/2,g);
axis([-rlen/2 rlen/2 -0.2 2.0002]);
%xlabel(“Time in Sampling Intervals”);
%title(‘Bandlimited Impulse’);
subplot(1,2,2);
zpad=20;
g2=[g;zeros((zpad-1)*pts,1)];
wspec=abs(fft(g2));
wspec=max(wspec/max(wspec),0.00001);
fmax=60000;
rng = round(rlen*zpad*fmax/fs);
xidx = 0:1:rng;
semilogy(fmax/1000*xidx/rng,wspec(1:(rng+1)));
%xlabel(‘Frequency in kHz’);
%title(‘amplitude spectrum’);
grid;
hold;
plot([20 20],[0.00001,1]);
plot([fs/1000-20 fs/1000-20], [0.00001 1]);
plot([fs/1000 fs/1000], [0.00001 1]);
hold off;
所以我正在寻找的是代码片段或如何处理此转换的一些资源。
提前致谢
最佳答案
由于您是从 Octave 代码进行转换,因此使用 Octave 的 C++ 库非常有意义。他们可以看到它的 doxygen docs在线。
对于您的具体情况,您可以使用 octave_range
类:
#include "ov-range.h"
octave_range x (0, pts -1, 1);
请注意,这只是一个范围,就像在 Octave 中一样。如果你想要一个矩阵,你可以这样做:
Matrix mx = x.matrix_value ();
如果这让您感到困惑,请将范围转换为矩阵,看看它在 Octave 中实际上是如何完成的。创建一个范围并检查其在内存中的大小。然后与由此创建的矩阵进行比较:
octave-cli-3.8.1> x = 0:1:10000;
octave-cli-3.8.1> whos x
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
x 1x10001 24 double
Total is 10001 elements using 24 bytes
octave-cli-3.8.1> x = [0:1:10000];
octave-cli-3.8.1> whos x
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
x 1x10001 80008 double
Total is 10001 elements using 80008 bytes
关于c++ - C/C++ 中的 GNU Octave 代码等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23746011/