Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] getPeaks gives error when calculating 1st polynomial order #21

Closed
Freymaurer opened this issue Feb 28, 2019 · 2 comments
Closed
Assignees

Comments

@Freymaurer
Copy link
Contributor

Describe the bug
When using the FSharp.Stats.Signal.PeakDetection.SecondDerivative.getPeaks function for the 1st polynomial order in combination with the second derivative, it results in an "System.IndexOutOfRangeException: Index was outside the bounds of the array." - error.

To Reproduce
Steps to reproduce the behavior:

I looked into the code and tried to narrow the origin of the error down and found it was the fault of the
Signal.Filtering.savitzky_golay function used for calculating the second derivative and for data smoothing.

open FSharp.Stats

///a = height of the gaussians peak, b = mean of gaussian distri, c = sigma of gaussian distribution,
///x = window on x -axis
let gaussianFunctionForY height mean sigma x =  height*(exp( -((x-mean) ** 2.) / (2.*(sigma ** 2.)) ))


let XX = [|0. .. 0.1 .. 100.|]
let gaussianYY = XX |> Array.map (fun x -> gaussianFunctionForY 50. 30. 0.3 x)

getPeaks 0. 1 5 XX gaussianYY  ///error seen above

let smoothedData = 
    Signal.Filtering.savitzky_golay 5 1 0 1 gaussianYY
    |> Array.ofSeq        ///no error, works as intended

Signal.Filtering.savitzky_golay 5 1 2 1 smoothedData  ///error seen above

Signal.Filtering.savitzky_golay 5 1 2 1 gaussianYY  ///error seen above

Signal.Filtering.savitzky_golay 5 1 1 1 gaussianYY //no error

Signal.Filtering.savitzky_golay 5 2 2 1 gaussianYY //no error

Apparently, the savitzky_golay function gives an error at the following line, but i am not 100% sure if i read the debugger correctly.

let m = (Algebra.LinearAlgebraManaged.pseudoInvers b).Row(deriv) * ((float(rate)**float(deriv)) * SpecialFunctions.Factorial.factorial(deriv))

Expected behavior
Should smooth data, while trying to fit the data to first order polynomials on second derivative.

OS and framework information (please complete the following information):

  • OS: Windows 10 professional
  • OS Version 1809
  • .NET Core SDK 2.2.104
  • .NET Framework 4.7.2 SDK

Additional context
I am using the FSharp.Stats developer branch

@kMutagene kMutagene added the bug label Feb 28, 2019
@kMutagene kMutagene changed the title getPeaks gives error when calculating 1st polynomial order [BUG] getPeaks gives error when calculating 1st polynomial order Feb 28, 2019
@ZimmerD ZimmerD assigned Freymaurer and unassigned ZimmerD Mar 12, 2019
@bvenn
Copy link
Member

bvenn commented May 12, 2019

The reason for the error is, that the pseudoinvers matrix b has just 2 rows and by setting the derivative to 2 you aim to access the non existing row number 3.

let deriv =2
let m = (Algebra.LinearAlgebraManaged.pseudoInvers b).Row(deriv) * ((float(rate)**float(deriv)) * SpecialFunctions.Factorial.factorial(deriv))

Explanation of this behaviour:
A first order polynomial takes the form f(x)=ax+b. The first derivative is f'(x)=a and consequently the second derivative is f''(x)=0, and therefore cannot be accessed.
I do not think that it is a good idea (and invalid), to use the savitzky golay filter with these parameters.

@bvenn bvenn removed the bug label May 12, 2019
bvenn pushed a commit that referenced this issue May 13, 2019
@bvenn
Copy link
Member

bvenn commented May 13, 2019

closed by a3c5a6d

@bvenn bvenn closed this as completed May 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants