-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnf.h
154 lines (134 loc) · 4.06 KB
/
dnf.h
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef _DNF_H
#define _DNF_H
#include "dnf/Neuron.h"
#include "dnf/Layer.h"
#include "dnf/Net.h"
/**
* Main Deep Neuronal Network main class.
* It's designed to be as simple as possible with
* only a few parameters as possible.
**/
class DNF {
public:
/**
* Constructor which sets up the delay lines, network layers
* and also calculates the number of neurons per layer so
* that the final layer always just has one neuron.
* \param NLAYERS Number of layers
* \param numTaps Number of taps for the delay line feeding into the 1st layer
* \param fs Sampling rate of the signals used in Hz.
* \param am The activation function for the neurons. Default is tanh.
**/
DNF(const int NLAYERS,
const int numTaps,
double fs,
Neuron::actMethod am = Neuron::Act_Tanh) : noiseDelayLineLength(numTaps),
signalDelayLineLength(noiseDelayLineLength / 2),
signal_delayLine(signalDelayLineLength),
nNeurons(new int[NLAYERS]),
noise_delayLine(new int[noiseDelayLineLength]) {
// calc an exp reduction of the numbers always reaching 1
double b = exp(log(noiseDelayLineLength)/(NLAYERS-1));
for(int i=0;i<NLAYERS;i++) {
nNeurons[i] = ceil(noiseDelayLineLength / pow(b,i));
if (i == (NLAYERS-1)) nNeurons[i] = 1;
}
//create the neural network
NNO = new Net(NLAYERS, nNeurons, noiseDelayLineLength, 0, "");
//setting up the neural networks
for(int i=0;i<NLAYERS;i++) {
NNO->getLayer(i)->initLayer(i,Neuron::W_RANDOM, Neuron::B_NONE, am);
fprintf(stderr,"Layer %d has %d neurons. act = %d\n",i,nNeurons[i],am);
}
}
/**
* Realtime sample by sample filtering operation
* \param signal The signal contaminated with noise. Should be less than one.
* \param noise The reference noise. Should be less than one.
* \returns The filtered signal where the noise has been removed by the DNF.
**/
int filter(int signal, int noise) {
signal_delayLine.push_back(signal);
const int delayed_signal = signal_delayLine[0];
for (int i = noiseDelayLineLength-1 ; i > 0; i--) {
noise_delayLine[i] = noise_delayLine[i-1];
}
noise_delayLine[0] = noise / noiseDelayLineLength;
// NOISE INPUT TO NETWORK
// using std::chrono::high_resolution_clock;
// using std::chrono::duration_cast;
// using std::chrono::duration;
// using std::chrono::microseconds;
// auto t1 = high_resolution_clock::now();
NNO->setInputs(noise_delayLine);
NNO->propInputs();
// REMOVER OUTPUT FROM NETWORK
remover = NNO->getOutput(0);
f_nn = delayed_signal - remover;
// FEEDBACK TO THE NETWORK
NNO->setError(f_nn);
NNO->propErrorBackward();
NNO->updateWeights();
// auto t2 = high_resolution_clock::now();
// auto us_int = duration_cast<microseconds>(t2 - t1);
// std::cout << us_int.count() << " ms\n";
return f_nn;
}
/**
* Returns a reference to the whole neural network.
* \returns A reference to Net.
**/
inline Net& getNet() const {
return *NNO;
}
/**
* Returns the length of the delay line which
* delays the signal polluted with noise.
* \returns Number of delay steps in samples.
**/
inline const int getSignalDelaySteps() const {
return signalDelayLineLength;
}
/**
* Returns the delayed with noise polluted signal by the delay
* indicated by getSignalDelaySteps().
* \returns The delayed noise polluted signal sample.
**/
inline const int getDelayedSignal() const {
return signal_delayLine[0];
}
/**
* Returns the remover signal.
* \returns The current remover signal sample.
**/
inline const int getRemover() const {
return remover;
}
/**
* Returns the output of the DNF: the the noise
* free signal.
* \returns The current output of the DNF which is idential to filter().
**/
inline const int getOutput() const {
return f_nn;
}
/**
* Frees the memory used by the DNF.
**/
~DNF() {
delete NNO;
delete[] nNeurons;
delete[] noise_delayLine;
}
private:
int count = 0;
Net *NNO;
int noiseDelayLineLength;
int signalDelayLineLength;
boost::circular_buffer<int> signal_delayLine;
int* noise_delayLine;
int* nNeurons;
int remover = 0;
int f_nn = 0;
};
#endif