summaryrefslogtreecommitdiffstats
path: root/Master/Modellbildung_und_Simulation/Aufgabenblatt1/Quantization/src/Quantization.cpp
blob: 919822373499c11cee4830c02ee9f3eb7a145a73 (plain)
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
/*
 * Quantization.cpp
 *
 *  Created on: 02.04.2011
 *      Author: sven
 */

#ifndef QUANTIZATION_H_
	#include "Quantization.h"
#endif

#ifndef _GLIBCXX_FSTREAM
	#include <fstream>
#endif

#ifndef _GLIBCXX_NUMERIC_LIMITS
	#include <limits>
#endif

#ifndef _GLIBCXX_CMATH
	#include <cmath>
#endif

#ifndef _GLIBCXX_IOSTREAM
	#include <iostream>
#endif

Quantization::Quantization(QuantizationParameters& params)
:mParams(params)
{
	calcQuantizationValues();
	std::ifstream inFile;
	std::ofstream outFile;
	inFile.open(mParams.getInputFile().c_str(), std::ios::in | std::ios::binary );
	outFile.open(mParams.getOutputFile().c_str(), std::ios::out | std::ios::binary);
	int dataLenOffset = 0x28;
	char header[dataLenOffset];
	inFile.read(&header[0],dataLenOffset);
	outFile.write(&header[0],dataLenOffset);
	int numSamples = 0;
	inFile.read((char*) &numSamples,sizeof(numSamples));
	outFile.write((char*) &numSamples,sizeof(numSamples));
	short data;
	short quantizationized;
//	unsigned counter = 0;
	for (int i=0; i < numSamples;) {
		inFile.read((char*) &data, sizeof(data) );
		quantizationized = getQuantizationValue(data);
		outFile.write((char*) &quantizationized, sizeof(quantizationized) );
//		std::cout << counter++ << " " << quantizationized << std::endl;
		i += sizeof(short);
	}
	inFile.close();
	outFile.close();
	std::cout << "quantization done" << std::endl;
}

Quantization::~Quantization()
{
	QuantizationRangesIter it = mQuantizationRanges.begin();
	for ( ;it < mQuantizationRanges.end(); )
	{
		delete *it++;
	}
	mQuantizationRanges.clear();
}

short Quantization::getQuantizationValue(short val)
{
	// shift to unsigned posivitives values
	unsigned short shiftedVal = val - std::numeric_limits<short>::min();
	QuantizationRangesIter it = mQuantizationRanges.begin();
	for(; it < mQuantizationRanges.end(); it++) {
		QuantizationRange* quantRangePtr = *it;
		if (quantRangePtr->IsInRange(shiftedVal)) {
			// shift back
			return quantRangePtr->mRangeValue + std::numeric_limits<short>::min();
		}
	}
	std::cout << val << " " << shiftedVal << " not quantizationized" << std::endl;
	return -1;
}

void Quantization::calcQuantizationValues()
{
	mNumQuantizationValues = 1 << mParams.getBits();
//	std::cout << mNumQuantizationValues << " quantization values" << std::endl;
	unsigned short valueRangeWidth = std::numeric_limits<unsigned short>::max() / mNumQuantizationValues;
//	std::cout << "value range width " << valueRangeWidth << std::endl;
	unsigned short rangeMiddle = valueRangeWidth / 2;
//	std::cout << "value range middle " << rangeMiddle << std::endl;
	QuantizationRange* quantRangePtr = NULL;
	for (unsigned i=0; i<mNumQuantizationValues; i++) {
		quantRangePtr = new QuantizationRange;
		quantRangePtr->mMinValue = i * valueRangeWidth;
		quantRangePtr->mMaxValue = ((i+1) * valueRangeWidth) - 1;
		quantRangePtr->mRangeValue = quantRangePtr->mMinValue + rangeMiddle;
		quantRangePtr->mRangeWidth = valueRangeWidth;
//		std::cout << "quantization value "<< (i+1) << ": " << quantRangePtr->mRangeValue << std::endl;
//		std::cout << "Range: "<< quantRangePtr->mMinValue << " - " << quantRangePtr->mMaxValue << std::endl;
		mQuantizationRanges.push_back(quantRangePtr);
	}
}