summaryrefslogtreecommitdiffstats
path: root/src/synth_math.h
blob: 74dec4c2d27c129c01cb272c72c68ae037c151ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef SYNTH_MATH_H
#define SYNTH_MATH_H

#include <stddef.h>

static long long
gcd(long long a, long long b)
{
   long long rem;
   rem=a-(a/b*b);
   if(rem==0)
      return b;
   else
      gcd(b,rem);
}
 
// Function to return LCM of two numbers
static long long
lcm(long long a, long long b)
{
    return (a / gcd(a, b)) * b;
}

static float
clamp(float f)
{
  if (f <= -1) return -0.9999;
  if (f >= 1)  return  0.9999;
  return f;
}

/* 1d convolution */
static void
convole(float *signal, float *filter,
        size_t signal_size, size_t filter_size, float *out) {
  for (size_t i = 0; i < filter_size + signal_size; i++) {
    size_t kmin, kmax, k;
    out[i] = 0;
    /* find overlap */
    kmin = (i >= filter_size - 1) ? i - (filter_size - 1) : 0;
    kmax = (i < signal_size - 1) ? i : signal_size - 1;

    /* Add the overlaping values */
    for (k = kmin; k <= kmax; k++) {
      out[i] += signal[k] * filter[i - k];
    }
  }
}

#endif /* SYNTH_MATH_H */