forked from rikigigi/analisi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convolution.cpp
73 lines (62 loc) · 1.72 KB
/
convolution.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
*
* (c) Riccardo Bertossa, 2019
*
* Use at your own risk.
*
* If you modified the code, I could be happy if you contribute on github!
*
**/
#include "convolution.h"
#include <iostream>
template <typename T> Convolution<T>::Convolution(std::function<double (const double &)> f,int n,double start,double stop,int center)
{
if (n<=0 || center <0 || center >= n) {
std::cerr<< "Errore: richiesta una lunghezza "<<n<<" ed un centro "<<center<<" per la convoluzione!\n";
abort();
}
centro=center;
double norm=0;
n_fc=n;
fc=new T[n];
for (int i=0;i<n;i++){
fc[i]=f(start+(stop-start)*double(i)/double(n-1));
norm+=fc[i];
}
for (int i=0;i<n;i++){
fc[i]/=norm;
#ifdef DEBUG
std::cerr << fc[i]<<"\n";
#endif
}
}
template <typename T> void Convolution<T>::calcola(T *in, T*out, int n, int skip) {
for (unsigned int in_index=0;in_index<n;in_index++) {
out[in_index]=0;
for (unsigned int i=0;i<n_fc;i++ ) {
int idx=in_index- centro+i;
if (idx <0)
idx=-idx;
if (idx>=n)
idx=n-2 - idx%n;
out[in_index]+=in[idx*skip]*fc[i];
}
}
}
template <typename T> void Convolution<T>::calcola(T *in, T*out, int n) {
for (unsigned int in_index=0;in_index<n;in_index++) {
out[in_index]=0;
for (unsigned int i=0;i<n_fc;i++ ) {
int idx=in_index- centro+i;
if (idx <0)
idx=-idx;
if (idx>=n)
idx=n-2 - idx%n;
out[in_index]+=in[idx]*fc[i];
}
}
}
template <typename T> Convolution<T>::~Convolution(){
delete [] fc;
}
template class Convolution<double>;