Skip to content
Snippets Groups Projects
Commit 80f1cbdf authored by Vojtech Moravec's avatar Vojtech Moravec
Browse files

Added methods which return codebook indices in quatizers.

parent 562b35ac
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,6 @@ public class DataCompressor {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
OutBitStream bitStream = new OutBitStream(outStream, 3, 64);
bitStream.write(0);
......
......@@ -28,6 +28,19 @@ public class ScalarQuantizer {
return result;
}
public int[] quantizeIntoIndices(short[] data) {
return quantizeIntoIndices(TypeConverter.shortArrayToIntArray(data));
}
public int[] quantizeIntoIndices(int[] data) {
int[] indices = new int[data.length];
for (int i = 0; i < data.length; i++) {
final int index = quantizeIndex(data[i]);
indices[i] = index;
}
return indices;
}
private void calculateBoundaryPoints() {
boundaryPoints[0] = min;
boundaryPoints[centroids.length] = max;
......@@ -36,15 +49,19 @@ public class ScalarQuantizer {
}
}
public int quantize(final int value) {
public int quantizeIndex(final int value) {
for (int intervalId = 1; intervalId <= centroids.length; intervalId++) {
if ((value >= boundaryPoints[intervalId - 1]) && (value <= boundaryPoints[intervalId])) {
return centroids[intervalId - 1];
return (intervalId - 1);
}
}
throw new RuntimeException("Value couldn't be quantized!");
}
public int quantize(final int value) {
return centroids[quantizeIndex(value)];
}
public double getMse(final int[] data) {
double mse = 0.0;
for (int i = 0; i < data.length; i++) {
......
......@@ -40,6 +40,29 @@ public class VectorQuantizer {
return result;
}
public int[] quantizeIntoIndices(final int[][] dataVectors) {
assert (dataVectors.length > 0 && dataVectors[0].length % vectorSize == 0) : "Wrong vector size";
int[] indices = new int[dataVectors.length];
for (int vectorIndex = 0; vectorIndex < dataVectors.length; vectorIndex++) {
indices[vectorIndex] = findClosestCodebookEntryIndex(dataVectors[vectorIndex],
VectorDistanceMetric.Euclidean);
}
return indices;
}
public int[] quantizeIntoIndices(short[][] dataVectors) {
assert (dataVectors.length > 0 && dataVectors[0].length % vectorSize == 0) : "Wrong vector size";
int[] indices = new int[dataVectors.length];
for (int vectorIndex = 0; vectorIndex < dataVectors.length; vectorIndex++) {
indices[vectorIndex] =
findClosestCodebookEntryIndex(TypeConverter.shortArrayToIntArray(dataVectors[vectorIndex]),
VectorDistanceMetric.Euclidean);
}
return indices;
}
public static double distanceBetweenVectors(final int[] originalDataVector,
final int[] codebookEntry,
final VectorDistanceMetric metric) {
......@@ -79,17 +102,21 @@ public class VectorQuantizer {
}
private CodebookEntry findClosestCodebookEntry(final int[] dataVector, final VectorDistanceMetric metric) {
return codebook[findClosestCodebookEntryIndex(dataVector, metric)];
}
private int findClosestCodebookEntryIndex(final int[] dataVector, final VectorDistanceMetric metric) {
double minDist = Double.MAX_VALUE;
CodebookEntry closestEntry = null;
for (CodebookEntry codebookEntry : codebook) {
final double dist = distanceBetweenVectors(dataVector, codebookEntry.getVector(), metric);
int closestEntryIndex = 0;
final int codebookSize = codebook.length;
for (int i = 0; i < codebookSize; i++) {
final double dist = distanceBetweenVectors(dataVector, codebook[i].getVector(), metric);
if (dist < minDist) {
minDist = dist;
closestEntry = codebookEntry;
closestEntryIndex = i;
}
}
assert (closestEntry != null) : "Closest entry wasn't found";
return closestEntry;
return closestEntryIndex;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment