summaryrefslogtreecommitdiff
path: root/Build/index.html
blob: e8af18b3575dba3f9c632ac3e1f7c0b0fe5deb24 (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!DOCTYPE html>

    <meta charset="utf-8">
    <title>WebAssembly Example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">



    <!-- Include the JavaScript glue code. -->
    <!-- This will load the WebAssembly module and run its main. --> 
    <script src="index.js"></script>
    <script type='text/javascript'>
          var Module = {};
          fetch('index.wasm')
            .then(response =>
              response.arrayBuffer()
            ).then(buffer => {
              //Module.canvas = document.getElementById("canvas");
              Module.wasmBinary = buffer;
              var script = document.createElement('script');
              script.src = "index.js";
              script.onload = function() {
                console.log("Emscripten boilerplate loaded.");
                run_dft         = Module.cwrap("dft",            [],[['float'],['float'],'number','number']);
                run_fft_shuffle = Module.cwrap("ffti_shuffle_1", [],[['float'],['float'],'number']);
                run_fft         = Module.cwrap("fft_1",          [],[['float'],['float'],'number','number']);



                var inputDataI    = document.querySelector('.inputDataI');
                var inputDataIerr = document.querySelector('.inputDataIerr');
                var inputDataQ    = document.querySelector('.inputDataQ');
                var inputDataQerr = document.querySelector('.inputDataQerr');
                var outputDataI   = document.querySelector('.outputDataI');
                var outputDataQ   = document.querySelector('.outputDataQ');

                document.querySelector("#calcButton").onclick = function() {
                      console.log("calc FFT");

                      function cArray(size) {
                          var offset = Module._malloc(size * 8);
                          Module.HEAPF64.set(new Float64Array(size), offset / 8);
                          return {
                              "data": Module.HEAPF64.subarray(offset / 8, offset / 8 + size),
                              "offset": offset
                          }
                      }

                      function checkArr(inputData,errorOutput) {
                        var strArr = inputData.value;
                        
                        strArr = strArr.replace(/ +(?= )/g,'');
                        var splArr = strArr.split(" ");
                        var data = Array(splArr.length).fill(0);
                        for (let i=0; i<splArr.length;i++) {
                          try {
                            var j=splArr[i];
                            if ( j != parseFloat(j)) throw new Error("ERROR: not float "+splArr[i]+" at index "+i);
                            data[i] = parseFloat(j)
                          } catch (err) {
                            console.log("Error: ",err.message)
                            errorOutput.innerHTML = err.message;
                            return {"result":false,"data":[]};
                          }

                        }
                        return {"result":true,"data":data};
                      }
                      console.log("checkArr");
                      var arrIok = checkArr(inputDataI,inputDataIerr);
                      console.log("checkArr");
                      var arrQok = checkArr(inputDataQ,inputDataQerr);
                      //max size 8 values, as thats enought for a demo
                      //https://stackoverflow.com/questions/17883799/how-to-handle-passing-returning-array-pointers-to-emscripten-compiled-code
                      if (!arrIok.result) return;
                      if (!arrQok.result) return;

                      var N=8;
                      var myArray_i = cArray(N);
                      var myArray_q = cArray(N);

                      //set test data
                      for(let i=0,j=0;i<N,j<arrIok.data.length;i++,j++) {
                        myArray_i.data[i] = arrIok.data[j];
                      }
                      for(let i=0,j=0;i<N,j<arrQok.data.length;i++,j++) {
                        myArray_q.data[i] = arrQok.data[j];
                      }
                      console.log(myArray_i.data);
                      console.log(myArray_q.data);
                      //DFT
                      run_dft(myArray_i.offset,myArray_q.offset,N,0);
                      //FFT
                      //run_fft_shuffle(myArray_i.offset,myArray_q.offset,N);
                      //run_fft(myArray_i.offset,myArray_q.offset,N,1);
                      
                      console.log(myArray_i.data);
                      console.log(myArray_q.data)
                      function outputResult(array,output) {
                        var arr = Array(array.slice(0,N));
                      
                        arr = arr.map(function(item){
                          return item.map(function(num){
                            return parseFloat(num.toFixed(4));
                          });
                        });
                        output.value = arr;
                      }
                      outputResult(myArray_i.data, outputDataI);
                      outputResult(myArray_q.data, outputDataQ);
                      
                  }

              } //end onload
              document.body.appendChild(script);
            });
            
            
    </script>

    <script>
    </script>

    FFT demo, max value N=8 </br>
    Input I:<textarea class="inputDataI">1 1 0</textarea><sup class="inputDataIerr"></sup></br>
    Input Q:<textarea class="inputDataQ">0 0 0</textarea><sup class="inputDataQerr"></sup></br>
    Output I:<textarea class="outputDataI"></textarea></br>
    Output Q:<textarea class="outputDataQ"></textarea></br>
    <button id="calcButton" type="button">Cals</button></br>

    <script>
      function calcFFT() {
              console.log("Calculate FFT")
              var arrStr = inputData.value;
              console.log(arrStr)
            }
    </script>