Add noise to the frame using Polar Method in C - c

i wrote a program who can read the Original YUV-file and add Gaussian noise with mean 0 to the Modified one.
the problem is that i don't know how to implement the Polar function on the main function, when i tried its always generate errors.
anyone have any ideas to solve my problem.
thanks
void polar(double *x1, double *x2)
{
double u, v, q, p;
do {
u = 2.0 * random() - 1;
v = 2.0 * random() - 1;
q = u * u + v * v;
} while (q >= 1.0 || q == 0.0);
p = sqrt(-2 * log(q) / q);
*x1 = u * p;
*x2 = v * p;
}
int main(void)
{
FILE *fp1, *fp2;
int a;
double a1,a2;
fp1= fopen("FOOTBALL_352x288_30_orig_01.yuv","rb");
fp2= fopen("FOOTBALL_352x288_30_orig_02.yuv","wb");
int tab[10]="";
while(!feof(fp1))
{
fread(tab,sizeof(int),1,fp1);
fwrite(tab,sizeof(int),1,fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}

The polar function expects the address of two doubles as inputs. If you declared a1 and a2 to be passed into polar, you can call polar(&a1, &a2). The polar function will have set the a1 and a2 by the time it returns. To check this, try printing these variables before and after your call to the polar function.

Related

returning two variables that have multiple values

I am trying to use a function that will calculate values for h and then input these values of h into an equation that will calculate n. This is what my code currently looks like...
int findN(double xI, double xF) {
double h = 0.1;
int n;
do {
printf_s("%8.5f \n", h);
n = ((xF - xI) / h);
h = h / 10;
printf_s("%6d \n", n);
} while (h >= 0.00001);
return n;
}
I know that this function will only return n currently, but as i am new to this i am unsure as to how to also return all the values of h as well as all the values of n... If someone could assist me and show me how to return all the values for n & h, it would be much appreciated.
Thanks.
Typical approach to return multpile values is using arrays and pass its pointer to function:
int f(double *h) {
h[0] = 1.1;
h[1] = 2.2;
}
int main()
{
// create pointer
double *h;
// initialize it with memory block
h = malloc(2*sizeof(double));
// call the function
f(h);
// show output
printf_s("%8.5f \n", h[0]);
printf_s("%8.5f \n", h[1]);
// release memory block
free(h);
return 0;
}
Also same array may be created without memory allocation. It is more simple but arrays exists only until execution is not leave away from function scope where it declared.
int main()
{
// create array
double h[2];
// call the function
f(h);
// show output
printf_s("%8.5f \n", h[0]);
printf_s("%8.5f \n", h[1]);
return 0;
}
And if you can know count of element only during function call you can allocate array in function and return array by pointer and release array at caller.
double* f() {
// create pointer
double *h;
// some size calculations
int size = 1+1;
// initialize it with memory block
h = malloc(size*sizeof(double));
// fill the array
h[0] = 1.1;
h[1] = 2.2;
// return array by pointer
return h;
}
int main()
{
// create pointer
double *h;
// call the function
h = f();
// show output
printf_s("%8.5f \n", h[0]);
printf_s("%8.5f \n", h[1]);
// release memory block
free(h);
return 0;
}
There are many ways to solve this. Another is to return a struct.
Below, findN() returns one object. It just happens that the object contains two members. This approach is suitable when with small objects. With large objects,other approaches should be considered.
typedef struct {
int n;
double h;
} nh;
nh findN(double xI, double xF) {
nh retval;
retval.h = 0.1;
do {
printf_s("%8.5f\n", retval.h);
retval.n = ((xF - xI) / retval.h);
retval.h = retval.h / 10;
printf_s("%6d\n", retval.n);
} while (retval.h >= 0.00001);
return retval;
}
// usage exanple
nh y;
y = findN(1.23, 4.56);
printf_s("h:%8.5f, n:%6d\n", y.h, y.n);
Read into pointers if you want to learn more. But essentially by sending h as a pointer it will return it's value to main.
#include <stdio.h>
int findN(double xI, double xF, double h[]) {
int i = 0;
int n;
h[i] = 0.1;
do {
i++;
printf_s("%8.5f \n", *h);
n = ((xF - xI) / (*h));
h[i] = h[i-1] / 10;
printf_s("%6d \n", n);
} while (h[i] >= 0.00001);
return n;
}
int main()
{
double h[100];
double xI = 1.0, xF = 1.0;
int n;
n = findN(xI, xF, h);
return 0;
}
Read pointers,you will be able to return as many values you want to return,when calling function through main add &h in actual parameters,it means findN(xI,xF,&h) and in declaring the function findN add double *h in formal parameters,that is int findN(double xI,double xF,double *h)...."meaning of * is -value at address of....meaning of & is address of.This will make changes in h globally in this program as the vale is changing in its address.You can return even more values like this using more variables.This is called returning values indirectly.Vote for my answer if its applicable.
The simplest way to handle this is change the function to accept pointers to variables that will accept the values of n and h. Then the function will dereference those pointers to update the relevant variables in the calling function.
void findN(double xI, double xF, int *ret_n, double *ret_h)
{
...
*ret_n = n;
*ret_h = h;
}
Then you can call your function like this:
int n;
double h;
findN(1.2, 3.4, &n, &h);
This method is fine for a relatively small number of parameters. If the number of parameters gets to be too large, you can instead create a struct containing all of the values to be returned either pass in the address of the struct or just return the struct outright.

Attempting to create vertical motion via kinematics in C

I'm attempting to print the height y as a function of time t.
EDIT: Alright I got the code to work now, thanks to all of you! I appreciate your help!
#include <stdio.h>
int main() {
double t, g = -9.8, v = 20;
double y;
for(t = 0; t < 4.079; t = t + .02){
y = (( v * t) + ( .5 * g * t * t ));
printf("value of y: %f\n", y);
}
return 0;
}
On this line:
printf("value of y: %d\n", y);
%d is for printing integers (int).
You are trying to print a double.
Use %f to print a double.
You will have to re-calculate the value of y every time in your loop:
for(t = 0; t < 4.079; t = t + .02) {
y = ((v * t) + (.5 * g * ( t * t));
printf("value of y: %d\n", y);
}
pow becomes a reserved word/syntax once you use math.h and compile with the -lm flag linking that math library. pow is then a function from math.h so you cannot use pow as a variable where you do double pow(double t, double s);
It is best, if not just for human readability, to simply declare all your variables first. Then at most, when declaring them, assign them a value like you do for g and v.
Consider this modification to your code:
# include <stdio.h>
# include <math.h>
int main ( void )
{
const double gravity = -9.8; /* don't be afraid to use full words */
double v = 20;
double t, s;
double p; /* changed from pow */
double y;
p = pow( t, s ); /* this is t^s consider using a better variable name than the letter p */
/* for equations like this `y=` below, */
/* putting the textbook definition of it just prior and */
/* and explaining what you are trying to do will help immensely */
/* later on when your program gives the wrong answer and you */
/* are scratching your head trying to figure out why */
/* and especially if it is someone else */
y = (v * t) + (0.5 * gravity * pow(t, 2.0) );
for ( t = 0; t < 4.079; t = t + .02)
{
printf("value of whatever: %lf\n", whatever ); /* original code had %d here, must use %lf because your variables are type double */
}
return 0;
}

How to implement user-defined function from c in stan

I am trying to use HMC to do MCMC sampling with a self-defined target function. I had my HMC code, but it doesn't work well. I recently knew "stan" which has a more powerful HMC model. So I want to implement my code with "stan".
I have already coded the target function as well as the derivative function in C. It involves for-loop, and functions needed for Cholesky decomposition and other linear algebras from Lapack. I really don't want to recode it in stan. Is there any way to use my functions directly? I would also appreciate it if anyone can give some clues about how to modify my code, or teach me how to modify "stan" code. The following is part of my code:
void get_V(double phi, double sigmasq, double k, double *neardist,
double *neardistM, int *nearind, double *w, int n, int m, double *V){
double one = 1.0, var;
int i, j, dim ,info, int_one = 1;
double *u, *temp_neardistM;
char UPLO = 'L';
char SIDE = 'L', DIAG = 'N';
char TRANS = 'N', TRANS2 = 'T';
var = (double)(1/k) * sigmasq;
V[0] = var;
for (i = 1; i < n ; i++){
if(i < m) {
dim = i;
}
else {
dim = m;
}
...
// Cholesky decomposition of temp_neardistM
info = -1;
dpotrf_(&UPLO, &dim, temp_neardistM, &dim, &info);
if (info != 0) {free(temp_neardistM); return ;}
...
// get Vi
dtrsm_(&SIDE, &UPLO, &TRANS, &DIAG, &dim, &int_one, &one, temp_neardistM, &dim, u, &dim);
V[i] = (var - sigmasq * (sq_sum(u, dim)) );
...
}
}

Model using Euler method and pointer arithmetic not functioning

I'm new to C, and quite unfamiliar with writing any program larger than a few lines.
I'm trying to write a model for an object in freefall acted upon by gravity and drag. It uses Eulers method to solve two first order differential equations, one for position and one for velocity.
So we have: F = m dv/dt = -mg - k|v|v and dy/dt = v
These are solved by: Vn+1 = Vn - (delta t*(g+(k/m)|Vn|Vn)) and Yn+1 = Yn + (delta t * Vn)
(In this Vn+1 is the n+1th term etc.)
In my program i've tried to have two functions, for position and velocity, which work by passing pointers with Y and V values between them and the main function, and it should then loop until Y=0 and print off the values at each step.
When I run it it comes up with something like this: http://imgur.com/DNHIhHI
Could anyone tell me either what is wrong with this, or if I need to use a different approach completely?
Many Thanks, Code below
#include <stdio.h>
void Velocity(double *ptr, double m, double k, double t);
void Position(double *pst, double *ptr, double t );
int main()
{
double k = 18833.5608;
double t = 0;
double m;
double speed = 0;
double *ptr = &speed;
double y = 1000;
double *pst = &y;
printf("Enter mass of object: \n");
scanf("%f" , &m);
do
{
Velocity( ptr, m, k, t );
printf("Velocity at time %f is: %f\n" , t, speed);
Position( pst, ptr, t);
printf("Position at time %f is: %f\n" , t , y);
t++;
}
while((y>0));
return 0;
}
void Velocity(double *velo, double m, double k, double t)
{
double g = 9.80665;
*velo = *velo - (t*(g+((k/m)*fabs(*velo)**(velo))));
}
void Position(double *Y , double *velo, double t )
{
*Y = *Y+(t*(*velo));
}
When writing programs that do calculations -- in any language, not just C -- try to make the code that does the computation take arguments and return results but not mutate variables. That is, do not write:
void do_calculation( double * result, double x, double y)
{
*result = x + y;
}
...
double r;
do_calculation(&r, 123, 456);
instead write
double do_calculation(double x, double y)
{
return x + y;
}
...
double r = do_calculation(123, 456);
Make sense?
If you want to modify an existing value, again, don't pass it in as a variable to be mutated. Instead of
void do_calculation(double * accumulator, double x, double y)
{
*accumulator = *accumulator + x + y;
}
...
double r = 10;
do_calculation(&r, 123, 456);
instead say
double do_calculation(double original, double x, double y)
{
return original + x + y;
}
...
double r = 10;
r = do_calculation(r, 123, 456);
Now, once you've got your program architected more sensibly, you need to learn how to debug small programs. Some good advice on that subject can be found here:
http://ericlippert.com/2014/03/05/how-to-debug-small-programs/
A misconcept. I believe you're trying to solve the equations by using small increments of time. Nothing wrong with that, just make the time increment as small as possible, and correct the formulas:
#include <stdio.h>
#include <math.h>
void Velocity(double *velocity, double m, double k, double t)
{
double g = 9.80665;
double velo = *(velocity);
velo = velo - (t*(g+((k/m)*abs(velo)*(velo))));
*(velocity)=velo;
}
void Position(double *position , double *velocity, double t )
{
double Y = *(position);
double velo = *(velocity);
Y = Y+(t*(velo));
*(position)=Y;
}
int main()
{
double k = 18833.5608;
double t = 0;
double dt = 0.001; //making a small increment of time
double m=100;
double speed = 0;
double y = 1000;
//printf("Enter mass of object: \n");
//scanf("%f" , &m);
do
{
Velocity( &speed, m, k, dt );
printf("Velocity at time %f is: %f\n" , t, speed);
Position( &y, &speed, dt);
printf("Position at time %f is: %f\n" , t , y);
t+=dt; //increment time by delta t
}
while((y>0));
return 0;
}

Accessing variable by pointer in OpenCL kernel

I am writing a raytracing program in OpenCL and I have a function in my Kernel, Quadratic, that takes in 3 float variables and two pointers to float values.
Function:
bool Quadratic(float A, float B, float C, float *t0, float *t1) {
float discrim = B * B - ( 4.0 * A * C );
if (discrim <= 0.0) return false;
float rootDiscrim = sqrtf(discrim);
float q;
if (B < 0) q = -0.5f * ( B - rootDiscrim);
else q = -0.5f * ( B + rootDiscrim);
*t0 = q / A;
*t1 = C / q;
float temp;
return true;
}
Calling the Function:
float t0;
float t1;
if (Quadratic(A, B, C, &t0, &t1)) c[(i*dimy)+j] = t0;
else c[(i*dimy)+j] = 0.0;
Produces the following error:
pyopencl.RuntimeError: clBuildProgram failed: build program failure -
Build on <pyopencl.Device 'ATI Radeon HD 6750M' on 'Apple' at 0x1021b00>:
Error returned by cvms_element_build_from_source
In trying to work out what the problem was I created the following test function which seems to work:
bool TestFunc(float Y, float *x) {
*x = Y;
return true;
}
float x;
if (TestFunc(50.0, &x)) c[(i*dimy)+j] = x;
As far as I can see both functions have the same types of inputs and outputs, any help would be greatly appreciated.
It turns out the problem was with using sqrtf. Once changed to sqrt it works perfectly.

Resources