numz 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/signal/functions/index.js +2 -1
- package/src/signal/functions/window/index.js +142 -0
- package/test/index.js +49 -36
package/package.json
CHANGED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const {cos, sin, abs, sqrt, exp, PI} = Math
|
|
2
|
+
export const hanning_window=(N, ArrayType = Float64Array)=>{
|
|
3
|
+
const w = new ArrayType(N);
|
|
4
|
+
const factor = 2 * PI / N;
|
|
5
|
+
for (let n = 0; n < N; n++)
|
|
6
|
+
w[n] = 0.5 - 0.5 * cos(factor * n);
|
|
7
|
+
return w;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const hamming_window=(N, ArrayType = Float64Array)=>{
|
|
11
|
+
const w = new ArrayType(N);
|
|
12
|
+
const factor = 2 * PI / N;
|
|
13
|
+
for (let n = 0; n < N; n++)
|
|
14
|
+
w[n] = 0.54 - 0.46 * cos(factor * n);
|
|
15
|
+
return w;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const I0 = (x) => {
|
|
19
|
+
let sum = 1;
|
|
20
|
+
let u = 1;
|
|
21
|
+
let k = 1;
|
|
22
|
+
const x2 = (x * x) / 4;
|
|
23
|
+
while (true) {
|
|
24
|
+
// const prev = u;
|
|
25
|
+
u *= x2 / (k * k);
|
|
26
|
+
sum += u;
|
|
27
|
+
if (u < 1e-10 * sum) break;
|
|
28
|
+
k++;
|
|
29
|
+
}
|
|
30
|
+
return sum;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const kaiser_window = (N, beta = 5, ArrayType = Float64Array) => {
|
|
34
|
+
const w = new ArrayType(N);
|
|
35
|
+
const denom = I0(beta);
|
|
36
|
+
for (let n = 0; n < N; n++) {
|
|
37
|
+
const r = (2 * n) / (N - 1) - 1;
|
|
38
|
+
w[n] = I0(beta * sqrt(1 - r * r)) / denom;
|
|
39
|
+
}
|
|
40
|
+
return w;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const blackman_window = (N, beta = 0.16, ArrayType = Float64Array) => {
|
|
44
|
+
const w = new ArrayType(N);
|
|
45
|
+
const factor = 2 * PI / N;
|
|
46
|
+
const a0 = (1 - beta) / 2;
|
|
47
|
+
const a1 = 0.5;
|
|
48
|
+
const a2 = beta / 2;
|
|
49
|
+
for (let n = 0; n < N; n++)
|
|
50
|
+
w[n] = a0,- a1 * cos(factor * n) + a2 * cos(2 * factor * n);
|
|
51
|
+
return w;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export const bartlett_window = (N, ArrayType = Float64Array) => {
|
|
55
|
+
const w = new ArrayType(N);
|
|
56
|
+
for (let n = 0; n < N; n++)
|
|
57
|
+
w[n] = 1 - abs((2 * n) / (N - 1) - 1);
|
|
58
|
+
return w;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export const blackman_harris_window = (N, ArrayType = Float64Array) => {
|
|
62
|
+
const w = new ArrayType(N);
|
|
63
|
+
const a0 = 0.35875;
|
|
64
|
+
const a1 = 0.48829;
|
|
65
|
+
const a2 = 0.14128;
|
|
66
|
+
const a3 = 0.01168;
|
|
67
|
+
const f = 2 * PI / N;
|
|
68
|
+
for (let n = 0; n < N; n++)
|
|
69
|
+
w[n] = a0 - a1 * cos(f * n) + a2 * cos(2 * f * n) - a3 * cos(3 * f * n);
|
|
70
|
+
return w;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const nuttall_window = (N, ArrayType = Float64Array) => {
|
|
74
|
+
const w = new ArrayType(N);
|
|
75
|
+
const f = 2 * PI / N;
|
|
76
|
+
for (let n = 0; n < N; n++)
|
|
77
|
+
w[n] = 0.355768 - 0.487396 * cos(f * n) + 0.144232 * cos(2 * f * n) - 0.012604 * cos(3 * f * n);
|
|
78
|
+
return w;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const flat_top_window = (N, ArrayType = Float64Array) => {
|
|
82
|
+
const w = new ArrayType(N);
|
|
83
|
+
const f = 2 * PI / N;
|
|
84
|
+
for (let n = 0; n < N; n++)
|
|
85
|
+
w[n] = 1 - 1.93 * cos(f * n) + 1.29 * cos(2 * f * n) - 0.388 * cos(3 * f * n) + 0.028 * cos(4 * f * n);
|
|
86
|
+
return w;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const tukey_window = (N, alpha = 0.5, ArrayType = Float64Array) => {
|
|
90
|
+
const w = new ArrayType(N);
|
|
91
|
+
const edge = alpha * (N - 1) / 2;
|
|
92
|
+
for (let n = 0; n < N; n++) {
|
|
93
|
+
if (n < edge)
|
|
94
|
+
w[n] = 0.5 * (1 + cos(PI * (2*n/(alpha*(N-1)) - 1)));
|
|
95
|
+
else if (n > (N - 1 - edge))
|
|
96
|
+
w[n] = 0.5 * (1 + cos(PI * (2*n/(alpha*(N-1)) - 2/alpha + 1)));
|
|
97
|
+
else
|
|
98
|
+
w[n] = 1;
|
|
99
|
+
}
|
|
100
|
+
return w;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const gaussian_window = (N, sigma = 0.4, ArrayType = Float64Array) => {
|
|
104
|
+
const w = new ArrayType(N);
|
|
105
|
+
const m = (N - 1) / 2;
|
|
106
|
+
const s2 = sigma * m;
|
|
107
|
+
for (let n = 0; n < N; n++)
|
|
108
|
+
w[n] = exp(-0.5 * ((n - m) / s2) ** 2);
|
|
109
|
+
return w;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
const sinc = (x) => x === 0 ? 1 : sin(PI * x) / (PI * x);
|
|
114
|
+
export const lanczos_window = (N, ArrayType = Float64Array) => {
|
|
115
|
+
const w = new ArrayType(N);
|
|
116
|
+
for (let n = 0; n < N; n++)
|
|
117
|
+
w[n] = sinc(2 * n / (N - 1) - 1);
|
|
118
|
+
return w;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const poisson_window = (N, alpha = 3, ArrayType = Float64Array) => {
|
|
122
|
+
const w = new ArrayType(N);
|
|
123
|
+
const m = (N - 1) / 2;
|
|
124
|
+
for (let n = 0; n < N; n++)
|
|
125
|
+
w[n] = exp(-alpha * abs(n - m) / m);
|
|
126
|
+
return w;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export const rect_window = (N, ArrayType = Float64Array) => {
|
|
130
|
+
const w = new ArrayType(N);
|
|
131
|
+
w.fill(1);
|
|
132
|
+
return w;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export const tri_window = (N, ArrayType = Float64Array) => {
|
|
136
|
+
const w = new ArrayType(N);
|
|
137
|
+
const M = (N - 1) / 2;
|
|
138
|
+
for (let n = 0; n < N; n++) {
|
|
139
|
+
w[n] = 1 - abs((n - M) / M);
|
|
140
|
+
}
|
|
141
|
+
return w;
|
|
142
|
+
};
|
package/test/index.js
CHANGED
|
@@ -1,39 +1,52 @@
|
|
|
1
|
-
import { fft, ifft } from "../src/signal/fft/index.js";
|
|
2
|
-
import { conv, conv2 } from '../src/signal/conv/index.js'
|
|
3
|
-
import { Complex } from "ziko/math/complex";
|
|
4
|
-
// import { cos } from 'ziko/math/functions'
|
|
1
|
+
// import { fft, ifft } from "../src/signal/fft/index.js";
|
|
2
|
+
// import { conv, conv2 } from '../src/signal/conv/index.js'
|
|
3
|
+
// import { Complex } from "ziko/math/complex";
|
|
4
|
+
// // import { cos } from 'ziko/math/functions'
|
|
5
|
+
|
|
6
|
+
// // const x = Array.from({ length: 100 }, (_, j)=> j/10)
|
|
7
|
+
// // const y = cos(x)
|
|
8
|
+
|
|
9
|
+
// // const ff = fft(x)
|
|
10
|
+
// // // const ff = fft([1,2,3])
|
|
11
|
+
// // // console.log(ff)
|
|
12
|
+
|
|
13
|
+
// // // console.log(ifft(ff))
|
|
14
|
+
// const signal = [1, 2, 3, 4, 5];
|
|
15
|
+
// let kernel = [1, 0.5];
|
|
16
|
+
// // kernel = [1, new Complex(0, 0.5)];
|
|
17
|
+
// const result = conv(signal, kernel);
|
|
18
|
+
// console.log(
|
|
19
|
+
// result
|
|
20
|
+
// )
|
|
5
21
|
|
|
6
|
-
// const x = Array.from({ length: 100 }, (_, j)=> j/10)
|
|
7
|
-
// const y = cos(x)
|
|
8
|
-
|
|
9
|
-
// const ff = fft(x)
|
|
10
|
-
// // const ff = fft([1,2,3])
|
|
11
22
|
// // console.log(ff)
|
|
23
|
+
// // console.log(ifft(ff).map(n=>+ n.a.toFixed(7)))
|
|
24
|
+
// // console.log(x)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
// // Example usage:
|
|
28
|
+
// const image = [
|
|
29
|
+
// [1, 2, 3],
|
|
30
|
+
// [4, 5, 6],
|
|
31
|
+
// [7, 8, 9]
|
|
32
|
+
// ];
|
|
33
|
+
|
|
34
|
+
// const filter = [
|
|
35
|
+
// [1, 0],
|
|
36
|
+
// [0, 1]
|
|
37
|
+
// ];
|
|
38
|
+
|
|
39
|
+
// console.log(conv2(image, filter, 'full', 'fill'));
|
|
40
|
+
|
|
41
|
+
function hanning(N, ArrayType = Float64Array) {
|
|
42
|
+
const w = new ArrayType(N);
|
|
43
|
+
const factor = 2 * Math.PI / N;
|
|
44
|
+
|
|
45
|
+
for (let n = 0; n < N; n++) {
|
|
46
|
+
w[n] = 0.5 - 0.5 * Math.cos(factor * n);
|
|
47
|
+
}
|
|
48
|
+
return w;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
console.log(hanning(10).map(a=>a*20))
|
|
12
52
|
|
|
13
|
-
// // console.log(ifft(ff))
|
|
14
|
-
const signal = [1, 2, 3, 4, 5];
|
|
15
|
-
let kernel = [1, 0.5];
|
|
16
|
-
// kernel = [1, new Complex(0, 0.5)];
|
|
17
|
-
const result = conv(signal, kernel);
|
|
18
|
-
console.log(
|
|
19
|
-
result
|
|
20
|
-
)
|
|
21
|
-
|
|
22
|
-
// console.log(ff)
|
|
23
|
-
// console.log(ifft(ff).map(n=>+ n.a.toFixed(7)))
|
|
24
|
-
// console.log(x)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
// Example usage:
|
|
28
|
-
const image = [
|
|
29
|
-
[1, 2, 3],
|
|
30
|
-
[4, 5, 6],
|
|
31
|
-
[7, 8, 9]
|
|
32
|
-
];
|
|
33
|
-
|
|
34
|
-
const filter = [
|
|
35
|
-
[1, 0],
|
|
36
|
-
[0, 1]
|
|
37
|
-
];
|
|
38
|
-
|
|
39
|
-
console.log(conv2(image, filter, 'full', 'fill'));
|