ANSI C 指向 java 的指针

发布于 2024-11-24 07:41:05 字数 4174 浏览 2 评论 0原文

从几周前开始,我一直在尝试将这个 c 和其他 c 函数转换为 java 语言(我是它的新手)。 我的第一个问题是如何转换为java代码,像这样的行上的指针:

q = fdata + ind;
datend = fdata + buff_size;

例如,当“ind”具有负位置(例如-220)时,指针将位于“fdata”值之前的“ind”位置,也datend var 也会发生同样的情况。

我发现可以通过为每个指针创建一个索引变量来了解其指向的位置,以孤立的方式解决这个问题。对我来说真正的问题是在那之后的几行,当我试图不越过“fdata”数组的帧末尾时。

请有更多经验的人帮助我吗? 感谢。

static Stat *stat = NULL;
static float *mem = NULL;

static Stat*
get_stationarity(fdata, freq, buff_size, nframes, frame_step, first_time)
float *fdata;
double freq;
int buff_size, nframes, frame_step, first_time;
{
    static int nframes_old = 0, memsize;
    float preemp = 0.4f, stab = 30.0f;
    float *p, *q, *r, *datend;
    int ind, i, j, m, size, order, agap, w_type = 3;

    agap = (int) (STAT_AINT * freq);
    size = (int) (STAT_WSIZE * freq);
    ind = (agap - size) / 2;

    if (nframes_old < nframes || !stat || first_time) {
        /* move this to init_dp_f0() later */
        nframes_old = nframes;
        if (stat) {
            ckfree((char *) stat->stat);
            ckfree((char *) stat->rms);
            ckfree((char *) stat->rms_ratio);
            ckfree((char *) stat);
        }
        if (mem) ckfree((void *) mem);
        stat = (Stat *) ckalloc(sizeof (Stat));
        stat->stat = (float*) ckalloc(sizeof (float) *nframes);
        stat->rms = (float*) ckalloc(sizeof (float) *nframes);
        stat->rms_ratio = (float*) ckalloc(sizeof (float) *nframes);
        memsize = (int) (STAT_WSIZE * freq) + (int) (STAT_AINT * freq);
        mem = (float *) ckalloc(sizeof (float) * memsize);
        for (j = 0; j < memsize; j++) mem[j] = 0;
    }

    if (nframes == 0) return (stat);

    q = fdata + ind;
    datend = fdata + buff_size;

    if ((order = (int) (2.0 + (freq / 1000.0))) > BIGSORD) {
        fprintf(stderr,
                "exceeds that allowable (%d); reduce Fs\n", BIGSORD);
        order = BIGSORD;
    }

    /* prepare for the first frame */
    for (j = memsize / 2, i = 0; j < memsize; j++, i++) mem[j] = fdata[i];

    /* do not run over end of frame */

    for (j = 0, p = q - agap; j < nframes; j++, p += frame_step, q += frame_step) {
        if ((p >= fdata) && (q >= fdata) && (q + size <= datend))
            stat->stat[j] = get_similarity(order, size, p, q,
                &(stat->rms[j]),
                &(stat->rms_ratio[j]), preemp,
                stab, w_type, 0);
        else {
            if (first_time) {
                if ((p < fdata) && (q >= fdata) && (q + size <= datend))
                    stat->stat[j] = get_similarity(order, size, NULL, q,
                        &(stat->rms[j]),
                        &(stat->rms_ratio[j]),
                        preemp, stab, w_type, 1);
                else {
                    stat->rms[j] = 0.0;
                    stat->stat[j] = 0.01f * 0.2f; /* a big transition */
                    stat->rms_ratio[j] = 1.0; /* no amplitude change */
                }
            } else {
                if ((p < fdata) && (q + size <= datend)) {
                    stat->stat[j] = get_similarity(order, size, mem,
                            mem + (memsize / 2) + ind,
                            &(stat->rms[j]),
                            &(stat->rms_ratio[j]),
                            preemp, stab, w_type, 0);
                    /* prepare for the next frame_step if needed */
                    if (p + frame_step < fdata) {
                        for (m = 0; m < (memsize - frame_step); m++)
                            mem[m] = mem[m + frame_step];
                        r = q + size;
                        for (m = 0; m < frame_step; m++)
                            mem[memsize - frame_step + m] = *r++;
                    }
                }
            }
        }
    }

    /* last frame, prepare for next call */
    for (j = (memsize / 2) - 1, p = fdata + (nframes * frame_step) - 1; j >= 0 && p >= fdata; j--)
        mem[j] = *p--;
    return (stat);
}

