-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdspMD1.qml
162 lines (131 loc) · 4.44 KB
/
dspMD1.qml
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import QtQuick 2.0
// [0; 1) Math.random()
// [0; 2) Math.random() + Math.random()
// [0; 4) Math.random() + Math.random() + Math.random() + Math.random()
//var step = 0.125 // x
//var step = 0.25 // x2
//var step = 0.5 // x4
Rectangle {
id: win
width: 800
height: 600
property int resolution: 80 // adjust according to your needs
function genData(rounds) {
if (rounds === 1) return Math.random()
return Math.random() + genData(--rounds)
}
function magicData(count, rounds) {
var signalData = new Array(count)
for (var j = 0; j < signalData.length; j++) {
signalData[j] = genData(rounds);
}
return signalData
}
function getStandardDeviation(mean, signalData) {
var variance = 0
for (var i = 0; i < signalData.length; i++) {
variance += (signalData[i] - mean) * (signalData[i] - mean)
}
variance /= signalData.length - 1
return Math.sqrt(variance)
}
function showHistogram(count, rounds, binCount) {
randomNumberModel.clear() // clears the screen for new ploting
var mean = 0; var tmp = 0
var variance = 0 // standard deviation
var step = rounds / binCount
var bins = new Array(binCount)
for (var i = 0; i < bins.length; i++) { bins[i] = 0 }
var signalData = magicData(count, rounds) // generated signal measurements
for (var t = 0; t < signalData.length; t++) {
mean += signalData[t]
for (var b = 0; b < bins.length; b++) {
if ((signalData[t] >= step * b) && (signalData[t] < step * b + step)) {
//console.log("Signal: " + signalData[t] + " bin-index: " + b)
bins[b]++
break
}
}
}
mean = mean / count
// fill the data model for ploting the histogram
for (var j = 0; j < bins.length; j++ ) {
randomNumberModel.append({"xx": j, "yy": bins[j]})
}
info.step = step
info.mean = mean
info.standardDeviation = getStandardDeviation(mean, signalData)
}
RandomNumberModel { id: randomNumberModel }
Column {
id: info
anchors.top: win.top
anchors.topMargin: 10
anchors.left: win.left
anchors.leftMargin: 10
spacing: 8
property alias step: stepLabel.step
property alias mean: meanLabel.mean
property alias standardDeviation: standardDeviationLabel.standardDeviation
Text {
id: stepLabel
property string step
text: "Step: " + step
}
Text {
id: meanLabel
property string mean
text: "Mean: " + mean
}
Text {
id: standardDeviationLabel;
property string standardDeviation
text: "Standard Deviation: " + standardDeviation
}
}
Row {
id: pipeRow
anchors.bottom: parent.bottom
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottomMargin: 30
Repeater {
model: randomNumberModel
Item {
width: 30
height: yy === 0 ? 1 : yy / resolution // take care of pipes having 0 values for height
anchors.bottom: pipeRow.bottom
Text {
anchors.bottom: bin.top
anchors.bottomMargin: 20
anchors.horizontalCenter: parent.horizontalCenter
rotation: 90
text: yy
}
Rectangle {
id: bin
width: parent.width
height: parent.height
border.color: "black"
border.width: 1
color: "darkgreen"
}
}
}
}
MouseArea {
anchors.fill: parent
onClicked: {
showHistogram(80000, 4, 16)
//showHistogram(80000, 2, 16)
//showHistogram(80000, 1, 8)
}
}
}
// -------- NOTES ---------
// The floor method rounds numbers down to the nearest integer.
// http://www.the-art-of-web.com/javascript/random/
// http://www.boallen.com/random-numbers.html
// http://en.wikipedia.org/wiki/PRNG
// http://en.wikipedia.org/wiki/Turing_completeness
// -------------------------
// console.log(Math.floor(Math.random()) * 11)