numz 0.0.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.
@@ -0,0 +1,174 @@
1
+ import { Matrix , matrix, sqrt } from "ziko";
2
+ const conv1d=(input, kernel , circular = true)=>{
3
+ const INPUT_LENGTH = input.length;
4
+ const KERNEL_LENGTH = kernel.length;
5
+ const output = [];
6
+ const LENGTH_OUT = circular ? Math.max(INPUT_LENGTH,KERNEL_LENGTH) : INPUT_LENGTH + KERNEL_LENGTH - 1;
7
+ for (let i = 0; i < LENGTH_OUT; i++) {
8
+ let sum = 0;
9
+ for (let j = 0; j < KERNEL_LENGTH; j++) {
10
+ const inputIndex = i + j - Math.floor(KERNEL_LENGTH / 2);
11
+ // Apply zero-padding for out-of-bounds indices
12
+ const inputValue = inputIndex >= 0 && inputIndex < INPUT_LENGTH
13
+ ? input[inputIndex]
14
+ : 0;
15
+ sum += inputValue * kernel[j];
16
+ }
17
+ output.push(sum);
18
+ }
19
+ return output;
20
+ }
21
+
22
+ const conv2d = (input, kernel, circular = true) => {
23
+ if(!(input instanceof Matrix)) input = matrix(input);
24
+ if(!(kernel instanceof Matrix)) kernel = matrix(kernel);
25
+ const INPUT_ROWS=input.rows;
26
+ const INPUT_COLS=input.cols;
27
+ const OUTPUT_ROWS = circular ? Math.max(input.rows,kernel.rows) : input.rows + kernel.rows-1;
28
+ const OUTPUT_COLS = circular ? Math.max(input.cols,kernel.cols) : input.cols + kernel.cols-1;
29
+ const KERNEL_SIZE = kernel.rows;
30
+ const output = [];
31
+ for (let i = 0; i < OUTPUT_ROWS ; i++) {
32
+ const row = [];
33
+ for (let j = 0; j < OUTPUT_COLS ; j++) {
34
+ let sum = 0;
35
+ for (let k = 0; k < KERNEL_SIZE; k++) {
36
+ for (let l = 0; l < KERNEL_SIZE; l++) {
37
+ const rowIndex = i + k - Math.floor(KERNEL_SIZE / 2);
38
+ const colIndex = j + l - Math.floor(KERNEL_SIZE / 2);
39
+ // Apply zero-padding for out-of-bounds indices
40
+ const inputValue = (rowIndex >= 0 && rowIndex < INPUT_ROWS &&
41
+ colIndex >= 0 && colIndex < INPUT_COLS)
42
+ ? input[rowIndex][colIndex]
43
+ : 0;
44
+ sum += inputValue * kernel[k][l];
45
+ }
46
+ }
47
+ row.push(sum);
48
+ }
49
+ output.push(row);
50
+ }
51
+ return output;
52
+ };
53
+
54
+ var convolute=(parent,kernel = [0, -1, 0, -1, 5, -1, 0, -1, 0], x1 = 0, y1 = 0, x2 = parent.element.width, y2 = parent.element.height)=>{
55
+ if(kernel instanceof Matrix)kernel=kernel.arr.flat(1)
56
+ var opaque = 1;
57
+ var pixels = parent.ctx.getImageData(x1, y1, x2, y2);
58
+ var side = Math.round(sqrt(kernel.length));
59
+ var halfSide = Math.floor(side / 2);
60
+ var src = pixels.data;
61
+ var sw = pixels.width;
62
+ var sh = pixels.height;
63
+ // pad output by the convolution matrix
64
+ var w = sw;
65
+ var h = sh;
66
+ var output = parent.ctx.createImageData(w, h);
67
+ var dst = output.data;
68
+ // go through the destination image pixels
69
+ var alphaFac = opaque ? 1 : 0;
70
+ for (var y = 0; y < h; y++) {
71
+ for (var x = 0; x < w; x++) {
72
+ var sy = y;
73
+ var sx = x;
74
+ var dstOff = (y * w + x) * 4;
75
+ // calculate the weighed sum of the source image pixels that
76
+ // fall under the convolution matrix
77
+ var r = 0,
78
+ g = 0,
79
+ b = 0,
80
+ a = 0;
81
+ for (var cy = 0; cy < side; cy++) {
82
+ for (var cx = 0; cx < side; cx++) {
83
+ var scy = sy + cy - halfSide;
84
+ var scx = sx + cx - halfSide;
85
+ if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
86
+ var srcOff = (scy * sw + scx) * 4;
87
+ var wt = kernel[cy * side + cx];
88
+ r += src[srcOff] * wt;
89
+ g += src[srcOff + 1] * wt;
90
+ b += src[srcOff + 2] * wt;
91
+ a += src[srcOff + 3] * wt;
92
+ }
93
+ }
94
+ }
95
+ dst[dstOff] = r;
96
+ dst[dstOff + 1] = g;
97
+ dst[dstOff + 2] = b;
98
+ dst[dstOff + 3] = a + alphaFac * (255 - a);
99
+ }
100
+ }
101
+ return output;
102
+ }
103
+
104
+ convolute=(parent,kernel = [0, -1, 0, -1, 5, -1, 0, -1, 0], x1 = 0, y1 = 0, x2 = parent.element.width, y2 = parent.element.height)=>{
105
+ if(kernel instanceof Matrix)kernel=kernel.arr.flat(1)
106
+ var opaque = 1;
107
+ var pixels = parent.ctx.getImageData(x1, y1, x2, y2);
108
+ var side = Math.round(sqrt(kernel.length));
109
+ var halfSide = Math.floor(side / 2);
110
+ var src = pixels.data;
111
+ var sw = pixels.width;
112
+ var sh = pixels.height;
113
+ // pad output by the convolution matrix
114
+ var w = sw;
115
+ var h = sh;
116
+ var output = parent.ctx.createImageData(w, h);
117
+ var dst = output.data;
118
+ // go through the destination image pixels
119
+ var alphaFac = opaque ? 1 : 0;
120
+ for (var y = 0; y < h; y++) {
121
+ for (var x = 0; x < w; x++) {
122
+ var sy = y;
123
+ var sx = x;
124
+ var dstOff = (y * w + x) * 4;
125
+ // calculate the weighed sum of the source image pixels that
126
+ // fall under the convolution matrix
127
+ var r = 0,
128
+ g = 0,
129
+ b = 0,
130
+ a = 0;
131
+ for (var cy = 0; cy < side; cy++) {
132
+ for (var cx = 0; cx < side; cx++) {
133
+ var scy = sy + cy - halfSide;
134
+ var scx = sx + cx - halfSide;
135
+ if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
136
+ var srcOff = (scy * sw + scx) * 4;
137
+ var wt = kernel[cy * side + cx];
138
+ r += src[srcOff] * wt;
139
+ g += src[srcOff + 1] * wt;
140
+ b += src[srcOff + 2] * wt;
141
+ a += src[srcOff + 3] * wt;
142
+ }
143
+ }
144
+ }
145
+ dst[dstOff] = r;
146
+ dst[dstOff + 1] = g;
147
+ dst[dstOff + 2] = b;
148
+ dst[dstOff + 3] = a + alphaFac * (255 - a);
149
+ }
150
+ }
151
+ return output;
152
+ }
153
+ const conv=(input,kernel,circular)=>{
154
+ if(input instanceof Matrix || (input instanceof Array && input[0][0]))return conv2d(input,kernel,circular);
155
+ return conv1d(input,kernel,circular)
156
+ }
157
+ const circularConv=(input,kernel)=>conv(input,kernel,true);
158
+ const linearConv=(input,kernel)=>conv(input,kernel,false);
159
+ const circularConv1d=(input,kernel)=>conv1d(input,kernel,true);
160
+ const circularConv2d=(input,kernel)=>conv2d(input,kernel,true);
161
+ const linearConv1d=(input,kernel)=>conv1d(input,kernel,false);
162
+ const linearConv2d=(input,kernel)=>conv2d(input,kernel,false);
163
+ export{
164
+ conv1d,
165
+ conv2d,
166
+ conv,
167
+ circularConv,
168
+ linearConv,
169
+ circularConv1d,
170
+ linearConv1d,
171
+ circularConv2d,
172
+ linearConv2d,
173
+ convolute
174
+ }
package/src-dep/fft.js ADDED
@@ -0,0 +1,59 @@
1
+ import{
2
+ Complex,
3
+ complex,
4
+ PI,
5
+ cos,
6
+ sin
7
+ } from "ziko"
8
+ const fft=x=>{
9
+ const output = [];
10
+ const N = x.length;
11
+ if(!(x[0]instanceof Complex))x=x.map((n)=>complex(n,0));
12
+ for (let k = 0; k < N; k++) {
13
+ let re = 0,im=0;
14
+ for (let n = 0; n < N; n++) {
15
+ const phi = (2*PI * k * n) / N;
16
+ re += x[n].a*cos(phi)+x[n].b*sin(phi);
17
+ im += -x[n].a*sin(phi)+x[n].b*cos(phi);
18
+ }
19
+ re = re / N;
20
+ im = im / N;
21
+ output[k] = complex(re,im);
22
+ }
23
+ return {
24
+ output,
25
+ re:output.map(n=>n.a),
26
+ im:output.map(n=>n.b),
27
+ z:output.map(n=>n.z),
28
+ phi:output.map(n=>n.phi)
29
+ }
30
+ }
31
+ const ifft=x=>{
32
+ const output = [];
33
+ const N = x.length;
34
+ if(!(x[0]instanceof Complex))x=x.map((n)=>complex(n,0));
35
+ for (let k = 0; k < N; k++) {
36
+ let re=0,im=0;
37
+ for (let n = 0; n < N; n++) {
38
+ const phi = (2*PI * k * n) / N;
39
+ re += x[n].a*cos(phi)+x[n].b*sin(phi);
40
+ im += x[n].a*sin(phi)+x[n].b*cos(phi);
41
+ }
42
+ re = re / N;
43
+ im = im / N;
44
+
45
+ output[k] = complex(re,im);
46
+ }
47
+ return {
48
+ output,
49
+ re:output.map(n=>n.a),
50
+ im:output.map(n=>n.b),
51
+ z:output.map(n=>n.z),
52
+ phi:output.map(n=>n.phi)
53
+ };
54
+ }
55
+
56
+ export{
57
+ fft,
58
+ ifft
59
+ }
@@ -0,0 +1,39 @@
1
+ import { complex } from "ziko";
2
+ import {fft,ifft} from "./fft.js";
3
+ // should be processed in other thread
4
+ class Filter{
5
+ constructor(input){
6
+ this.input=input;
7
+ this.input_fft=fft(this.input);
8
+ this.output_fft=[]
9
+ }
10
+ // get length(){
11
+ // return this.input.length;
12
+ // }
13
+ lowPass(fc){
14
+ this.input_fft.output.forEach((n,i)=>{
15
+ n=n.z<fc
16
+ ? this.output_fft[i]=this.input_fft.output[i]
17
+ : this.output_fft[i]=complex(0,0)
18
+ })
19
+ return ifft(this.output_fft).re;
20
+ }
21
+ highPass(fc){
22
+ this.input_fft.output.forEach((n,i)=>{
23
+ n=n.z>fc
24
+ ? this.output_fft[i]=this.input_fft.output[i]
25
+ : this.output_fft[i]=complex(0,0)
26
+ })
27
+ return ifft(this.output_fft).re;
28
+ }
29
+ bandePass(){
30
+
31
+ }
32
+ bandeCoupe(){
33
+
34
+ }
35
+ }
36
+ const filter=input=>new Filter(input);
37
+ export{
38
+ filter
39
+ }
@@ -0,0 +1,85 @@
1
+ import { mapfun, abs , sinc , sign , sin, Random } from "ziko";
2
+ import{
3
+ fft,
4
+ ifft
5
+ } from "./fft.js"
6
+ import {
7
+ conv,
8
+ conv1d,
9
+ conv2d,
10
+ circularConv,
11
+ linearConv,
12
+ circularConv1d,
13
+ linearConv1d,
14
+ circularConv2d,
15
+ linearConv2d
16
+ } from "./conv.js";
17
+ import {
18
+ filter
19
+ } from "./filter.js";
20
+ //import { Matrix } from "../Matrix/index.js";
21
+ const Signal={
22
+
23
+ noise(n,min = 0,max = 1){
24
+ return Random.floats(n,min,max);
25
+ },
26
+ echelon(t,t0 = 0 , A = 1){
27
+ if(!(t instanceof Array))t=[t];
28
+ const Y = mapfun(n=>n>=t0?1:0,...t);
29
+ return Y instanceof Array ? Y.map(n=>n*A) : Y*A
30
+ },
31
+ rampe(t,t0 = 0 , A = 1){
32
+ if(!(t instanceof Array))t=[t]
33
+ const Y = mapfun(n=>n>=t0?n-t0:0,...t);
34
+ return Y instanceof Array ? Y.map(n=>n*A) : Y*A
35
+ },
36
+ sign(t,t0 = 0 , A = 1){
37
+ if(!(t instanceof Array))t=[t]
38
+ const Y = mapfun(n=>Math.sign(n-t0),...t);
39
+ return Y instanceof Array ? Y.map(n=>n*A) : Y*A
40
+ },
41
+ rect(t,T,t0 = 0, A = 1){
42
+ if(!(t instanceof Array))t=[t];
43
+ const Y = mapfun(n=>((t0-T/2 < n) && (t0+T/2 > n)? 1 - 2 * abs(n/T) : 0),...t)
44
+ return Y instanceof Array ? Y.map(n=>n*A) : Y*A
45
+ },
46
+ tri(t,T,t0 = 0 , A = 1){
47
+ if(!(t instanceof Array))t=[t]
48
+ const Y = mapfun(n=>((t0-T/2 < n) && (t0+T/2 > n)? 1 - 2 * abs(n/T) : 0),...t)
49
+ return Y instanceof Array ? Y.map(n=>n*A) : Y*A;
50
+ },
51
+ dirac(t,t0){
52
+ return mapfun(n=>n===t0?Infinity:0,...t);
53
+ },
54
+ lorentz(t , t0 = 0 , A = 1){
55
+ if(!(t instanceof Array))t=[t]
56
+ const Y = mapfun(n => 1/(1+(n-t0)**2),...t);
57
+ return Y instanceof Array ? Y.map(n=>n*A) : Y*A;
58
+ },
59
+ sinc(t , t0 , A = 1){
60
+ if(!(t instanceof Array))t=[t]
61
+ const Y = mapfun(n=>sinc(n-t0),...t);
62
+ return Y instanceof Array ? Y.map(n=>n*A) : Y*A;
63
+ },
64
+ square(t,T=1,A=1){
65
+ if(!(t instanceof Array))t=[t]
66
+ const Y = mapfun(n=>sign(sin(n*2*Math.PI/T)),...t);
67
+ return Y instanceof Array ? Y.map(n=>n*A) : Y*A;
68
+ },
69
+ sawtooth(){
70
+
71
+ },
72
+ conv,
73
+ conv1d,
74
+ conv2d,
75
+ circularConv,
76
+ linearConv,
77
+ circularConv1d,
78
+ linearConv1d,
79
+ circularConv2d,
80
+ linearConv2d,
81
+ fft,
82
+ ifft,
83
+ filter,
84
+ }
85
+ export{Signal}