-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateDCTMatrix.js
More file actions
62 lines (52 loc) · 1.38 KB
/
Copy pathcreateDCTMatrix.js
File metadata and controls
62 lines (52 loc) · 1.38 KB
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
/*!
* Discrete Cosine Transform Matrix Generator
* Boris Bischoff, July 2020
*/
/*
* Execute this program as follows:
* $ node createDCTMatrix.js 8
* for plain text output of a 8x8 matrix.
*
* For KaTeX mode, execute the program as follows for a 8x8 matrix:
* $ node createDCTMatrix.js 8 latex
*/
let parameters = process.argv.slice(2);
let n = parameters[0];
let style = parameters[1];
let delimiter = '\t';
let linebreak = '\n';
if (style === 'latex' || style === 'katex') {
delimiter = '\&';
linebreak = '\\\\';
}
function getMatrixHead(n) {
return Math.sqrt(1/n).toFixed(3);
}
function getMatrixBody(x, y, n) {
let cos = Math.cos((((2*y)-1)*(x-1)*Math.PI)/(2*n));
let sqrt = Math.sqrt(2/n);
return (sqrt*cos).toFixed(3);
}
function getStructure(y, n) {
if (y < n) {
process.stdout.write(delimiter);
}
if (y == n) {
process.stdout.write(linebreak);
}
}
if (n) {
for (let y = 1; y <= n; y++) {
process.stdout.write(getMatrixHead(n));
getStructure(y, n);
}
for (x = 2; x <= n; x++) {
for (y = 1; y <= n; y++) {
process.stdout.write(getMatrixBody(x, y, n));
getStructure(y, n);
}
}
console.log("\n");
} else {
console.log('Please execute the program as follows: $ node createDCTMatrix.js <integer>.\nExample: $ node createDCTMatrix.js 8');
}