Thanks for all the help. It works locally. But now I’m trying to run the study on Pavlovia and I’m having trouble using the values. I get ‘value is not finite’. Even though I’ve set the values to 4 decimel places (this is after the thresholding routine). I’ve tried changing the code (removing let - I saw on another thread that var could be the problem).
Basically I’m trying to obtain thresholds from my Quest - I’ll later use these values as volumes.
Post-threshold routine code:
// Function to calculate quantiles (after the loop finishes)
function calculateQuantiles(x_data, y_data) {
// Sort the data based on intensity values (x_data)
let sortedData = x_data.map((x, i) => ({ x: x, y: y_data[i] }));
sortedData.sort((a, b) => a.x - b.x); // Sort by x (intensity)
// Get sorted x values
let sortedX = sortedData.map(d => d.x);
// Calculate quantiles
let threshold_50 = calculatePercentile(sortedX, 50);
let threshold_25 = calculatePercentile(sortedX, 25);
let threshold_75 = calculatePercentile(sortedX, 75);
// Round thresholds to 4 decimal places and ensure they remain numbers
let threshold_dict = {
"threshold_50": Number(threshold_50.toFixed(4)),
"threshold_25": Number(threshold_25.toFixed(4)),
"threshold_75": Number(threshold_75.toFixed(4))
};
// Print values to console
console.log("Thresholds calculated:");
console.log("50% threshold:", threshold_dict["threshold_50"]);
console.log("25% threshold:", threshold_dict["threshold_25"]);
console.log("75% threshold:", threshold_dict["threshold_75"]);
return threshold_dict;
}
// Declare global variables to store thresholds
let threshold_50, threshold_25, threshold_75;
// Function to calculate quantiles (after the loop finishes)
function calculateQuantiles(x_data, y_data) {
// Sort the data based on intensity values (x_data)
let sortedData = x_data.map((x, i) => ({ x: x, y: y_data[i] }));
sortedData.sort((a, b) => a.x - b.x); // Sort by x (intensity)
// Get sorted x values
let sortedX = sortedData.map(d => d.x);
// Calculate quantiles
threshold_50 = calculatePercentile(sortedX, 50);
threshold_25 = calculatePercentile(sortedX, 25);
threshold_75 = calculatePercentile(sortedX, 75);
// Round thresholds to 4 decimal places and ensure they remain numbers
threshold_50 = Number(threshold_50.toFixed(4));
threshold_25 = Number(threshold_25.toFixed(4));
threshold_75 = Number(threshold_75.toFixed(4));
// Print values to console
console.log("Thresholds calculated:");
console.log("50% threshold:", threshold_50);
console.log("25% threshold:", threshold_25);
console.log("75% threshold:", threshold_75);
}
// Function to calculate the nth percentile
function calculatePercentile(sortedData, percentile) {
let index = Math.floor(percentile / 100 * (sortedData.length - 1));
return sortedData[index];
}
// At the end of your loop or experiment, no need to use ‘let’ for global variables
calculateQuantiles(x_data, y_data);
// Print final values to console again (for clarity)
console.log(“Final Thresholds:”);
console.log(“50% threshold:”, threshold_50);
console.log(“25% threshold:”, threshold_25);
console.log(“75% threshold:”, threshold_75);
Then later in the study I have a conditions file with a column GVolume which includes values e.g., threshold_75. I then use this code to make sure it converts to the values obtained and use $GVolume2 as my variable in my audio file (this has worked for python).
if (GVolume === “threshold_50”) {
GVolume2 = threshold_50;
} else if (GVolume === “threshold_25”) {
GVolume2 = threshold_25;
} else if (GVolume === “threshold_75”) {
GVolume2 = threshold_75;
} else if (GVolume === “0”) {
GVolume2 = 0;
} else {
GVolume2 = threshold_75; // Default value if no condition matches
}
// Print values for debugging
console.log(“Trial 1”, TrialLoop.thisTrialN + 1, “GVolume:”, GVolume);
console.log(“Volume:”, GVolume2);
I tried using the values directly (GVolume) from the condition file and it didn’t work either.
Link to study:
Another unrelated Q - I use a sound file throughout my study. It initially sounds fine but as it goes through the experiment it seems to change? Any idea why.
Any help would be much appreciated! I’ve been going in circles all day