0

Possible Duplicate:
C programming division

I am trying to convert values into the Mel Filter but, every time I output the result, it is always "0" even though it is a double. Should it be a float? Here is an article on the equation: http://en.wikipedia.org/wiki/Mel_scale

Here is the functions:

double MFCC::mel_filter(int val)
{
     double tmp;
     tmp = 2595 * log10(1 + val / 700);
     cout << tmp << endl;
     return tmp;
    }

    vector<double> MFCC::mel_frame(int size)
    {
    vector<double> mel_Frame;
    for(int i=1; (i < size); i++)
    {
          this->mel_filter(i);
    }

    return mel_Frame;
    }

In main, I will pass values such as.. (1, 2, 3, 4, 5, 6, 7 ......)

And if "1" was entered then the equation would be:

m = 2595 * log10(1 + 1 / 700) = 1.6

Any help or suggestions?

Community
  • 1
  • 1
Phorce
  • 2,632
  • 13
  • 43
  • 76

4 Answers4

5

Your problem is at

log10(1 + val / 700)

That's using an integer division instead of a real division. Try with

log10(1 + val / 700.0)
K-ballo
  • 80,396
  • 20
  • 159
  • 169
3

The type of val is int. So the expression val / 700 is evaluated with integer arithmetic. The result is zero.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
2

When you use integer number literals, integer arithmetics are used. That means any decimals will be rounded down. Write the constants explicitly as decimal numbers, and double arithmetics will be used:

tmp = 2595.0 * log10(1.0 + val / 700.0);
Philipp
  • 67,764
  • 9
  • 118
  • 153
2

Try

log10(1 + val / (float) 700)
thelatemail
  • 91,185
  • 12
  • 128
  • 188
Andy White
  • 21
  • 2