This repository has been archived by the owner on Nov 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
signal.cpp
executable file
·88 lines (84 loc) · 1.51 KB
/
signal.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "uspeech.h"
/**
* Constructor
*/
signal::signal(int port){
pin = port;
fconstant = F_CONSTANT;
econstant = 2;
aconstant = 4;
vconstant = 6;
shconstant = 10;
amplificationFactor = 10;
}
/**
* Calibration of background based on averaging
*/
void signal::calibrate(){
#ifdef ARDUINO_ENVIRONMENT > 0
calib = (analogRead(pin)+analogRead(pin)+analogRead(pin)+analogRead(pin))/4; //acquire background noise
#endif
}
/**
* Sampling of the sound: Based on storing values minus average background noise
*/
void signal::sample(){
int i = 0;
while ( i < 32){
#ifdef ARDUINO_ENVIRONMENT > 0
arr[i] = analogRead(pin)-calib;
#endif
i++;
}
}
/**
* An estimate of background noise
*/
unsigned int signal::power(){
unsigned int j = 0;
char i = 0;
while(i<32){
j+=abs(arr[i]);
i++;
}
return j;
}
/**
* power/abs(sum of derivative)
*/
unsigned int signal::complexity(int power){
unsigned int j = 0;
int i = 1;
while(i<32){
j+=abs(arr[i]-arr[i-1]);
i++;
}
//Serial.println(j);
return (j*amplificationFactor)/power;
}
/**
* Point of maximum amplitude
*/
unsigned int signal::maxPower(){
int i =0;
unsigned int max = 0;
while (i<32){
if(max<abs(arr[i])){
max = abs(arr[i]);
maxPos = i;
}
i++;
avgPower+=arr[i];
}
avgPower /= 32;
return max;
}
int signal::snr(int power){
int i =0,j=0;
int mean =power/32;
while(i <32){
j+=sq(arr[i]-mean);
i++;
}
return sqrt(j/mean)/power;
}