Skip to content
Snippets Groups Projects
math_lib.c 4.64 KiB
Newer Older
Og's avatar
Og committed
#include "math_lib.h"

#include <math.h>

/**
 * @brief The function f(x) to define
 *
 * @param x The x param for the function
 * @return double return the result of f(x)
 */
double my_function_x(double x)
{
    return -((2 * x - 1) / (exp(pow(x, 2) - x + 2)));
}

/**
 * @brief Intergration of my_function_x
 *
 * @param a the a
 * @param b the b
 * @param N the N
 * @param f the function to use
 *
 * @return double the result of integrations
 */
double interg(double a, double b, double N, double (*f)(double))
{
    double delta_x = ((b - a) / N);
    double res = 0.0;

    for (int32_t i = 0; i < N; i += 1)
    {
        res += (*f)(a + (i * delta_x)) * delta_x;
    }
    return res;
}

/**
 * @brief Compute E(n) with function f(x)
 *
 * @param N The N number
 * @return double return the result of E(N)
 */
double E_n(double N)
{
    return fabs((VALUE_I - interg(VALUE_A, VALUE_B, N, my_function_x)) / VALUE_I);
}

/**
 * @brief Compute the singal s(x)
 *
 * @param x The param x for the signal
 * @return double return the result of s(x)
 */
double s_x(double x, double w_1, double w_2)
{
    return sin(2 * M_PI * w_1 * x) + sin(2 * M_PI * w_2 * x);
}

/**
 * @brief
 *
 * @param x
 * @param psi
 * @return double
 */
double h_x(double x, double psi)
{
    return (1 / sqrt(2 * M_PI * psi)) * exp(-pow(x, 2) / (2 * psi));
}

poulpe's avatar
poulpe committed
/**
 * @brief Normalise the matrix for get more contrast for zone
 * 
 * @param orig The original matrix will be affected
 * @param min The minimum in the matrix
 * @param max The maximum in the matrix
 * @param lumMax The composant max in the matrix in header
 */
void normalise_matrix(matrix *orig, int32_t min, int32_t max, int32_t lumMax)
{
    for (uint32_t i = 1; i < orig->col-1; i += 1)
Og's avatar
Og committed
    {
        for (uint32_t j = 1; j < orig->row-1; j += 1)
Og's avatar
Og committed
        {
            orig->data[i][j] = (orig->data[i][j] - min) * (lumMax / (max - min));
Og's avatar
Og committed
        }
    }
}

poulpe's avatar
poulpe committed
/**
 * @brief Convolve matrix and return the output in matrix conv, The factor is used for divide the result of kernel
 * 
 * @param conv The convolve matrix output
 * @param orig The original matrix to use for the convolve
 * @param kernel The kernel to apply to the matrix
 * @param factor The factor for divide
 */
void convolve_matrix(matrix *conv,matrix * orig, const matrix *const kernel, const int32_t factor)
Og's avatar
Og committed
{
    int32_t kmoin = 0 - kernel->row/2;
    int32_t kplus = kernel->col / 2;

    printf("Orig col : %d\n",orig->col);
    printf("Orig row : %d\n",orig->row);
    printf("Kernplus : %d\n",kplus);
    printf("Kernmoins : %d\n",kmoin);
    printf("Kern row : %d\n",kernel->row);
Og's avatar
Og committed

    for (uint32_t x = 0; x < orig->col; x++)
Og's avatar
Og committed
    {
        for (uint32_t y = 0; y < orig->row; y++)
Og's avatar
Og committed
        {
            for (int32_t s = kmoin; s <= kplus; s++)
Og's avatar
Og committed
            {
                for (int32_t t = kmoin; t <= kplus; t++)
Og's avatar
Og committed
                {
                    if (((x + s) >= orig->col))
Og's avatar
Og committed
                    {
                        continue;
                    }
                    if ( ((y + t) >= orig->row))
Og's avatar
Og committed
                    {
                        continue;
                    }
                    // printf("%d",y);
                    pixels += orig->data[x + s][y + t] * kernel->data[kplus + s][kplus + t];
Og's avatar
Og committed
                }
            }
            if (pixels > 65535)
                pixels = 65535;
            if (pixels < 0)
                pixels = 0;
Og's avatar
Og committed
            pixels = 0;
        }
    }
}
Og's avatar
Og committed

double function_for_trapez(double a, uint32_t N, double (*f)(double), double (*O)(double))
{
    double delta_x = 0.0;
    double res = 0.0;
    for (uint32_t i = 0; i < N; i += 1)
    {
        res += ((*f)(a + i * delta_x) + (*f)(a + (i + 1) * delta_x) / 2) + O(delta_x * delta_x);
    }
    return res;
}

Og's avatar
Og committed

double fun1(double *signal, int index, int N, int len)
{
    double sum = 0.0;
    if ((index + N) < len)
    {
        for (int i = index; i < index + N; i++)
        {
            sum += signal[i];
        }
        return sum/N;
    }
    return 0;
}

double funH(double *signal,int index, int N, int len)
{
    double res = 0.0;
    double pi = 3.14;

    res = (1 / sqrt(2*pi*N))* exp((-signal[index]*signal[index])/2*N);

    return res;
}
poulpe's avatar
poulpe committed

/**
 * @brief Convolve the signal passed in param and return in the out
 * 
 * @param signal The signal to use for convolution
 * @param out The double array alloced for get data after convolve
 * @param len The length of data in signal
 */
Og's avatar
Og committed
void convolution_signal(double *signal, double *out, int len)
{
    for (int i = 0; i < len; i++)
    {
        out[i] = fun1(signal,i,40,len);
        //out[i] = funH(signal,i,40,len);
        //printf("%f\n",out[i]);
    }
}