Hey, I have a function in C that I am trying to convert over to C++. I have done everything, apart from this line:

``````memcpy(mel[i],&temp[melstart[i]],mellength[i]*sizeof(double));
``````

I need a way of copying the elements in temp[melstart[i]] over to a vector of doubles..

Here is the C code:

``````void Setup_Mel(int fft_size, int sample_rate) {
int i,j,k,tap;
double fmax;
double dphi;
double fsample;
double freq;
double temp[fft_size/2];

fmax=2595*log10(8000.0f/700+1);
dphi = fmax/17;
freq = (double)sample_rate/fft_size;

for (i=0; i<16; i++) {
melstart[i]=fft_size/2;
mellength[i]=0;
memset(temp,0,sizeof(double)*fft_size/2);
for (j=0; j<fft_size/2; j++) {
fsample = 2595*log10(freq*j/700 + 1);

if ((dphi*i <= fsample) && (fsample < dphi*(i+1))) temp[j] = (fsample-dphi*i)/(dphi*(i+1)-dphi*i);
if ((dphi*(i+1) <= fsample) && (fsample < dphi*(i+2))) temp[j] = (fsample-dphi*(i+2))/(dphi*(i+1)-dphi*(i+2));
if ((temp[j] != 0) && (melstart[i] > j)) melstart[i] = j;
if (temp[j] != 0) mellength[i]++;
}

mel[i] = malloc(sizeof(double)*mellength[i]);
memcpy(mel[i],&temp[melstart[i]],mellength[i]*sizeof(double));

printf("%f\n", temp[melstart[i]]);
for(k=0; (k < mellength[i]); k++)
{
//printf("%f\n", mel[i][k]);
}
//              for (k=0; k<mellength[i]; tap++,k++) printf("mel filter: %d, %d, %d, %f, %f\n",i,melstart[i]+k,tap,mel[i][k],(melstart[i]+k)*freq);
}
}
``````

And the C++ function I'm working on:

``````void setUp_Mel(int fft_size, int sample_rate, vector< vector< double > > &mel, int *melstart, int *mellength)
{
double fmax;
double dphi;
double fsample;
double freq;
double temp[fft_size/2];

fmax = 2595*log10(8000.0f/700+1);
dphi = fmax / 17;
freq = (double)sample_rate/fft_size;

for(int i=0; (i < 16); i++)
{
melstart[i]=fft_size/2;
mellength[i]=0;
memset(temp,0,sizeof(double)*fft_size/2);

for(int j=0; (j < fft_size/2); j++)
{
fsample = 2595*log10(freq*j/700 + 1);

if ((dphi*i <= fsample) && (fsample < dphi*(i+1))) temp[j] = (fsample-dphi*i)/(dphi*(i+1)-dphi*i);
if ((dphi*(i+1) <= fsample) && (fsample < dphi*(i+2))) temp[j] = (fsample-dphi*(i+2))/(dphi*(i+1)-dphi*(i+2));
if ((temp[j] != 0) && (melstart[i] > j)) melstart[i] = j;
if (temp[j] != 0) mellength[i]++;

}

mel[i].resize(mellength[i]);

mel[i].assign(temp[melstart[0]], temp[melstart[0]] + mellength[i]);

for(int k=0; (k < mellength[i]); k++)
{
cout << mel[i][k] << endl;
}
//mel[i].assign(temp[melstart[i]], temp[melstart[i]] + mellength[i]);
//cout << temp[melstart[i]] << endl;
}
}
``````

This throws up an error. Sorry there is a lot of code, but, the problem should be simple.. I just cannot figure it out. Thanks guys

Shouldn't it be `melstart[i]` and not `melstart[0]`?

Also, you should use the `reserve()` function instead of resize if you are going to use `assign()` after, that will avoid the default-construction of all the objects that resize() would trigger. So, I guess this should work:

All 3 Replies

Shouldn't it be `melstart[i]` and not `melstart[0]`?

Also, you should use the `reserve()` function instead of resize if you are going to use `assign()` after, that will avoid the default-construction of all the objects that resize() would trigger. So, I guess this should work:

``````      mel[i].reserve(mellength[i]);
mel[i].assign(temp[melstart[i]], temp[melstart[i]] + mellength[i]);
``````

Also, I don't think that you need the temporary array `temp`. Why not just use `mel[i]` directly?

No, it didn't work! Gives this error:

MFCCexample.cpp:46: instantiated from here
/usr/include/c++/4.2.1/bits/stl_iterator_base_types.h:129: error: ‘double’ is not a class, struct, or union type

I even tried assigning it directly, I just get a segmentation fault. Any ideas?

Oh yeah, sorry, this should work:

``````mel[i].assign(&temp[melstart[i]], &temp[melstart[i]] + mellength[i]);
``````

notice the `&` to take the address of the values in `temp`.

commented: Great help - Thank you very much :) +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.