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
155
156
157
158
|
/*
* WaveAnalyzer.cpp
*
* Created on: 02.04.2011
* Author: sven
*/
#ifndef WAVEANALYZER_H_
#include "WaveAnalyzer.h"
#endif
#ifndef _GLIBCXX_FSTREAM
#include <fstream>
#endif
#ifndef _GLIBCXX_IOSTREAM
#include <iostream>
#endif
#ifndef _GLIBCXX_CMATH
#include <cmath>
#endif
#ifndef _GLIBCXX_MAP
#include <map>
#endif
#ifndef _GLIBCXX_NUMERIC_LIMITS
#include <limits>
#endif
WaveAnalyzer::WaveAnalyzer() {
// TODO Auto-generated constructor stub
}
WaveAnalyzer::~WaveAnalyzer() {
// TODO Auto-generated destructor stub
}
long double WaveAnalyzer::calcSNR(std::string& fullQuantFilename, std::string& lowQuantFilename)
{
long double result = 0.0;
double sumXnSquare = 0.0;
double sumDiffSquare = 0.0;
std::ifstream fullQuantFile;
std::ifstream lowQuantFile;
fullQuantFile.open(fullQuantFilename.c_str(), std::ios::in|std::ios::binary);
lowQuantFile.open(lowQuantFilename.c_str(), std::ios::in|std::ios::binary);
int dataLenOffset = 0x28;
fullQuantFile.seekg(dataLenOffset,std::ios_base::cur);
int numSamples = 0;
fullQuantFile.read((char*) &numSamples, sizeof(numSamples));
lowQuantFile.seekg(dataLenOffset+sizeof(numSamples),std::ios_base::cur);
double fullVal = 0.0;
double lowVal = 0.0;
short data = 0;
int diff = 0;
for(int i=0; i < numSamples; ) {
fullQuantFile.read((char*) &data, sizeof(data));
fullVal = (double)(data);
//std::cout << "full: " << data << " "<< fullVal << std::endl;
lowQuantFile.read((char*) &data, sizeof(data));
lowVal = (double)(data);
//std::cout << "low: " << data << " "<< lowVal << std::endl;
sumXnSquare += (fullVal * fullVal);
//std::cout << "Sum Xn Square: " << sumXnSquare << std::endl;
diff = lowVal - fullVal;
sumDiffSquare += (diff * diff);
//std::cout << "Sum Diff Square: " << sumDiffSquare << std::endl;
i += sizeof(data);
}
if (sumDiffSquare != 0.0) {
result = 10.0 * std::log10(sumXnSquare / sumDiffSquare);
} else {
result = std::numeric_limits<long double>::infinity();
}
fullQuantFile.close();
lowQuantFile.close();
return result;
}
long double WaveAnalyzer::calcArithAvg(std::string& wavefilename)
{
long double result = 0.0;
int sum = 0;
std::ifstream waveFile;
waveFile.open(wavefilename.c_str(), std::ios::in|std::ios::binary);
int dataLenOffset = 0x28;
waveFile.seekg(dataLenOffset,std::ios_base::cur);
int numSamples = 0;
waveFile.read((char*) &numSamples, sizeof(numSamples));
short data = 0;
for(int i=0 ; i < numSamples; ) {
waveFile.read((char*) &data, sizeof(data));
sum += data;
i+=sizeof(data);
}
result = ((long double) sum)/((long double)numSamples);
waveFile.close();
return result;
}
long double WaveAnalyzer::calcVariance(std::string& wavefilename)
{
long double variance = 0.0;
std::map<short,statVal*> statMap;
std::ifstream waveFile;
waveFile.open(wavefilename.c_str(), std::ios::in|std::ios::binary);
UInt32 dataLenOffset = 0x28;
waveFile.seekg(dataLenOffset,std::ios_base::cur);
UInt32 numBytes = 0;
waveFile.read((char*) &numBytes, sizeof(numBytes));
UInt32 numSamples = numBytes / sizeof(short);
for(UInt32 i=0 ; i < numSamples; i++) {
short data = 0;
waveFile.read((char*) &data, sizeof(data));
std::map<short,statVal*>::iterator findIt = statMap.find(data);
if (findIt == statMap.end()) {
statVal* vPtr = new statVal;
vPtr->count = 1UL;
vPtr->p = 0.0;
statMap.insert(std::make_pair(data,vPtr));
} else {
findIt->second->count++;
}
}
waveFile.close();
long double expectation = 0.0;
std::map<short,statVal*>::iterator it;
long double pSum = 0.0;
for(it = statMap.begin() ; it != statMap.end(); it++)
{
long double x = (long double) it->first;
it->second->p = (long double) it->second->count / (long double) numSamples;
long double actExp = (x * it->second->p);
expectation += actExp;
pSum += it->second->p;
}
std::cout << "expectation: " << expectation << " pSum: " << pSum << std::endl;
for(it = statMap.begin() ; it != statMap.end(); it++)
{
long double x = (long double) it->first;
long double diff = x - expectation;
long double diffQuad = (diff*diff);
variance += diffQuad * it->second->p;
}
for (it = statMap.begin() ; it != statMap.end();) {
delete it->second;
it++;
}
statMap.clear();
return variance;
}
long double WaveAnalyzer::calcStdDeviation(std::string& wavefilename)
{
return std::sqrt(calcVariance(wavefilename));
}
|