danaur Posted April 13, 2023 Share Posted April 13, 2023 Hi, I'm trying to calculate the deviation from an array. Something like var u_max = [] for(var idx = 0; wait(1) && idx < samples; idx++){ const frequ = Scope.Channel1.measure("Frequency"); u_max[idx] = Scope.Channel1.measure("Maximum"); Scope.wait(); } const u_max_mean = u_max.reduce((a, b) => a + b, 0) / u_max.length; But I get a parse exception Uncaught exception at <anonymous script, id=1898586689712>:48: SyntaxError: Parse error 48 const u_max_mean = u_max.reduce((a, b) => a + b, 0) / u_max.length; Is array.reduce not available in the WaveForms inbuild scripting engine? Link to comment Share on other sites More sharing options...
0 attila Posted April 13, 2023 Share Posted April 13, 2023 Hi @danaur You can use:print(u_max.reduce(function(total, num){return total + num;})/u_max.length); You also have such stats available in the Scope Measurement enabled under the Show menu: Link to comment Share on other sites More sharing options...
0 danaur Posted April 19, 2023 Author Share Posted April 19, 2023 (edited) Thanks @attila print(u_max.reduce(function(total, num){return total + num;}) / u_max.length; does work like a charm. I want to calculate the standard deviation and just for the completeness is there a cleaner way than doing a loop? What works: // calculation standard deviation var SDprep = 0; for(var key in u_max) SDprep += Math.pow((parseFloat(u_max[key]) - u_max_mean),2); const u_max_dev = Math.sqrt(SDprep/(u_max.length-1)); This throws a parse exception: u_max_dev = Math.sqrt(u_max.map(function(x){return Math.pow(x - u_max_mean, 2);}).reduce(function(total, num){return (total + num)/u_max.length;}) I wonder if array.map is the issue here. It's propably a stupid mistake again. Edit: I know about the inbuild measurement functions. Quite useful. I couldn't figure out how to access the Scope.Measurements.Frequency Mean value in a script and how to reset the measurements from script. Edited April 19, 2023 by danaur Link to comment Share on other sites More sharing options...
0 attila Posted April 20, 2023 Share Posted April 20, 2023 Hi @danaur You are missing one parenthesis at the end.u_max_dev = Math.sqrt(u_max.map(function(x){return Math.pow(x - u_max_mean, 2);}).reduce(function(total, num){return (total + num)/u_max.length;})) After your first post I've added functions to be able to read mean,median... measurements from Script. It will be available in the next sw version. Link to comment Share on other sites More sharing options...
Question
danaur
Hi,
I'm trying to calculate the deviation from an array. Something like
But I get a parse exception
Is array.reduce not available in the WaveForms inbuild scripting engine?
Link to comment
Share on other sites
3 answers to this question
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now