A friend of mine needs an analogue of MatLAB's betainc function for some statistical calculations in programmable logic devices (PLD's) (I'm not a man of hardware and don't know any details on his project yet).
Therefore using precompiled libraries is not an option.
She needs an implementation in raw C considering that each of the three parameters is variable.
Is there a good one somewhere on the Web?
Thank you so much in advance!
I know I'm late to answering, but your currently accepted answer (using code from "Numerical Recipes") has a terrible license. Also, it doesn't help others that don't already own the book.
Here is raw C99 code for the incomplete beta function released under the Zlib license:
#include <math.h>
#define STOP 1.0e-8
#define TINY 1.0e-30
double incbeta(double a, double b, double x) {
if (x < 0.0 || x > 1.0) return 1.0/0.0;
/*The continued fraction converges nicely for x < (a+1)/(a+b+2)*/
if (x > (a+1.0)/(a+b+2.0)) {
return (1.0-incbeta(b,a,1.0-x)); /*Use the fact that beta is symmetrical.*/
}
/*Find the first part before the continued fraction.*/
const double lbeta_ab = lgamma(a)+lgamma(b)-lgamma(a+b);
const double front = exp(log(x)*a+log(1.0-x)*b-lbeta_ab) / a;
/*Use Lentz's algorithm to evaluate the continued fraction.*/
double f = 1.0, c = 1.0, d = 0.0;
int i, m;
for (i = 0; i <= 200; ++i) {
m = i/2;
double numerator;
if (i == 0) {
numerator = 1.0; /*First numerator is 1.0.*/
} else if (i % 2 == 0) {
numerator = (m*(b-m)*x)/((a+2.0*m-1.0)*(a+2.0*m)); /*Even term.*/
} else {
numerator = -((a+m)*(a+b+m)*x)/((a+2.0*m)*(a+2.0*m+1)); /*Odd term.*/
}
/*Do an iteration of Lentz's algorithm.*/
d = 1.0 + numerator * d;
if (fabs(d) < TINY) d = TINY;
d = 1.0 / d;
c = 1.0 + numerator / c;
if (fabs(c) < TINY) c = TINY;
const double cd = c*d;
f *= cd;
/*Check for stop.*/
if (fabs(1.0-cd) < STOP) {
return front * (f-1.0);
}
}
return 1.0/0.0; /*Needed more loops, did not converge.*/
}
It is taken from this Github repo. There is also a very thorough write-up about how it works here.
Hope you find this helpful.
Or you could read "Numerical Recipes in C" and find complete source. You'll have to worry about licensing issues, but it'll have a lucid explanation of what the function and its implementation are about.
Related
My code is taking a string format and compose it into a buffer (without sprintf, itoa etc).
I'm having trouble converting a float number to string, as I need to have precision of at most 7 digits with no trailing zeros. as well as convert each number in the float variable to char (but in that matter I don't need any help).
I tried several methods, all including math calculations, but nothing has brought me to the desired outcome.
Here's my code so far, but it is messy and also sometimes gives incorrect outcome. I also believe there is a more clean and less-complicated way to do it.
any help will be widely appreciated.
if (*format == 'f') {
float f = *(float*)ptrs[counter];
char str[30];
int b, c, m, k = 7, i = 0, l = 0;
int a = f;
f -= a;
while (k >= 0) {
l = pow(10, k);
m = a / l;
if (m > 0) {
break;
}
k--;
}
printf("%d", k);
for (l = k + 1; l > 0; l--) {
b = pow(10, l - 1);
c = a / b;
str[i++] = c + 48;
a %= b;
}
str[i++] = '.';
for (l = 0; l < 7; l++) {
f *= 10.0;
b = f;
str[i++] = b + 48;
f -= b;
}
for (i = 0; i < 7; i++) {
*buffer = str[i];
buffer++;
}
counter++;
str[i] = '\0';
for example:
input: float v2 =0.9372;
output: .937199
desired output: 0.9372
input: float v2 =0.25000;
output: 1.25000
desired output: 0.25
he's messy and also sometimes gives incorrect outcome
At some point given the base 2 nature of typical floating point, the programmer needs to make choices:
Short code that gets close to the correct answer.
Exact correct conversion with a fair amount of code. e.g. Function to print a double - exactly
Something in between.
With common floating point encoding, there are also issues of
Infinity
Not-a-number.
Oddities like -0.0
And how portable to make the code?
Sources of inexactness
OP's use of int limits float to about [INT_MIN...INT_MAX]. Code fails for float much outside that range. Could use long long to get some more range without a great deal of code change. Better yet research float modff(float value, float *iptr).
float f = ...;
// int a=f;
long long a=f;
Repeated use of f*=10.0 below with a fractional value in f injects a possible rounding (inexactness), at each iteration.
for(l=0;l<7;l++) {
f*=10.0;
Code makes no effort to round given that f may not be 0.0 after the fraction forming for(l=0;l<7;l++) { f*=10.0; loop. I see this as a place to improve precision. This area is also tricky as the round-up may effect many leading digits when they are 9, eventually needing a shift of the string buffer. In the 0.93721, after this loop, f was about 0.74. Since more than 0.5, a rounding would have given the answer of ".9371999" --> ".9372000".
Code aims for 7 decimal digits after the .. Values, as a decimal in code, assigned to a float match to 6 to 9 significant decimal places - which includes digits to the left of .. Expectations should not get too high.
Improvement idea
When the number of fraction digits desired is small, perform 1 scaling/round
// ASSUME f >= 0.0, (sign handling done before here)
long long a=f;
f-=a;
int int_f = round(f*pow(10, 7));
if (int_f < 10000000) {
later_printf_the_7_digits(int_f);
} else {
a++;
later_printf_the_7_digits(0);
}
I'm trying to use the Sine Table lookup method to find the tone frequency at different step size, but when I'm converting the floating point to integer and use the oscicopte to view the frequncy, it can't display any things on screen.
Does anyone know what's the solution for this issues. Any help is apperaite.
Below is the code:
// use the formula: StepSize = 360/(Fs/f) Where Fs is the Sample frequency 44.1 kHz and f is the tone frequency.
// example: StepSize = 360/(44100/440) = 3.576, since the STM32 doesn't support the floating point, therefore, we have to use the fixed-point format which multiply it by 1000 to be 3575
int StepSize = 3575;
unsigned int v=0;
signed int sine_table[91] = {
0x800,0x823,0x847,0x86b,
0x88e,0x8b2,0x8d6,0x8f9,
0x91d,0x940,0x963,0x986,
0x9a9,0x9cc,0x9ef,0xa12,
0xa34,0xa56,0xa78,0xa9a,
0xabc,0xadd,0xaff,0xb20,
0xb40,0xb61,0xb81,0xba1,
0xbc1,0xbe0,0xc00,0xc1e,
0xc3d,0xc5b,0xc79,0xc96,
0xcb3,0xcd0,0xcec,0xd08,
0xd24,0xd3f,0xd5a,0xd74,
0xd8e,0xda8,0xdc1,0xdd9,
0xdf1,0xe09,0xe20,0xe37,
0xe4d,0xe63,0xe78,0xe8d,
0xea1,0xeb5,0xec8,0xedb,
0xeed,0xeff,0xf10,0xf20,
0xf30,0xf40,0xf4e,0xf5d,
0xf6a,0xf77,0xf84,0xf90,
0xf9b,0xfa6,0xfb0,0xfba,
0xfc3,0xfcb,0xfd3,0xfda,
0xfe0,0xfe6,0xfec,0xff0,
0xff4,0xff8,0xffb,0xffd,
0xffe,0xfff,0xfff};
unsigned int sin(int x){
x = x % 360;
if(x <= 90)
return sine_table[x];
else if ( x <= 180){
return sine_table[180 - x];
}else if ( x <= 270){
return 4096 - sine_table[x - 180];
}else{
return 4096 - sine_table[360 - x];
}
}
void main(void)
{
while(1){
v+=StepSize; // Don't know why it doesn't work in this way. not display anything on screen.
DAC->DHR12R2 = sin(v/1000); // DAC channel-2 12-bit Right aligned data
if (v >= 360) v = 0;
}
}
But, if I change the StepSize = 3; it shows the frequency:
There are a few issues with your code. But I will start with the one that you asked about.
int StepSize = 3575;
unsigned int v=0;
while(1){
v+=StepSize;
DAC->DHR12R2 = sin(v/1000);
if (v >= 360) v = 0;
}
The reason why this code doesn't work is that v is always set to 0 at the end of the loop because 3575 is greater than 360. So then you always call sin(3) because 3575/1000 is 3 in integer division.
Perhaps, you should rewrite your last line as if ((v/1000) >= 360) v = 0;. Otherwise, I would rewrite your loop like this
while(1){
v+=StepSize;
v/=1000;
DAC->DHR12R2 = sin(v);
if (v >= 360) v = 0;
}
I would also recommend that you declare your lookup table a const. So it would look like
const signed int sine_table[91] = {
Last recommendation is to choose another name for your sin function so as not to confuse with the sin library function. Even though in this case there shouldn't be a problem.
For example if I have a float/double variable:
float f = 1212512.028423;
double d = 938062.3453;
int f1 = 1;
int d1 = 9;
What's the fastest way to get the first digit of those number?
int first_digit(double n) {
while(n>10) n/=10;
return n;
}
Is this the most efficient?
I need an implementation that doesn't involves char/string, that would be also works (or give the best performance in their specific language). The language are Ruby, Python, C#, Java, C++, Go, JavaScript, PHP.
//Try this.
double d = 938062.3453;
int f1 = Int32.Parse(d.ToString().Substring(0, 1));
Both mentioned solutions will crash at number -0.0123. I have constructed a different method for slightly different purpose:
public static double InteligentRound(double X, int kam=0)
{ int zn = (X < 0) ? (-1) : (1); //signum of the input
X *= zn; //we work with positive values only
double exp = Math.Log10(X); //exponent of 10
exp = Math.Floor(exp);
double B = Math.Pow(10, exp);//the pure power of 10
double FD = X / B; //lies between 1 and 10
if (kam == 0) FD = Math.Round(FD);
if (kam < 0) FD = Math.Floor(FD); //Now, FD is the first digit
if (kam > 0) FD = Math.Ceiling(FD);
return (zn * FD * B);
}
I was going through GSL library. I am pasting the function they used for finding the power of a double number.
double gsl_pow_int(double x, int n)
{
double value = 1.0;
if(n < 0) {
x = 1.0/x;
n = -n;
}
/* repeated squaring method
* returns 0.0^0 = 1.0, so continuous in x
*/
do {
if(n & 1) value *= x; /* for n odd */
n >>= 1;
x *= x;
} while (n);
return value;
}
But wouldn't it be more efficient if they use?
double gsl_pow_int(double x, int n)
{
double value = 1.0;
if(n < 0) {
x = 1.0/x;
n = -n;
}
/* repeated squaring method
* returns 0.0^0 = 1.0, so continuous in x
*/
do{
if(--n)value*=x;
}while(n);
return value;
}
Your code doesn't even properly handle negative powers! How can you claim that your code is optimised.
Also,next,just decreasing space from your program doesn't make your
code more-optimised.Their code has got more readability and more
proper indentation than yours!!! Their code is proper for negative
powers too and much more optimised!
Also,next, bitwise logical operations like & and right shifting >> is considered more efficient than multiplying as what you have done.
I'm attempting to write a general additive synthesis c program that will generate a complex sinusoid created from a sequence of pure sine waves of arbitrary frequency following a single envelope.
The input file will be something of this nature
F0 P0 // a list of up to 100 floats indicating frequencies and
F1 P1 // % contribution of this frequency to the sound
F2 P2
....
-1 // sentinal value to indicate end of frequency list
B0 A0 // first breakpoint
B1 A1
... // There can be an arbitary number of breakpoints
I want my program to generate a WAV file up to the last breakpoint where all the sine waves will be generated at the given frequencies, scaled to the % contribution as listed, and added together to make the final sound
I've attempted to try some of the c programming out but I'm not naturally a C programming so this is what I've done so far:
#include <stdio.h>
#include <stdlib.h>
#include <portsf.h>
#include <math.h>
#ifndef M_PI
#define M_PI (3.141592654)
#endif
// Additive Synthesis
PSF_PROPS props;
props.srate = 44100;
props.chans = 2;
props.samptype = PSF_SAMP_IEEE_FLOAT;
props.format = PSF_STDWAVE;
props.chformat = STDWAVE;
float* frame;
int sampleNumber;
double angleIncrement;
double frequency;
double sampleRate;
int i;
int twopi = 2.0 * M_PI;
ofd = psf_sndCreate(argv[2],&props,0,0,PSF_CREATE_RDWR);
int main (int argc, char* argv[]) {
sampleNumber = 0;
angleIncrement = twopi * frequency/sampleRate;
while (i < sampleNumber) {
frame[0] = frame[1] = sin(sampleNumber * angleIncrement);
sampleNumber++;
if (fwrite(&sampleout,sizeof(float),1,rawfile) !=1) {
printf("Error writing to output file.\n");
return 1;
}
if (i <=1000)
fprintf(bpfile, "%ld\t%f\n", i, frame);
phase += angleIncrement;
if (phase >= twopi)
phase -= twopi;
}
phase_offset = -1 * PI / 2;
sample_buffer = (float*) malloc(samples * sizeof(float));
// sample_buffer set back to 0
memset(sample_buffer, 0, sizeof(float)*sampleNumber);
// go through the number of harmonics
for (i = 1; i <= NHARMS; i++) {
amp = 1.0 / i;
// go through number of sinusoid components
for (n = 0; n < sampleNumber; n++) {
sample_buffer[n] += amp * cos(i * angleIncrement * n + phase_offset);
}
}
}
However, I'm not really sure if I'm doing this right at all. Any ideas on how I can fix this and proceed?
As I've started noticing more and more little details, the comments start getting annoying to read (but make sure you still answer my compiling Hello World question), so I've coalesced them into this answer and will update it as I see more:
as of right now, frequency is used only once, and at that time is only 0.0 ever. is that what you want?
i is initalized to 0, and sampleNumber is also started at 0. Thus the while condition (i < sampleNumber) will never execute
i is never incremented yet you have a if ( i <= 1000 ) condition, which will thus always evaluate true
sampleRate is initialized to 0.0, and the first operation on it is to divide by it, which, obviously, is not happy times
Obviously pedantic, but the n in the bottom most for loop is never declared ( int n; for (n = ....) as an example.
in your sample_buffer = (float*) malloc(samples * sizeof(float)); line, samples should most probably be sampleNumber, right? samples is never defined
I agree with AKA4749, and have one more thing you should do: clip the samples when you add them or you get distortion:
if ( sample[i] > 1 ) sample[i] = 1;
else if ( sample[i] < -1 ) sample[i] = -1;