3 people like it.

Calculate the root-mean-square (RMS) of a dataset

Calculate the root-mean-square of a dataset. Eg. given the voltages over time of an alternating current, calculate the RMS voltage.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
let voltagesEU = [|0.; -325.; 0.; 325.|]
let voltagesUS = [|0.; -170.; 0.; 170.|]

let RMS data =
    data 
    |> Seq.averageBy (fun v -> v ** 2.)
    |> sqrt

do
    printfn "EU: %0.0f volts" (voltagesEU |> RMS) // 230
    printfn "US: %0.0f volts" (voltagesUS |> RMS) // 120
val voltagesEU : float []

Full name: Script.voltagesEU
val voltagesUS : float []

Full name: Script.voltagesUS
val RMS : data:seq<float> -> float

Full name: Script.RMS
val data : seq<float>
module Seq

from Microsoft.FSharp.Collections
val averageBy : projection:('T -> 'U) -> source:seq<'T> -> 'U (requires member ( + ) and member DivideByInt and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.averageBy
val v : float
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view New version

More information

Link:http://fssnip.net/jh
Posted:3 years ago
Author:Kit Eason
Tags: learning f#