Created
          September 15, 2020 09:06 
        
      - 
      
 - 
        
Save tk009999/c5d15beea6ada4593ebd66a24154a4f5 to your computer and use it in GitHub Desktop.  
    #Unity3D
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public static class ExtensionTools | |
| { | |
| private static void ScanPeak(float[] arr, int length, float[] pvlst) | |
| { | |
| for (int i = 1; i < length - 1; i++) | |
| { | |
| if ((arr[i] < arr[i - 1]) && (arr[i] < arr[i + 1])) | |
| { | |
| pvlst[i] = -1; | |
| } | |
| else if ((arr[i] > arr[i - 1]) && (arr[i] > arr[i + 1])) | |
| { | |
| pvlst[i] = 1; | |
| } | |
| } | |
| if (arr[0] < arr[1]) | |
| { | |
| pvlst[0] = -1; | |
| } | |
| else if (arr[0] > arr[1]) | |
| { | |
| pvlst[0] = 1; | |
| } | |
| if (arr[length - 1] < arr[length - 2]) | |
| { | |
| pvlst[length - 1] = -1; | |
| } | |
| else if (arr[length - 1] > arr[length - 2]) | |
| { | |
| pvlst[length - 1] = 1; | |
| } | |
| } | |
| private static WaveCrestData MaxNeighboringPeak(float[] arr, int length) | |
| { | |
| float[] pvlst = new float[length]; | |
| int peak = 0; | |
| int vall = 0; | |
| float pair = 0; | |
| float max = 0; | |
| Array.Clear(pvlst, 0, pvlst.Length); | |
| ScanPeak(arr, length, pvlst); | |
| for (int i = 0; i < length; i++) | |
| { | |
| if (pvlst[i] == 1) | |
| { | |
| peak = i; | |
| pair++; | |
| } | |
| if (pvlst[i] == -1) | |
| { | |
| vall = i; | |
| pair++; | |
| } | |
| if (2 == pair) | |
| { | |
| pair = 0; | |
| if (arr[peak] - arr[vall] > max) | |
| max = arr[peak] - arr[vall]; | |
| } | |
| } | |
| WaveCrestData result = new WaveCrestData(); | |
| result.Pvlst = pvlst.ToList(); | |
| result.Max = max; | |
| return result; | |
| } | |
| public static WaveCrestData GetWaveCrest(this List<float> data) | |
| { | |
| int length = Buffer.ByteLength(data.ToArray()) / Marshal.SizeOf(data[0]); | |
| return MaxNeighboringPeak(data.ToArray(), length); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment