-
Notifications
You must be signed in to change notification settings - Fork 0
/
atdctrl.js
149 lines (124 loc) · 3.38 KB
/
atdctrl.js
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
var app2 = angular.module("ConverterApp", []);
var truncate = function(x, _precision)
{
var precision = _precision || 0;
var scales = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000];
var scale = scales[precision];
return Math.round(x * scale) / scale;
};
var ConverterCtrl = function ($scope)
{
$scope.timeLimit = 20.0;
$scope.analog =
{
element: $("#analog"),
amplitude: 7.0,
cyclicFrqnc: 1.0,
accuracy: 0.1,
value: function(v){ return $scope.analog.amplitude * Math.sin(v * $scope.analog.cyclicFrqnc); },//Math.sin,
points: []
};
$scope.digital =
{
element: $("#digital"),
samplingPeriod: 0.1,
samplingFrqnc: 1.0 / 0.1,
quantLvls: 8,
digitCount: 3,
points: []
};
$scope.getDigitalValue = function(analogValue)
{
var part = 2 * $scope.analog.amplitude / $scope.digital.quantLvls;
var partsCount = analogValue / part;
var integerPartsCount = Math.floor(partsCount);
var fractionalPartsCount = partsCount - integerPartsCount;
if (fractionalPartsCount >= 0.5) integerPartsCount++;
return integerPartsCount * part;
};
$scope.setAnalogSignal = function()
{
$scope.analog.points = [];
$scope.digital.points = [];
for(var i = 0; i < $scope.timeLimit; i+= $scope.analog.accuracy)
{
$scope.analog.points.push([i, $scope.analog.value(i)]);
}
var period = 1.0 / $scope.digital.samplingFrqnc;
for(var i = 0; i < $scope.timeLimit; i+= period)
{
$scope.digital.points.push([i, $scope.getDigitalValue($scope.analog.value(i))]);
$scope.digital.points.push([i + period, $scope.getDigitalValue($scope.analog.value(i))]);
}
$.plot($scope.analog.element, [$scope.analog.points], {yaxis: {max: 10, min: -10}});
$.plot($scope.digital.element, [$scope.digital.points], {yaxis: {max: 10, min: -10}});
};
$scope.setAmplitude = function(a)
{
$scope.analog.amplitude = a;
$scope.setAnalogSignal();
};
$scope.setCyclicFrqnc = function(a)
{
$scope.analog.cyclicFrqnc = truncate(a, 1);
$scope.setAnalogSignal();
};
$scope.setSamplingPeriod = function(a)
{
$scope.digital.samplingPeriod = truncate(a, 1);
$scope.digital.samplingFrqnc = truncate(1.0 / $scope.digital.samplingPeriod, 2);
$scope.setAnalogSignal();
};
$scope.setSamplingFrqnc = function(a)
{
$scope.digital.samplingFrqnc = truncate(a, 2);
$scope.digital.samplingPeriod = truncate(1.0 / $scope.digital.samplingFrqnc, 1);
$scope.setAnalogSignal();
};
$scope.setQuantLvls = function(a)
{
$scope.digital.quantLvls = a;
$scope.setAnalogSignal();
};
$scope.setTimeLimit = function(a)
{
$scope.timeLimit = a;
$scope.setAnalogSignal();
};
$scope.setDigitCount = function(a)
{
$scope.digital.digitCount = a;
$scope.digital.quantLvls = Math.pow(2, a);
$scope.setAnalogSignal();
};
$scope.getAmplitude = function()
{
return $scope.analog.amplitude;
};
$scope.getCyclicFrqnc = function()
{
return $scope.analog.cyclicFrqnc;
};
$scope.getSamplingPeriod = function()
{
return $scope.digital.samplingPeriod;
};
$scope.getSamplingFrqnc = function()
{
return $scope.digital.samplingFrqnc;
};
$scope.getQuantLvls = function()
{
return $scope.digital.quantLvls;
};
$scope.getTimeLimit = function()
{
return $scope.timeLimit;
};
$scope.getDigitCount = function()
{
return $scope.digital.digitCount;
};
$scope.setAnalogSignal();
}
app2.controller("ConverterCtrl", ConverterCtrl);