pmlpp/mlpp/transforms/transforms.cpp

59 lines
1.3 KiB
C++
Raw Normal View History

//
// Transforms.cpp
//
// Created by Marc Melikyan on 11/13/20.
//
2023-01-24 18:12:23 +01:00
#include "transforms.h"
#include "../lin_alg/lin_alg.h"
2023-01-24 19:00:54 +01:00
#include <cmath>
#include <iostream>
#include <string>
2023-04-22 17:17:58 +02:00
/*
2023-01-24 19:00:54 +01:00
// DCT ii.
// https://www.mathworks.com/help/images/discrete-cosine-transform.html
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> MLPPTransforms::discreteCosineTransform(std::vector<std::vector<real_t>> A) {
2023-01-25 00:29:02 +01:00
MLPPLinAlg alg;
2023-01-24 19:00:54 +01:00
A = alg.scalarAdd(-128, A); // Center around 0.
2023-01-27 13:01:16 +01:00
std::vector<std::vector<real_t>> B;
2023-01-24 19:00:54 +01:00
B.resize(A.size());
2023-02-12 15:16:14 +01:00
for (uint32_t i = 0; i < B.size(); i++) {
2023-01-24 19:00:54 +01:00
B[i].resize(A[i].size());
}
int M = A.size();
2023-02-12 15:16:14 +01:00
for (uint32_t i = 0; i < B.size(); i++) {
for (uint32_t j = 0; j < B[i].size(); j++) {
2023-01-27 13:01:16 +01:00
real_t sum = 0;
real_t alphaI;
2023-01-24 19:00:54 +01:00
if (i == 0) {
alphaI = 1 / std::sqrt(M);
} else {
2023-01-27 13:01:16 +01:00
alphaI = std::sqrt(real_t(2) / real_t(M));
2023-01-24 19:00:54 +01:00
}
2023-01-27 13:01:16 +01:00
real_t alphaJ;
2023-01-24 19:00:54 +01:00
if (j == 0) {
alphaJ = 1 / std::sqrt(M);
} else {
2023-01-27 13:01:16 +01:00
alphaJ = std::sqrt(real_t(2) / real_t(M));
2023-01-24 19:00:54 +01:00
}
2023-02-12 15:16:14 +01:00
for (uint32_t k = 0; k < B.size(); k++) {
for (uint32_t f = 0; f < B[k].size(); f++) {
2023-01-24 19:00:54 +01:00
sum += A[k][f] * std::cos((M_PI * i * (2 * k + 1)) / (2 * M)) * std::cos((M_PI * j * (2 * f + 1)) / (2 * M));
}
}
B[i][j] = sum;
B[i][j] *= alphaI * alphaJ;
}
}
return B;
}
2023-04-22 17:17:58 +02:00
*/
void MLPPTransforms::_bind_methods() {
}