1

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);
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
user844178
  • 11
  • 2
  • it is really hard to read through all this pointer magic, you'd better post what you have in java and where the problem exactly is. – Denis Tulskiy Jul 14 '11 at 09:40

1 Answers1

2

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.

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
  • Thank you very much. Yes, in fact, I have the equation that defines the process. The problem is, "I am newbie" on Java. I have my doubts on how to convert all this pointers work to Java arrays. Now With your clarification I think I can. – user844178 Jul 16 '11 at 07:56
  • That is good to hear. If you run into any problems with expressing your formula on Java arrays, please tell. – Bernd Elkemann Jul 16 '11 at 11:44