Skip to content

Commit 4f089fc

Browse files
RefreshRate calculation bug - samples.length
1 parent 15414d8 commit 4f089fc

File tree

1 file changed

+60
-68
lines changed

1 file changed

+60
-68
lines changed

src/common.js

Lines changed: 60 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Decimal from './external/decimal.mjs';
22

3-
export const calculateCycleDurationInSeconds = frequency => 1 / frequency;
3+
export const calculateCycleDurationInSeconds = frequency => 1 / frequency;
44

55

66
// --------------------- Refresh Rate Helpers ---------------------
@@ -13,30 +13,30 @@ export const getRefreshRateReadings = async (bufferSize = 10, samples = 10) => {
1313
return new Promise(resolve => {
1414

1515
const run = (now) => {
16-
rafTimestamps.unshift(now); // place current timestamp (passed each time requestAnimationFrame is invoked) at the start of the array
17-
18-
if (rafTimestamps.length > bufferSize) { // once 'timestamps' array contains 11 elements
19-
20-
var firstTime = rafTimestamps.pop();
21-
var refreshRate = 1000 * bufferSize / (now - firstTime);
22-
refreshRates.unshift(refreshRate);
23-
24-
if (refreshRates.length == samples) {
25-
cancelAnimationFrame(id);
26-
resolve(refreshRates)
27-
return;
28-
}
16+
rafTimestamps.unshift(now); // place current timestamp (passed each time requestAnimationFrame is invoked) at the start of the array
17+
18+
if (rafTimestamps.length > bufferSize) { // once 'timestamps' array contains 11 elements
19+
20+
var firstTime = rafTimestamps.pop();
21+
var refreshRate = 1000 * bufferSize / (now - firstTime);
22+
refreshRates.unshift(refreshRate);
23+
24+
if (refreshRates.length == samples.length) {
25+
cancelAnimationFrame(id);
26+
resolve(refreshRates)
27+
return;
2928
}
30-
31-
var id = window.requestAnimationFrame(run);
32-
}
29+
}
3330

3431
var id = window.requestAnimationFrame(run);
32+
}
33+
34+
var id = window.requestAnimationFrame(run);
3535
})
3636

3737
};
3838

39-
export const calculateRefreshRate = async (bufferSize=10, samples=10) => {
39+
export const calculateRefreshRate = async (bufferSize = 10, samples = 10) => {
4040

4141
const refreshRates = await getRefreshRateReadings(bufferSize, samples)
4242

@@ -54,77 +54,69 @@ export const calculateRefreshRate = async (bufferSize=10, samples=10) => {
5454

5555
if (maxCount.length > 1) maxCount = [maxCount.reduce((a, b) => a + b) / maxCount.length];
5656

57-
const val = Number(maxCount.shift().toFixed(7))
58-
return val
57+
const val = Number(maxCount.shift().toFixed(7))
58+
return val
5959
}
6060

6161

6262
// --------------------- Decimal.js Helpers ---------------------
6363
export const period = new Decimal(2).times(Decimal.acos(-1));
6464

65-
export function countDecimals(stimulusFrequency)
66-
{
67-
if (Math.floor(stimulusFrequency) === Number(stimulusFrequency))
68-
return 0;
65+
export function countDecimals(stimulusFrequency) {
66+
if (Math.floor(stimulusFrequency) === Number(stimulusFrequency))
67+
return 0;
6968

70-
var noOfDecimalPlaces = stimulusFrequency.toString().split(".")[1].length;
69+
var noOfDecimalPlaces = stimulusFrequency.toString().split(".")[1].length;
7170

72-
if (noOfDecimalPlaces < 3)
73-
return noOfDecimalPlaces;
71+
if (noOfDecimalPlaces < 3)
72+
return noOfDecimalPlaces;
7473

75-
throw "Stimuli frequencies must have less than 3 decimal places."
74+
throw "Stimuli frequencies must have less than 3 decimal places."
7675
}
7776

78-
export function calculateNumberOfSeconds(stimulusFrequency)
79-
{
80-
var maxSeconds = Math.pow(10, countDecimals(stimulusFrequency));
77+
export function calculateNumberOfSeconds(stimulusFrequency) {
78+
var maxSeconds = Math.pow(10, countDecimals(stimulusFrequency));
8179

82-
for (var noOfSecs = 1; noOfSecs <= maxSeconds; noOfSecs += 1)
83-
{
84-
85-
var noOfCycles = new Decimal(noOfSecs).times(stimulusFrequency);
80+
for (var noOfSecs = 1; noOfSecs <= maxSeconds; noOfSecs += 1) {
8681

87-
if (noOfCycles.mod(1).toNumber() === 0)
88-
return noOfSecs;
89-
}
82+
var noOfCycles = new Decimal(noOfSecs).times(stimulusFrequency);
9083

91-
throw "Failed to calculate the required number of seconds.";
84+
if (noOfCycles.mod(1).toNumber() === 0)
85+
return noOfSecs;
86+
}
87+
88+
throw "Failed to calculate the required number of seconds.";
9289
}
9390

94-
export function modulus(x, y)
95-
{
96-
var quotient = x.div(y);
91+
export function modulus(x, y) {
92+
var quotient = x.div(y);
9793

98-
var quotientInt = new Decimal(quotient.toFixed(0));
94+
var quotientInt = new Decimal(quotient.toFixed(0));
9995

100-
if ((quotient.minus(quotientInt)).div(quotientInt).abs().toNumber() < Number.EPSILON)
101-
{
102-
return new Decimal(0);
103-
}
104-
else
105-
{
106-
var n = quotient.floor();
107-
var temp = y.times(n);
108-
109-
var signY = Math.sign(y.toNumber());
110-
var result = x.minus(temp);
111-
112-
if (!x.equals(y) && !result.equals(0))
113-
{
114-
if (Math.sign(result.toNumber()) != signY)
115-
result = result.abs().times(signY);
116-
}
96+
if ((quotient.minus(quotientInt)).div(quotientInt).abs().toNumber() < Number.EPSILON) {
97+
return new Decimal(0);
98+
}
99+
else {
100+
var n = quotient.floor();
101+
var temp = y.times(n);
117102

118-
return result;
103+
var signY = Math.sign(y.toNumber());
104+
var result = x.minus(temp);
105+
106+
if (!x.equals(y) && !result.equals(0)) {
107+
if (Math.sign(result.toNumber()) != signY)
108+
result = result.abs().times(signY);
119109
}
110+
111+
return result;
112+
}
120113
}
121114

122-
export function generateSquareWave(timeInstant)
123-
{
124-
const dutyCycle = new Decimal(50),
125-
halfPeriod = period.times(dutyCycle.div(100));
126-
127-
var modulusValue = modulus(timeInstant, period);
115+
export function generateSquareWave(timeInstant) {
116+
const dutyCycle = new Decimal(50),
117+
halfPeriod = period.times(dutyCycle.div(100));
118+
119+
var modulusValue = modulus(timeInstant, period);
128120

129-
return 2 * (modulusValue.toNumber() < halfPeriod.toNumber() ? 1 : 0) - 1;
121+
return 2 * (modulusValue.toNumber() < halfPeriod.toNumber() ? 1 : 0) - 1;
130122
}

0 commit comments

Comments
 (0)