Since some weeks ago I have been trying to convert this c and other c function to java language (i'm a newbie on it).
My first problem is how-to convert to java code, the pointers on lines like:

q = fdata + ind;
datend = fdata + buff_size;

For example when "ind" has a negative position (-220 for example) the pointer will be on "ind" position before "fdata" values, also the same occur with datend var.

I figured out that can be solved on isolated way by creating an index var for each pointer to know where there are pointing at. The real problem for me comes a few line after that, when i trying to not run over end of frame of "fdata" array.

Can some body with more experiences help me please?
Thank.

static Stat *stat = NULL;
static float *mem = NULL;

static Stat*
get_stationarity(fdata, freq, buff_size, nframes, frame_step, first_time)
float *fdata;
double freq;
int buff_size, nframes, frame_step, first_time;
{
    static int nframes_old = 0, memsize;
    float preemp = 0.4f, stab = 30.0f;
    float *p, *q, *r, *datend;
    int ind, i, j, m, size, order, agap, w_type = 3;

    agap = (int) (STAT_AINT * freq);
    size = (int) (STAT_WSIZE * freq);
    ind = (agap - size) / 2;

    if (nframes_old < nframes || !stat || first_time) {
        /* move this to init_dp_f0() later */
        nframes_old = nframes;
        if (stat) {
            ckfree((char *) stat->stat);
            ckfree((char *) stat->rms);
            ckfree((char *) stat->rms_ratio);
            ckfree((char *) stat);
        }
        if (mem) ckfree((void *) mem);
        stat = (Stat *) ckalloc(sizeof (Stat));
        stat->stat = (float*) ckalloc(sizeof (float) *nframes);
        stat->rms = (float*) ckalloc(sizeof (float) *nframes);
        stat->rms_ratio = (float*) ckalloc(sizeof (float) *nframes);
        memsize = (int) (STAT_WSIZE * freq) + (int) (STAT_AINT * freq);
        mem = (float *) ckalloc(sizeof (float) * memsize);
        for (j = 0; j < memsize; j++) mem[j] = 0;
    }

    if (nframes == 0) return (stat);

    q = fdata + ind;
    datend = fdata + buff_size;

    if ((order = (int) (2.0 + (freq / 1000.0))) > BIGSORD) {
        fprintf(stderr,
                "exceeds that allowable (%d); reduce Fs\n", BIGSORD);
        order = BIGSORD;
    }

    /* prepare for the first frame */
    for (j = memsize / 2, i = 0; j < memsize; j++, i++) mem[j] = fdata[i];

    /* do not run over end of frame */

    for (j = 0, p = q - agap; j < nframes; j++, p += frame_step, q += frame_step) {
        if ((p >= fdata) && (q >= fdata) && (q + size <= datend))
            stat->stat[j] = get_similarity(order, size, p, q,
                &(stat->rms[j]),
                &(stat->rms_ratio[j]), preemp,
                stab, w_type, 0);
        else {
            if (first_time) {
                if ((p < fdata) && (q >= fdata) && (q + size <= datend))
                    stat->stat[j] = get_similarity(order, size, NULL, q,
                        &(stat->rms[j]),
                        &(stat->rms_ratio[j]),
                        preemp, stab, w_type, 1);
                else {
                    stat->rms[j] = 0.0;
                    stat->stat[j] = 0.01f * 0.2f; /* a big transition */
                    stat->rms_ratio[j] = 1.0; /* no amplitude change */
                }
            } else {
                if ((p < fdata) && (q + size <= datend)) {
                    stat->stat[j] = get_similarity(order, size, mem,
                            mem + (memsize / 2) + ind,
                            &(stat->rms[j]),
                            &(stat->rms_ratio[j]),
                            preemp, stab, w_type, 0);
                    /* prepare for the next frame_step if needed */
                    if (p + frame_step < fdata) {
                        for (m = 0; m < (memsize - frame_step); m++)
                            mem[m] = mem[m + frame_step];
                        r = q + size;
                        for (m = 0; m < frame_step; m++)
                            mem[memsize - frame_step + m] = *r++;
                    }
                }
            }
        }
    }

    /* last frame, prepare for next call */
    for (j = (memsize / 2) - 1, p = fdata + (nframes * frame_step) - 1; j >= 0 && p >= fdata; j--)
        mem[j] = *p--;
    return (stat);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深陷 2024-12-01 07:41:09

重写此代码比移植更容易。原因是它使用了大量的指针运算和转换。

在我看来,这段代码结合了滑动窗口和数据平均函数。您可以在 Java 中轻松完成此操作,只需将每个时间序列放入数组中,使用索引(而不是指针)指向数组的条目。在 C 代码使用指针,然后使用(可能为负)偏移量作为第二个指针的情况下,只需使用时间序列数组中的两个索引。

如果您有此代码应该计算的公式(数学符号。滑动窗口加上平均函数),并将该公式转换为 java,那么执行此操作会更容易。

This code is easier rewritten than ported. The reason is, that it uses a large number of pointer-arithmetic and casting.

It looks to me like this code combines a sliding window and averaging functions over the data. You can easily do this in Java, just place each time-series in an array, use an index (instead of a pointer) to point at the array's entries. In the case where the C-code uses a pointer and then a (possibly negative) offset as a second pointer just use two indices into the time-series-array.

It is easier to do this if you have the formula (math-notation. sliding-window plus averaging functions) that this code is supposed to compute and translate the formula to java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文