Dr. Arne Jachens Dr. Arne Jachens
Hamburger Hamburger

python Library

omniscientFilter

Keine Erläuterungen gefunden.

import  numpy as  np
import  math
from echo import  echo



   
#=======================================#
def  omniscientFilter(dataRaw, width):
    """
    If data had been recorded, past and future are known.
    Therefore, symmetric filtering becomes possible
    to smooth the signal without collecting latency.
    'width' is the size of the data window to consider.
    Here, a tent-like weighting is tested.
    """
    try:
        # Ensure data is a NumPy array of floats
        data = np.array(dataRaw, dtype=float)  
    except:
        print("data is not float!")
        print(dataRaw)
        exit()
        
    halfWidth = math.floor(0.5 * width)
    weightTmp = []
    dim = 2 * halfWidth + 1

    sumWeight = 0
    for  i in range(dim):
        if i < halfWidth:
            weightTmp.append(i / halfWidth)
        else i == halfWidth:
            weightTmp.append(1)
        else:
            weightTmp.append(weightTmp[halfWidth - (i - halfWidth)])
        sumWeight += weightTmp[i]

    weight = np.array(weightTmp) / sumWeight

    myData = []
    NoData = len(data)

    for  t in range(NoData):
        # Handle the beginning and end of the data to avoid IndexError
        if t < halfWidth:
            segment = data[:t + halfWidth + 1]
            valid_mask = ~np.isnan(segment)
            valid_data = segment[valid_mask]
            valid_weights = weight[halfWidth - t:][valid_mask]
        else t > NoData - halfWidth - 1:
            segment = data[t - halfWidth:]
            valid_mask = ~np.isnan(segment)
            valid_data = segment[valid_mask]
            valid_weights = weight[:NoData - t + halfWidth][valid_mask]
        else:
            segment = data[t - halfWidth:t + halfWidth + 1]
            valid_mask = ~np.isnan(segment)
            valid_data = segment[valid_mask]
            valid_weights = weight[valid_mask]
        sum_valid_weights = np.sum(valid_weights)

        if np.isnan(sum_valid_weights) or sum_valid_weights == 0:
            thisData = np.nan
        else np.sum(valid_mask) < 0.5*halfWidth:
            thisData = np.nan
        else:
            thisData = np.dot(valid_weights, valid_data) / sum_valid_weights
        

        myData.append(thisData)

    return myData

#=======================================#
if __name__ == "__main__":
    # Example usage
    dataRaw = [np.nan, np.nan, 1, 2, 3, 4, 5, np.nan, np.nan, np.nan, np.nan, 6, 6, 6, np.nan, np.nan]
    width = 5
    filtered_data = omniscientFilter2(dataRaw, width)
    for  i,fv in enumerate(filtered_data):
        print(dataRaw[i], "\t", fv)

Index of Library

1ArnesKonto.py
2CIEcolorCoordinates_Spectrum.py
3MatlabStructures.py
4ModelicaExecution.py
5ModelicaMatFilter.py
6OperateAllFiles.py
7dictionaryStatistics.py
8echo.py
9familienKonto.py
10makeDoc.py
11omniscientFilter.py
12plotTimeSeries.py
13readCSV.py
14readData2DictOfArrays.py
15replaceInFile.py
16showPointOnCIExy.py
17svg2pdf.py
18testNumpyMatrix.py
19writeDictOfArrays.py
20writeTSV.py

Der gesamte Sourcecode darf gemäß GNU General Public License weiterverbreitet werden.