Advertisement
Hamikadze

Untitled

Nov 5th, 2020
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define pi 3.142857
  4. const int m = 8, n = 8;
  5.  
  6. int dctTransform(int matrix[][n])
  7. {
  8.     int i, j, k, l;
  9.  
  10.     // dct хранит коэффициенты после дискретно-косинусного преобразования
  11.     float dct[m][n];
  12.  
  13.     float ci, cj, dct1, sum;
  14.  
  15.     //обход слева направо сведху вниз  
  16.     for (i = 0; i < m; i++) {
  17.         for (j = 0; j < n; j++) {
  18.  
  19.             // ci и cj зависят от частоты, а также от индекса строки и столбца матрицы
  20.             if (i == 0)
  21.                 ci = 1 / sqrt(m);
  22.             else
  23.                 ci = sqrt(2) / sqrt(m);
  24.             if (j == 0)
  25.                 cj = 1 / sqrt(n);
  26.             else
  27.                 cj = sqrt(2) / sqrt(n);
  28.  
  29.             // sum временно хранит сумму вычислений косинуса
  30.             sum = 0;
  31.             for (k = 0; k < m; k++) {
  32.                 for (l = 0; l < n; l++) {
  33.                     dct1 = matrix[k][l] *  
  34.                            cos((2 * k + 1) * i * pi / (2 * m)) *  
  35.                            cos((2 * l + 1) * j * pi / (2 * n));
  36.                     sum = sum + dct1;
  37.                 }
  38.             }
  39.  
  40.             dct[i][j] = ci * cj * sum;
  41.         }
  42.     }
  43.  
  44.     //Дальше просто вывод идёт в консольку
  45.     for (i = 0; i < m; i++) {
  46.         for (j = 0; j < n; j++) {
  47.             printf("%f\t", dct[i][j]);
  48.         }
  49.         printf("\n");
  50.     }
  51. }
  52.  
  53. // Ну это как вызывается
  54. int main()
  55. {
  56.     int matrix[m][n] = { { 255, 255, 255, 255, 255, 255, 255, 255 },
  57.                          { 255, 255, 255, 255, 255, 255, 255, 255 },
  58.                          { 255, 255, 255, 255, 255, 255, 255, 255 },
  59.                          { 255, 255, 255, 255, 255, 255, 255, 255 },
  60.                          { 255, 255, 255, 255, 255, 255, 255, 255 },
  61.                          { 255, 255, 255, 255, 255, 255, 255, 255 },
  62.                          { 255, 255, 255, 255, 255, 255, 255, 255 },
  63.                          { 255, 255, 255, 255, 255, 255, 255, 255 } };
  64.     dctTransform(matrix);
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement